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
co-authored by Marcus Efraimsson
parent 5b61273497
commit 30aa24a183
62 changed files with 717 additions and 474 deletions
+14 -10
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
}