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:
idafurjes
2022-01-20 11:10:12 +01:00
committed by GitHub
parent 5b61273497
commit 30aa24a183
62 changed files with 717 additions and 474 deletions

View File

@@ -7,8 +7,9 @@ import (
"reflect"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/setting"
"github.com/opentracing/opentracing-go"
"go.opentelemetry.io/otel/attribute"
)
// HandlerFunc defines a handler function interface.
@@ -55,9 +56,11 @@ type InProcBus struct {
listeners map[string][]HandlerFunc
listenersWithCtx map[string][]HandlerFunc
txMng TransactionManager
tracer tracing.Tracer
}
func ProvideBus() *InProcBus {
func ProvideBus(tracer tracing.Tracer) *InProcBus {
globalBus.tracer = tracer
return globalBus
}
@@ -71,7 +74,7 @@ var globalBus = New()
// New initialize the bus
func New() *InProcBus {
return &InProcBus{
bus := &InProcBus{
logger: log.New("bus"),
handlers: make(map[string]HandlerFunc),
handlersWithCtx: make(map[string]HandlerFunc),
@@ -79,6 +82,8 @@ func New() *InProcBus {
listenersWithCtx: make(map[string][]HandlerFunc),
txMng: &noopTransactionManager{},
}
bus.tracer = tracing.InitializeForBus()
return bus
}
// Want to get rid of global bus
@@ -95,10 +100,10 @@ func (b *InProcBus) SetTransactionManager(tm TransactionManager) {
func (b *InProcBus) Dispatch(ctx context.Context, msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
span, ctx := opentracing.StartSpanFromContext(ctx, "bus - "+msgName)
defer span.Finish()
ctx, span := b.tracer.Start(ctx, "bus - "+msgName)
defer span.End()
span.SetTag("msg", msgName)
span.SetAttributes("msg", msgName, attribute.Key("msg").String(msgName))
withCtx := true
var handler = b.handlersWithCtx[msgName]
@@ -148,11 +153,10 @@ func (b *InProcBus) Publish(ctx context.Context, msg Msg) error {
return err
}
}
_, span := b.tracer.Start(ctx, "bus - "+msgName)
defer span.End()
span, _ := opentracing.StartSpanFromContext(ctx, "bus - "+msgName)
defer span.Finish()
span.SetTag("msg", msgName)
span.SetAttributes("msg", msgName, attribute.Key("msg").String(msgName))
return nil
}

View File

@@ -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)