Skip to content

Commit

Permalink
Update datasource metrics middleware (#1151)
Browse files Browse the repository at this point in the history
* Remove datasource label and update test

* Fix potential nil panic

* Update to create the labels inside the middleware function
  • Loading branch information
aangelisc authored Nov 22, 2024
1 parent 2d81559 commit 0263252
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
40 changes: 13 additions & 27 deletions backend/httpclient/datasource_metrics_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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"},
)
)

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions backend/httpclient/datasource_metrics_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0263252

Please sign in to comment.