Skip to content

Commit

Permalink
Updategrafanalinter (#320)
Browse files Browse the repository at this point in the history
* Update grafana_dashboard.go

Signed-off-by: ghostinsoba <125242947+ghostinsoba@users.noreply.github.com>

* Update grafana_dashboard_test.go

Signed-off-by: ghostinsoba <125242947+ghostinsoba@users.noreply.github.com>

---------

Signed-off-by: ghostinsoba <125242947+ghostinsoba@users.noreply.github.com>
  • Loading branch information
ghostinsoba authored Aug 26, 2024
1 parent 8df210c commit 91bff48
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
60 changes: 31 additions & 29 deletions tools/validation/grafana_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
),
)
}
legacyDatasourceUIDs, hardcodedDatasourceUIDs, invalidPrometheusDatasourceUIDs := evaluateDeprecatedDatasourceUIDs(panel)
legacyDatasourceUIDs, hardcodedDatasourceUIDs, nonRecommendedPrometheusDatasourceUIDs := evaluateDeprecatedDatasourceUIDs(panel)
for _, datasourceUID := range legacyDatasourceUIDs {
msgs.Add(
NewError(
Expand All @@ -137,13 +137,13 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
),
)
}
for _, datasourceUID := range invalidPrometheusDatasourceUIDs {
for _, datasourceUID := range nonRecommendedPrometheusDatasourceUIDs {
msgs.Add(
NewError(
fileName,
"invalid prometheus datasource uid",
"non-recommended prometheus datasource uid",
fmt.Sprintf("Panel %s contains invalid datasource uid: '%s', required to be one of: %s",
panelTitle, datasourceUID, prometheusDatasourceValidUIDsMessageString),
panelTitle, datasourceUID, prometheusDatasourceRecommendedUIDsMessageString),
),
)
}
Expand All @@ -154,12 +154,12 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
if evaluatePrometheusDatasourceTemplateVariable(dashboardTemplate) {
hasPrometheusDatasourceVariable = true
}
if queryVariable, ok := evaluateInvalidPrometheusDatasourceQueryTemplateVariable(dashboardTemplate); ok {
if queryVariable, ok := evaluateNonRecommendedPrometheusDatasourceQueryTemplateVariable(dashboardTemplate); ok {
msgs.Add(
NewError(
fileName,
"invalid prometheus datasource query variable",
fmt.Sprintf("Dashboard variable '%s' must use one of: %s as it's datasource", queryVariable, prometheusDatasourceValidUIDsMessageString),
"non-recommended prometheus datasource query variable",
fmt.Sprintf("Dashboard variable '%s' must use one of: %s as it's datasource", queryVariable, prometheusDatasourceRecommendedUIDsMessageString),
),
)
}
Expand All @@ -170,7 +170,7 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
fileName,
"missing prometheus datasource variable",
fmt.Sprintf("Dashboard must contain prometheus variable with query type: '%s' and name: '%s'",
prometheusDatasourceQuery, prometheusDatasourceValidName),
prometheusDatasourceQuery, prometheusDatasourceRecommendedName),
),
)
}
Expand Down Expand Up @@ -211,40 +211,42 @@ func extractDashboardTemplates(dashboard gjson.Result) []gjson.Result {
}

const (
prometheusDatasourceType = "prometheus"
prometheusDatasourceQuery = "prometheus"
prometheusDatasourceValidName = "ds_prometheus"
prometheusDatasourceType = "prometheus"
prometheusDatasourceQuery = "prometheus"
prometheusDatasourceRecommendedName = "ds_prometheus"
)

