Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostinsoba committed Aug 12, 2024
1 parent 29e0cb2 commit 0005fa6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
35 changes: 29 additions & 6 deletions tools/validation/grafana_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
NewError(
fileName,
"invalid prometheus datasource uid",
fmt.Sprintf("Panel %s contains invalid datasource uid: '%s', required to be: '%s'",
panelTitle, datasourceUID, prometheusDatasourceValidUID),
fmt.Sprintf("Panel %s contains invalid datasource uid: '%s', required to be one of: %s",
panelTitle, datasourceUID, prometheusDatasourceValidUIDsMessageString),
),
)
}
Expand All @@ -159,7 +159,7 @@ func validateGrafanaDashboardFile(fileName string, fileContent []byte) *Messages
NewError(
fileName,
"invalid prometheus datasource query variable",
fmt.Sprintf("Dashboard variable '%s' must use '%s' as it's datasource", queryVariable, prometheusDatasourceValidUID),
fmt.Sprintf("Dashboard variable '%s' must use one of: %s as it's datasource", queryVariable, prometheusDatasourceValidUIDsMessageString),
),
)
}
Expand Down Expand Up @@ -214,9 +214,32 @@ const (
prometheusDatasourceType = "prometheus"
prometheusDatasourceQuery = "prometheus"
prometheusDatasourceValidName = "ds_prometheus"
prometheusDatasourceValidUID = "${" + prometheusDatasourceValidName + "}"
)

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

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

func evaluateDeprecatedDatasourceUIDs(panel gjson.Result) (legacyUIDs, hardcodedUIDs, invalidPrometheusUIDs []string) {
targets := panel.Get("targets").Array()
legacyUIDs = make([]string, 0)
Expand All @@ -240,7 +263,7 @@ func evaluateDeprecatedDatasourceUIDs(panel gjson.Result) (legacyUIDs, hardcoded
datasourceType := datasource.Get("type")
if datasourceType.Exists() {
datasourceTypeStr := datasourceType.String()
if datasourceTypeStr == prometheusDatasourceType && uidStr != prometheusDatasourceValidUID {
if datasourceTypeStr == prometheusDatasourceType && !isValidPrometheusDatasourceUID(uidStr) {
invalidPrometheusUIDs = append(invalidPrometheusUIDs, uidStr)
}
}
Expand Down Expand Up @@ -326,7 +349,7 @@ func evaluateInvalidPrometheusDatasourceQueryTemplateVariable(dashboardTemplate
return "", false
}
datasourceUID := datasource.Get("uid")
if datasourceUID.String() == prometheusDatasourceValidUID {
if isValidPrometheusDatasourceUID(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: '${ds_prometheus}'"),
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", "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: '${ds_prometheus}'"),
NewError("dashboard.json", "invalid prometheus datasource query variable", "Dashboard variable 'dashboard_variable' must use '${ds_prometheus}' as it's datasource"),
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", "missing prometheus datasource variable", "Dashboard must contain prometheus variable with query type: 'prometheus' and name: 'ds_prometheus'"),
}}

Expand Down

0 comments on commit 0005fa6

Please sign in to comment.