2022-10-11 15:37:34 +03:00
|
|
|
package redisotel
|
2022-01-18 09:44:05 +03:00
|
|
|
|
|
|
|
import (
|
2022-02-22 15:56:31 +03:00
|
|
|
"context"
|
2022-01-18 09:44:05 +03:00
|
|
|
"testing"
|
|
|
|
|
2022-06-04 15:15:43 +03:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
"go.opentelemetry.io/otel"
|
2022-02-22 15:56:31 +03:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2022-01-18 09:44:05 +03:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-06-04 15:15:43 +03:00
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2022-01-18 09:44:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type providerFunc func(name string, opts ...trace.TracerOption) trace.Tracer
|
|
|
|
|
|
|
|
func (fn providerFunc) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
|
|
|
|
return fn(name, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithTracerProvider(t *testing.T) {
|
|
|
|
invoked := false
|
|
|
|
|
|
|
|
tp := providerFunc(func(name string, opts ...trace.TracerOption) trace.Tracer {
|
|
|
|
invoked = true
|
|
|
|
return otel.GetTracerProvider().Tracer(name, opts...)
|
|
|
|
})
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
_ = newTracingHook("", WithTracerProvider(tp))
|
2022-01-18 09:44:05 +03:00
|
|
|
|
|
|
|
if !invoked {
|
|
|
|
t.Fatalf("did not call custom TraceProvider")
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 15:56:31 +03:00
|
|
|
|
2022-08-26 05:07:47 +03:00
|
|
|
func TestWithDBStatement(t *testing.T) {
|
|
|
|
provider := sdktrace.NewTracerProvider()
|
2022-10-11 15:37:34 +03:00
|
|
|
hook := newTracingHook(
|
|
|
|
"",
|
|
|
|
WithTracerProvider(provider),
|
|
|
|
WithDBStatement(false),
|
2022-08-26 05:07:47 +03:00
|
|
|
)
|
|
|
|
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
|
|
|
|
cmd := redis.NewCmd(ctx, "ping")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
|
|
|
|
for _, attr := range attrs {
|
|
|
|
if attr.Key == semconv.DBStatementKey {
|
|
|
|
t.Fatal("Attribute with db statement should not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
err := processHook(ctx, cmd)
|
2022-08-26 05:07:47 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|