var (
// both ${datasource_uid} and $datasource_uid will be parsed as $datasource_uid
prometheusDatasourceValidUIDs = []string{
"$" + prometheusDatasourceValidName,
"${" + prometheusDatasourceValidName + "}",
prometheusDatasourceRecommendedUIDs = []string{
"$" + prometheusDatasourceRecommendedName,
"${" + prometheusDatasourceRecommendedName + "}",
}
prometheusDatasourceValidUIDsMessageString = func() string {
res := make([]string, 0, len(prometheusDatasourceValidUIDs))
for _, prometheusDatasourceValidUID := range prometheusDatasourceValidUIDs {
res = append(res, fmt.Sprintf("'%s'", prometheusDatasourceValidUID))
prometheusDatasourceRecommendedUIDsMessageString = func() string {
res := make([]string, 0, len(prometheusDatasourceRecommendedUIDs))
for _, prometheusDatasourceRecommendedUID := range prometheusDatasourceRecommendedUIDs {
res = append(res, fmt.Sprintf("'%s'", prometheusDatasourceRecommendedUID))
}
return strings.Join(res, ", ")
}()
)

func isValidPrometheusDatasourceUID(prometheusDatasourceUID string) bool {
for _, prometheusDatasourceValidUID := range prometheusDatasourceValidUIDs {
if prometheusDatasourceValidUID == prometheusDatasourceUID {
func isRecommendedPrometheusDatasourceUID(prometheusDatasourceUID string) bool {
for _, prometheusDatasourceRecommendedUID := range prometheusDatasourceRecommendedUIDs {
if prometheusDatasourceRecommendedUID == prometheusDatasourceUID {
return true
}
}
return false
}

func evaluateDeprecatedDatasourceUIDs(panel gjson.Result) (legacyUIDs, hardcodedUIDs, invalidPrometheusUIDs []string) {
func evaluateDeprecatedDatasourceUIDs(panel gjson.Result) (
legacyUIDs, hardcodedUIDs, nonRecommendedPrometheusUIDs []string,
) {
targets := panel.Get("targets").Array()
legacyUIDs = make([]string, 0)
hardcodedUIDs = make([]string, 0)
invalidPrometheusUIDs = make([]string, 0)
nonRecommendedPrometheusUIDs = make([]string, 0)
for _, target := range targets {
datasource := target.Get("datasource")
if datasource.Exists() {
Expand All @@ -263,13 +265,13 @@ func evaluateDeprecatedDatasourceUIDs(panel gjson.Result) (legacyUIDs, hardcoded
datasourceType := datasource.Get("type")
if datasourceType.Exists() {
datasourceTypeStr := datasourceType.String()
if datasourceTypeStr == prometheusDatasourceType && !isValidPrometheusDatasourceUID(uidStr) {
invalidPrometheusUIDs = append(invalidPrometheusUIDs, uidStr)
if datasourceTypeStr == prometheusDatasourceType && !isRecommendedPrometheusDatasourceUID(uidStr) {
nonRecommendedPrometheusUIDs = append(nonRecommendedPrometheusUIDs, uidStr)
}
}
}
}
return hardcodedUIDs, legacyUIDs, invalidPrometheusUIDs
return hardcodedUIDs, legacyUIDs, nonRecommendedPrometheusUIDs
}

var (
Expand Down Expand Up @@ -326,13 +328,13 @@ func evaluatePrometheusDatasourceTemplateVariable(dashboardTemplate gjson.Result
return false
}
templateName := dashboardTemplate.Get("name")
if templateName.String() != prometheusDatasourceValidName {
if templateName.String() != prometheusDatasourceRecommendedName {
return false
}
return true
}

func evaluateInvalidPrometheusDatasourceQueryTemplateVariable(dashboardTemplate gjson.Result) (string, bool) {
func evaluateNonRecommendedPrometheusDatasourceQueryTemplateVariable(dashboardTemplate gjson.Result) (string, bool) {
templateType := dashboardTemplate.Get("type")
if !templateType.Exists() {
return "", false
Expand All @@ -349,7 +351,7 @@ func evaluateInvalidPrometheusDatasourceQueryTemplateVariable(dashboardTemplate
return "", false
}
datasourceUID := datasource.Get("uid")
if isValidPrometheusDatasourceUID(datasourceUID.String()) {
if isRecommendedPrometheusDatasourceUID(datasourceUID.String()) {
return "", false
}
templateName := dashboardTemplate.Get("name")
Expand Down
6 changes: 3 additions & 3 deletions tools/validation/grafana_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,16 @@ func TestValidateGrafanaDashboardFile(t *testing.T) {
NewError("dashboard.json", "deprecated panel type", "Panel Plugin Single Panel is of deprecated type: 'graph', consider using 'timeseries'"),
NewError("dashboard.json", "deprecated interval", "Panel Plugin Single Panel contains deprecated interval: 'interval_rv', consider using '$__rate_interval'"),
NewError("dashboard.json", "legacy datasource uid", "Panel Plugin Single Panel contains legacy datasource uid: 'prometheus_datasource_uid', consider resaving dashboard using newer version of Grafana"),
NewError("dashboard.json", "invalid prometheus datasource uid", "Panel Plugin Single Panel contains invalid datasource uid: 'prometheus_datasource_uid', required to be one of: '$ds_prometheus', '${ds_prometheus}'"),
NewError("dashboard.json", "non-recommended prometheus datasource uid", "Panel Plugin Single Panel contains invalid datasource uid: 'prometheus_datasource_uid', required to be one of: '$ds_prometheus', '${ds_prometheus}'"),
NewError("dashboard.json", "deprecated interval", "Panel Panel Inside Row contains deprecated interval: 'interval_sx3', consider using '$__rate_interval'"),
NewError("dashboard.json", "legacy alert rule", "Panel Panel Inside Row contains legacy alert rule: 'Panel Inside Row Alert Rule', consider using external alertmanager"),
NewError("dashboard.json", "legacy datasource uid", "Panel Panel Inside Row contains legacy datasource uid: 'prometheus_datasource_uid', consider resaving dashboard using newer version of Grafana"),
NewError("dashboard.json", "hardcoded datasource uid", "Panel Panel Inside Row contains hardcoded datasource uid: 'prometheus_datasource_uid', consider using grafana variable of type 'Datasource'"),
NewError("dashboard.json", "deprecated panel type", "Panel Plugin Panel Inside Row is of deprecated type: 'flant-statusmap-panel', consider using 'state-timeline'"),
NewError("dashboard.json", "deprecated interval", "Panel Plugin Panel Inside Row contains deprecated interval: 'interval_sx4', consider using '$__rate_interval'"),
NewError("dashboard.json", "legacy datasource uid", "Panel Plugin Panel Inside Row contains legacy datasource uid: 'prometheus_datasource_uid', consider resaving dashboard using newer version of Grafana"),
NewError("dashboard.json", "invalid prometheus datasource uid", "Panel Plugin Panel Inside Row contains invalid datasource uid: 'prometheus_datasource_uid', required to be one of: '$ds_prometheus', '${ds_prometheus}'"),
NewError("dashboard.json", "invalid prometheus datasource query variable", "Dashboard variable 'dashboard_variable' must use one of: '$ds_prometheus', '${ds_prometheus}' as it's datasource"),
NewError("dashboard.json", "non-recommended prometheus datasource uid", "Panel Plugin Panel Inside Row contains invalid datasource uid: 'prometheus_datasource_uid', required to be one of: '$ds_prometheus', '${ds_prometheus}'"),
NewError("dashboard.json", "non-recommended prometheus datasource query variable", "Dashboard variable 'dashboard_variable' must use one of: '$ds_prometheus', '${ds_prometheus}' as it's datasource"),
NewError("dashboard.json", "missing prometheus datasource variable", "Dashboard must contain prometheus variable with query type: 'prometheus' and name: 'ds_prometheus'"),
}}

Expand Down

0 comments on commit 91bff48

Please sign in to comment.