Chore: Implement OpenTelemtry in Grafana (#42674)
* Separate Tracer interface to TracerService and Tracer * Fix lint * Fix:Make it possible to start spans for both opentracing and opentelemetry in ds proxy * Add span methods, use span interface for rest of tracing * Fix logs in tracing * Fix tests that are related to tracing * Fix resourcepermissions test * Fix some tests * Fix more tests * Add TracingService to wire cli runner * Remove GlobalTracer from bus * Renaming test function * Remove GlobalTracer from TSDB * Replace GlobalTracer in api * Adjust tests to the InitializeForTests func * Remove GlobalTracer from services * Remove GlobalTracer * Remove bus.NewTest * Remove Tracer interface * Add InitializeForBus * Simplify tests * Clean up tests * Rename TracerService to Tracer * Update pkg/middleware/request_tracing.go Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * Initialize tracer before passing it to SQLStore initialization in commands * Remove tests for opentracing * Set span attributes correctly, remove unnecessary trace initiliazation form test * Add tracer instance to newSQLStore * Fix changes due to rebase * Add modified tracing middleware test * Fix opentracing implementation tags Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
co-authored by
Marcus Efraimsson
parent
5b61273497
commit
30aa24a183
+58
-14
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -15,6 +16,9 @@ type testQuery struct {
|
||||
|
||||
func TestDispatch(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -23,7 +27,8 @@ func TestDispatch(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.NoError(t, err)
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, invoked, "expected handler to be called")
|
||||
@@ -31,14 +36,20 @@ func TestDispatch(t *testing.T) {
|
||||
|
||||
func TestDispatch_NoRegisteredHandler(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.Equal(t, err, ErrHandlerNotFound,
|
||||
"expected bus to return HandlerNotFound since no handler is registered")
|
||||
}
|
||||
|
||||
func TestDispatch_ContextHandler(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -47,7 +58,7 @@ func TestDispatch_ContextHandler(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, invoked, "expected handler to be called")
|
||||
@@ -55,6 +66,9 @@ func TestDispatch_ContextHandler(t *testing.T) {
|
||||
|
||||
func TestDispatchCtx(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -63,7 +77,7 @@ func TestDispatchCtx(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, invoked, "expected handler to be called")
|
||||
@@ -71,6 +85,9 @@ func TestDispatchCtx(t *testing.T) {
|
||||
|
||||
func TestDispatchCtx_NoContextHandler(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -79,7 +96,7 @@ func TestDispatchCtx_NoContextHandler(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.True(t, invoked, "expected handler to be called")
|
||||
@@ -87,14 +104,20 @@ func TestDispatchCtx_NoContextHandler(t *testing.T) {
|
||||
|
||||
func TestDispatchCtx_NoRegisteredHandler(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.Equal(t, err, ErrHandlerNotFound,
|
||||
"expected bus to return HandlerNotFound since no handler is registered")
|
||||
}
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
want := "hello from handler"
|
||||
|
||||
@@ -105,7 +128,7 @@ func TestQuery(t *testing.T) {
|
||||
|
||||
q := &testQuery{}
|
||||
|
||||
err := bus.Dispatch(context.Background(), q)
|
||||
err = bus.Dispatch(context.Background(), q)
|
||||
require.NoError(t, err, "unable to dispatch query")
|
||||
|
||||
require.Equal(t, want, q.Resp)
|
||||
@@ -113,17 +136,23 @@ func TestQuery(t *testing.T) {
|
||||
|
||||
func TestQuery_HandlerReturnsError(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
bus.AddHandler(func(ctx context.Context, query *testQuery) error {
|
||||
return errors.New("handler error")
|
||||
})
|
||||
|
||||
err := bus.Dispatch(context.Background(), &testQuery{})
|
||||
err = bus.Dispatch(context.Background(), &testQuery{})
|
||||
require.Error(t, err, "expected error but got none")
|
||||
}
|
||||
|
||||
func TestEventPublish(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -132,7 +161,7 @@ func TestEventPublish(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
|
||||
require.True(t, invoked)
|
||||
@@ -140,13 +169,19 @@ func TestEventPublish(t *testing.T) {
|
||||
|
||||
func TestEventPublish_NoRegisteredListener(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
}
|
||||
|
||||
func TestEventCtxPublishCtx(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -155,7 +190,7 @@ func TestEventCtxPublishCtx(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
|
||||
require.True(t, invoked)
|
||||
@@ -163,13 +198,19 @@ func TestEventCtxPublishCtx(t *testing.T) {
|
||||
|
||||
func TestEventPublishCtx_NoRegisteredListener(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
}
|
||||
|
||||
func TestEventPublishCtx(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -178,7 +219,7 @@ func TestEventPublishCtx(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
|
||||
require.True(t, invoked)
|
||||
@@ -186,6 +227,9 @@ func TestEventPublishCtx(t *testing.T) {
|
||||
|
||||
func TestEventCtxPublish(t *testing.T) {
|
||||
bus := New()
|
||||
tracer, err := tracing.InitializeTracerForTest()
|
||||
require.NoError(t, err)
|
||||
bus.tracer = tracer
|
||||
|
||||
var invoked bool
|
||||
|
||||
@@ -194,7 +238,7 @@ func TestEventCtxPublish(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
err = bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err, "unable to publish event")
|
||||
|
||||
require.True(t, invoked)
|
||||
|
||||
Reference in New Issue
Block a user