mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 13:39:19 -06:00
30aa24a183
* 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>
246 lines
5.5 KiB
Go
246 lines
5.5 KiB
Go
package bus
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type testQuery struct {
|
|
ID int64
|
|
Resp string
|
|
}
|
|
|
|
func TestDispatch(t *testing.T) {
|
|
bus := New()
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
require.NoError(t, err)
|
|
bus.tracer = tracer
|
|
|
|
var invoked bool
|
|
|
|
bus.AddHandler(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
err = bus.Dispatch(context.Background(), &testQuery{})
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
}
|
|
|
|
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{})
|
|
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
|
|
|
|
bus.AddHandler(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Dispatch(context.Background(), &testQuery{})
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
}
|
|
|
|
func TestDispatchCtx(t *testing.T) {
|
|
bus := New()
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
require.NoError(t, err)
|
|
bus.tracer = tracer
|
|
|
|
var invoked bool
|
|
|
|
bus.AddHandler(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Dispatch(context.Background(), &testQuery{})
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
}
|
|
|
|
func TestDispatchCtx_NoContextHandler(t *testing.T) {
|
|
bus := New()
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
require.NoError(t, err)
|
|
bus.tracer = tracer
|
|
|
|
var invoked bool
|
|
|
|
bus.AddHandler(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Dispatch(context.Background(), &testQuery{})
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
}
|
|
|
|
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{})
|
|
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"
|
|
|
|
bus.AddHandler(func(ctx context.Context, q *testQuery) error {
|
|
q.Resp = want
|
|
return nil
|
|
})
|
|
|
|
q := &testQuery{}
|
|
|
|
err = bus.Dispatch(context.Background(), q)
|
|
require.NoError(t, err, "unable to dispatch query")
|
|
|
|
require.Equal(t, want, q.Resp)
|
|
}
|
|
|
|
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{})
|
|
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
|
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Publish(context.Background(), &testQuery{})
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
require.True(t, invoked)
|
|
}
|
|
|
|
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{})
|
|
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
|
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Publish(context.Background(), &testQuery{})
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
require.True(t, invoked)
|
|
}
|
|
|
|
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{})
|
|
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
|
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Publish(context.Background(), &testQuery{})
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
require.True(t, invoked)
|
|
}
|
|
|
|
func TestEventCtxPublish(t *testing.T) {
|
|
bus := New()
|
|
tracer, err := tracing.InitializeTracerForTest()
|
|
require.NoError(t, err)
|
|
bus.tracer = tracer
|
|
|
|
var invoked bool
|
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
|
invoked = true
|
|
return nil
|
|
})
|
|
|
|
err = bus.Publish(context.Background(), &testQuery{})
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
require.True(t, invoked)
|
|
}
|