diff --git a/backend/httpclient/datasource_metrics_middleware.go b/backend/httpclient/datasource_metrics_middleware.go index 9f14ef5f4..45a8211f6 100644 --- a/backend/httpclient/datasource_metrics_middleware.go +++ b/backend/httpclient/datasource_metrics_middleware.go @@ -26,7 +26,7 @@ var ( Name: "datasource_request_total", Help: "A counter for outgoing requests for an external data source", }, - []string{"datasource", "datasource_type", "code", "method", "secure_socks_ds_proxy_enabled", "endpoint"}, + []string{"datasource_type", "code", "method", "secure_socks_ds_proxy_enabled", "endpoint"}, ) datasourceRequestHistogram = promauto.NewHistogramVec( @@ -35,7 +35,7 @@ var ( Name: "datasource_request_duration_seconds", Help: "histogram of durations of outgoing external data source requests sent from Grafana", Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 50, 100}, - }, []string{"datasource", "datasource_type", "code", "method", "secure_socks_ds_proxy_enabled", "endpoint"}, + }, []string{"datasource_type", "code", "method", "secure_socks_ds_proxy_enabled", "endpoint"}, ) datasourceResponseHistogram = promauto.NewHistogramVec( @@ -49,7 +49,7 @@ var ( NativeHistogramBucketFactor: 1.1, NativeHistogramMaxBucketNumber: 100, NativeHistogramMinResetDuration: time.Hour, - }, []string{"datasource", "datasource_type", "secure_socks_ds_proxy_enabled", "endpoint"}, + }, []string{"datasource_type", "secure_socks_ds_proxy_enabled", "endpoint"}, ) datasourceRequestsInFlight = promauto.NewGaugeVec( @@ -58,7 +58,7 @@ var ( Name: "datasource_request_in_flight", Help: "A gauge of outgoing external data source requests currently being sent by Grafana", }, - []string{"datasource", "datasource_type", "secure_socks_ds_proxy_enabled", "endpoint"}, + []string{"datasource_type", "secure_socks_ds_proxy_enabled", "endpoint"}, ) ) @@ -96,19 +96,6 @@ func DataSourceMetricsMiddleware() Middleware { return next } - datasourceName, exists := opts.Labels["datasource_name"] - if !exists { - return next - } - - datasourceLabelName, err := sanitizeLabelName(datasourceName) - // if the datasource named cannot be turned into a prometheus - // label we will skip instrumenting these metrics. - if err != nil { - log.DefaultLogger.Error("failed to sanitize datasource name", "error", err) - return next - } - datasourceType, exists := opts.Labels["datasource_type"] if !exists { return next @@ -121,22 +108,21 @@ func DataSourceMetricsMiddleware() Middleware { return next } - labels := prometheus.Labels{ - "datasource": datasourceLabelName, - "datasource_type": datasourceLabelType, - "secure_socks_ds_proxy_enabled": strconv.FormatBool(opts.ProxyOptions != nil && opts.ProxyOptions.Enabled), - } - - return executeMiddlewareFunc(next, labels) + return executeMiddlewareFunc(next, datasourceLabelType, strconv.FormatBool(opts.ProxyOptions != nil && opts.ProxyOptions.Enabled)) }) } -func executeMiddleware(next http.RoundTripper, labels prometheus.Labels) http.RoundTripper { +func executeMiddleware(next http.RoundTripper, datasourceType string, secureSocksProxyEnabled string) http.RoundTripper { return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { ctx := r.Context() - labels["endpoint"] = "" + endpoint := "" if ep := ctx.Value(endpointctx.EndpointCtxKey); ep != nil { - labels["endpoint"] = fmt.Sprintf("%v", ep) + endpoint = fmt.Sprintf("%v", ep) + } + labels := prometheus.Labels{ + "datasource_type": datasourceType, + "secure_socks_ds_proxy_enabled": secureSocksProxyEnabled, + "endpoint": endpoint, } requestCounter := datasourceRequestCounter.MustCurryWith(labels) requestHistogram := datasourceRequestHistogram.MustCurryWith(labels) diff --git a/backend/httpclient/datasource_metrics_middleware_test.go b/backend/httpclient/datasource_metrics_middleware_test.go index 44782f38b..bb2d2493d 100644 --- a/backend/httpclient/datasource_metrics_middleware_test.go +++ b/backend/httpclient/datasource_metrics_middleware_test.go @@ -14,7 +14,7 @@ func TestDataSourceMetricsMiddleware(t *testing.T) { origExecuteMiddlewareFunc := executeMiddlewareFunc executeMiddlewareCalled := false middlewareCalled := false - executeMiddlewareFunc = func(next http.RoundTripper, _ prometheus.Labels) http.RoundTripper { + executeMiddlewareFunc = func(next http.RoundTripper, _ string, _ string) http.RoundTripper { executeMiddlewareCalled = true return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { middlewareCalled = true @@ -48,11 +48,11 @@ func TestDataSourceMetricsMiddleware(t *testing.T) { require.False(t, middlewareCalled) }) - t.Run("Without data source name label options set should return next http.RoundTripper", func(t *testing.T) { + t.Run("Without data source type label options set should return next http.RoundTripper", func(t *testing.T) { origExecuteMiddlewareFunc := executeMiddlewareFunc executeMiddlewareCalled := false middlewareCalled := false - executeMiddlewareFunc = func(next http.RoundTripper, _ prometheus.Labels) http.RoundTripper { + executeMiddlewareFunc = func(next http.RoundTripper, _ string, _ string) http.RoundTripper { executeMiddlewareCalled = true return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { middlewareCalled = true @@ -86,14 +86,14 @@ func TestDataSourceMetricsMiddleware(t *testing.T) { require.False(t, middlewareCalled) }) - t.Run("With datasource name label options set should execute middleware", func(t *testing.T) { + t.Run("With datasource type label options set should execute middleware", func(t *testing.T) { origExecuteMiddlewareFunc := executeMiddlewareFunc executeMiddlewareCalled := false labels := prometheus.Labels{} middlewareCalled := false - executeMiddlewareFunc = func(next http.RoundTripper, datasourceLabel prometheus.Labels) http.RoundTripper { + executeMiddlewareFunc = func(next http.RoundTripper, datasourceLabel string, secureSocksProxyEnabled string) http.RoundTripper { executeMiddlewareCalled = true - labels = datasourceLabel + labels = prometheus.Labels{"datasource_type": datasourceLabel, "secure_socks_ds_proxy_enabled": secureSocksProxyEnabled} return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { middlewareCalled = true return next.RoundTrip(r) @@ -111,14 +111,14 @@ func TestDataSourceMetricsMiddleware(t *testing.T) { { description: "secure socks ds proxy is disabled", httpClientOptions: Options{ - Labels: map[string]string{"datasource_name": "My Data Source 123", "datasource_type": "prometheus"}, + Labels: map[string]string{"datasource_type": "prometheus"}, }, expectedSecureSocksDSProxyEnabled: "false", }, { description: "secure socks ds proxy is enabled", httpClientOptions: Options{ - Labels: map[string]string{"datasource_name": "My Data Source 123", "datasource_type": "prometheus"}, + Labels: map[string]string{"datasource_type": "prometheus"}, ProxyOptions: &proxy.Options{Enabled: true}, }, expectedSecureSocksDSProxyEnabled: "true", @@ -147,8 +147,7 @@ func TestDataSourceMetricsMiddleware(t *testing.T) { require.Len(t, ctx.callChain, 1) require.ElementsMatch(t, []string{"finalrt"}, ctx.callChain) require.True(t, executeMiddlewareCalled) - require.Len(t, labels, 3) - require.Equal(t, "My_Data_Source_123", labels["datasource"]) + require.Len(t, labels, 2) require.Equal(t, "prometheus", labels["datasource_type"]) require.Equal(t, tt.expectedSecureSocksDSProxyEnabled, labels["secure_socks_ds_proxy_enabled"]) require.True(t, middlewareCalled)