grafana/pkg/services/pluginsintegration/clientmiddleware/tracing_header_middleware_test.go
Giedrius Statkevičius 0485cf34cd
Plugins: Pass through dashboard/contextual HTTP headers to plugins/datasources (#60301)
`X-Dashboard-Uid`, `X-Datasource-Uid`, `X-Grafana-Org-Id`, `X-Panel-Id` are very useful headers set
by Grafana front-end that we would like to see on the data source as
well. This is so that it would be possible to pinpoint from where slow
queries are coming in Mimir/Thanos/Cortex/etc., for example. Relevant
Mimir code lines:
0a94f26203/pkg/frontend/transport/handler.go (L182-L184)

Tested manually that with these changes the headers are visible.
2022-12-22 11:15:00 +01:00

164 lines
5.4 KiB
Go

package clientmiddleware
import (
"net/http"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/plugins/manager/client/clienttest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
)
func TestTracingHeaderMiddleware(t *testing.T) {
t.Run("When a request comes in with tracing headers set to empty strings", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/some/thing", nil)
require.NoError(t, err)
req.Header[`X-Dashboard-Uid`] = []string{}
req.Header[`X-Datasource-Uid`] = []string{}
req.Header[`X-Grafana-Org-Id`] = []string{}
req.Header[`X-Panel-Id`] = []string{}
pluginCtx := backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
}
t.Run("tracing headers are not set for query data", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.QueryData(req.Context(), &backend.QueryDataRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.QueryDataReq.GetHTTPHeaders(), 0)
})
t.Run("tracing headers are not set for health check", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.CheckHealth(req.Context(), &backend.CheckHealthRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.CheckHealthReq.GetHTTPHeaders(), 0)
})
})
t.Run("When a request comes in with tracing headers empty", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/some/thing", nil)
require.NoError(t, err)
pluginCtx := backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
}
t.Run("tracing headers are not set for query data", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.QueryData(req.Context(), &backend.QueryDataRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.QueryDataReq.GetHTTPHeaders(), 0)
})
t.Run("tracing headers are not set for health check", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.CheckHealth(req.Context(), &backend.CheckHealthRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.CheckHealthReq.GetHTTPHeaders(), 0)
})
})
t.Run("When a request comes in with tracing headers set", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/some/thing", nil)
require.NoError(t, err)
req.Header[`X-Dashboard-Uid`] = []string{"lN53lOcVk"}
req.Header[`X-Datasource-Uid`] = []string{"aIyC_OcVz"}
req.Header[`X-Grafana-Org-Id`] = []string{"1"}
req.Header[`X-Panel-Id`] = []string{"2"}
pluginCtx := backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
}
t.Run("tracing headers are set for query data", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.QueryData(req.Context(), &backend.QueryDataRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.QueryDataReq.GetHTTPHeaders(), 4)
require.Equal(t, `lN53lOcVk`, cdt.QueryDataReq.GetHTTPHeader(`X-Dashboard-Uid`))
require.Equal(t, `aIyC_OcVz`, cdt.QueryDataReq.GetHTTPHeader(`X-Datasource-Uid`))
require.Equal(t, `1`, cdt.QueryDataReq.GetHTTPHeader(`X-Grafana-Org-Id`))
require.Equal(t, `2`, cdt.QueryDataReq.GetHTTPHeader(`X-Panel-Id`))
})
t.Run("tracing headers are set for health check", func(t *testing.T) {
cdt := clienttest.NewClientDecoratorTest(t,
clienttest.WithReqContext(req, &user.SignedInUser{
IsAnonymous: true,
Login: "anonymous"},
),
clienttest.WithMiddlewares(NewTracingHeaderMiddleware()),
)
_, err = cdt.Decorator.CheckHealth(req.Context(), &backend.CheckHealthRequest{
PluginContext: pluginCtx,
Headers: map[string]string{},
})
require.NoError(t, err)
require.Len(t, cdt.CheckHealthReq.GetHTTPHeaders(), 4)
require.Equal(t, `lN53lOcVk`, cdt.CheckHealthReq.GetHTTPHeader(`X-Dashboard-Uid`))
require.Equal(t, `aIyC_OcVz`, cdt.CheckHealthReq.GetHTTPHeader(`X-Datasource-Uid`))
require.Equal(t, `1`, cdt.CheckHealthReq.GetHTTPHeader(`X-Grafana-Org-Id`))
require.Equal(t, `2`, cdt.CheckHealthReq.GetHTTPHeader(`X-Panel-Id`))
})
})
}