From 91bff48b61c78efcf27772eab5eecac7aa38c6a6 Mon Sep 17 00:00:00 2001 From: ghostinsoba <125242947+ghostinsoba@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:54:28 +0600 Subject: [PATCH] Updategrafanalinter (#320) * 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> --- tools/validation/grafana_dashboard.go | 60 +++++++++++----------- tools/validation/grafana_dashboard_test.go | 6 +-- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/tools/validation/grafana_dashboard.go b/tools/validation/grafana_dashboard.go index e1f9e28447..49e3c7e9ed 100644 --- a/tools/validation/grafana_dashboard.go +++ b/tools/validation/grafana_dashboard.go @@ -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( @@ -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), ), ) } @@ -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), ), ) } @@ -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), ), ) } @@ -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() { @@ -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 ( @@ -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 @@ -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") diff --git a/tools/validation/grafana_dashboard_test.go b/tools/validation/grafana_dashboard_test.go index e51ae9dab1..9f4e5265fd 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 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"), @@ -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 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'"), }}