From 0005fa6a9de143b1288d833f66c46903c57bee49 Mon Sep 17 00:00:00 2001 From: "timur.kamaev" Date: Mon, 12 Aug 2024 21:59:08 +0600 Subject: [PATCH] update --- tools/validation/grafana_dashboard.go | 35 ++++++++++++++++++---- tools/validation/grafana_dashboard_test.go | 6 ++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/tools/validation/grafana_dashboard.go b/tools/validation/grafana_dashboard.go index 9158ef2d44..e1f9e28447 100644 --- a/tools/validation/grafana_dashboard.go +++ b/tools/validation/grafana_dashboard.go @@ -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), ), ) } @@ -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), ), ) } @@ -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) @@ -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) } } @@ -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") diff --git a/tools/validation/grafana_dashboard_test.go b/tools/validation/grafana_dashboard_test.go index c9c385c716..e51ae9dab1 100644 --- a/tools/validation/grafana_dashboard_test.go +++ b/tools/validation/grafana_dashboard_test.go @@ -523,7 +523,7 @@ 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"), @@ -531,8 +531,8 @@ func TestValidateGrafanaDashboardFile(t *testing.T) { 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'"), }}