67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
|
|
package otel
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.opentelemetry.io/otel/metric/noop"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
|
)
|
|
|
|
func TestInit(t *testing.T) {
|
|
cfg := &config.AppConfig{
|
|
Name: "test-app",
|
|
OTEL: &config.OTELConfig{
|
|
Enabled: true,
|
|
},
|
|
}
|
|
ctx := cfg.AddToCtx(context.Background())
|
|
|
|
ctx, shutdown := Init(ctx)
|
|
defer shutdown(context.Background())
|
|
|
|
assert.NotNil(t, ctx.Value(ctxKeyTracer))
|
|
assert.NotNil(t, ctx.Value(ctxKeyMeter))
|
|
}
|
|
|
|
func TestInit_Disabled(t *testing.T) {
|
|
cfg := &config.AppConfig{
|
|
Name: "test-app",
|
|
OTEL: &config.OTELConfig{
|
|
Enabled: false,
|
|
},
|
|
}
|
|
ctx := cfg.AddToCtx(context.Background())
|
|
|
|
ctx, shutdown := Init(ctx)
|
|
defer shutdown(context.Background())
|
|
|
|
assert.NotNil(t, ctx.Value(ctxKeyTracer))
|
|
assert.NotNil(t, ctx.Value(ctxKeyMeter))
|
|
}
|
|
|
|
func TestContextFuncs(t *testing.T) {
|
|
tracer := trace.NewNoopTracerProvider().Tracer("test")
|
|
meter := noop.NewMeterProvider().Meter("test")
|
|
|
|
ctx := context.Background()
|
|
ctx = AddTracerToCtx(ctx, tracer)
|
|
ctx = AddMeterToCtx(ctx, meter)
|
|
|
|
assert.Equal(t, tracer, MustTracerFromCtx(ctx))
|
|
assert.Equal(t, meter, MustMeterFromCtx(ctx))
|
|
}
|
|
|
|
func TestContextFuncs_Panic(t *testing.T) {
|
|
assert.Panics(t, func() {
|
|
MustTracerFromCtx(context.Background())
|
|
})
|
|
assert.Panics(t, func() {
|
|
MustMeterFromCtx(context.Background())
|
|
})
|
|
}
|