grafana/pkg/plugins/pluginrequestmeta/plugin_request_meta_test.go
Giuseppe Guerra f5076d1868
Plugins: Add status_source label to plugin request metrics (#76236)
* Plugins: Chore: Renamed instrumentation middleware to metrics middleware

* Removed repeated logger attributes in middleware and contextual logger

* renamed loggerParams to logParams

* PR review suggestion

* Add pluginsInstrumentationStatusSource feature toggle

* Plugin error source prometheus metrics

* Add error_source to logs

* re-generate feature toggles

* fix compilation issues

* remove unwanted changes

* Removed logger middleware changes, implement error source using context

* Renamed pluginmeta to pluginrequestmeta, changed some method names

* Fix comment

* pluginrequestmeta.go -> plugin_request_meta.go

* Replaced plugin request meta with status source

* Add tests for pluginrequestmeta status source

* Fix potential nil pointer dereference in instrmentation middleware

* Add metrics middleware tests

* Sort imports in clienttest.go

* Add StatusSourceFromContext test

* Add error_source label to plugin_request_duration_seconds

* Re-generate feature flags

* lint

* Use StatusSourcePlugin by default

* re-generate feature flags
2023-10-17 10:27:45 +02:00

51 lines
1.5 KiB
Go

package pluginrequestmeta
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestStatusSource(t *testing.T) {
t.Run("WithStatusSource", func(t *testing.T) {
ctx := context.Background()
ss := StatusSourceFromContext(ctx)
require.Equal(t, StatusSourcePlugin, ss)
ctx = WithStatusSource(ctx, StatusSourceDownstream)
ss = StatusSourceFromContext(ctx)
require.Equal(t, StatusSourceDownstream, ss)
})
t.Run("WithDownstreamStatusSource", func(t *testing.T) {
t.Run("Returns error if no status source is set", func(t *testing.T) {
ctx := context.Background()
err := WithDownstreamStatusSource(ctx)
require.Error(t, err)
require.Equal(t, StatusSourcePlugin, StatusSourceFromContext(ctx))
})
t.Run("Should mutate context if status source is set", func(t *testing.T) {
ctx := WithStatusSource(context.Background(), StatusSourcePlugin)
err := WithDownstreamStatusSource(ctx)
require.NoError(t, err)
require.Equal(t, StatusSourceDownstream, StatusSourceFromContext(ctx))
})
})
t.Run("StatusSourceFromContext", func(t *testing.T) {
t.Run("Background returns StatusSourcePlugin", func(t *testing.T) {
ctx := context.Background()
ss := StatusSourceFromContext(ctx)
require.Equal(t, StatusSourcePlugin, ss)
})
t.Run("Context with status source returns the set status source", func(t *testing.T) {
ctx := WithStatusSource(context.Background(), StatusSourcePlugin)
ss := StatusSourceFromContext(ctx)
require.Equal(t, StatusSourcePlugin, ss)
})
})
}