From 095fb1c1109471fd881bf81be2ed4568fdf87462 Mon Sep 17 00:00:00 2001 From: Rangel Reale Date: Tue, 14 Feb 2023 09:55:07 -0300 Subject: [PATCH 1/2] tracing option to customize span name --- extra/redisotel/config.go | 24 ++++++++++++++++++++---- extra/redisotel/tracing.go | 21 ++++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/extra/redisotel/config.go b/extra/redisotel/config.go index 5b558aa284..72f9fc9a03 100644 --- a/extra/redisotel/config.go +++ b/extra/redisotel/config.go @@ -12,8 +12,9 @@ import ( type config struct { // Common options. - dbSystem string - attrs []attribute.KeyValue + dbSystem string + attrs []attribute.KeyValue + spanNameFn func(hook TracingHook, defaultName string) string // Tracing options. @@ -82,7 +83,7 @@ func WithAttributes(attrs ...attribute.KeyValue) Option { }) } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ type TracingOption interface { baseOption @@ -114,7 +115,22 @@ func WithDBStatement(on bool) TracingOption { }) } -//------------------------------------------------------------------------------ +type TracingHook int + +const ( + TracingHookDial TracingHook = iota + TracingHookProcess + TracingHookProcessPipeline +) + +// WithSpanName provides a function to customize the span names. +func WithSpanName(f func(hook TracingHook, defaultName string) string) TracingOption { + return tracingOption(func(conf *config) { + conf.spanNameFn = f + }) +} + +// ------------------------------------------------------------------------------ type MetricsOption interface { baseOption diff --git a/extra/redisotel/tracing.go b/extra/redisotel/tracing.go index 0bbf692adf..3cdf058634 100644 --- a/extra/redisotel/tracing.go +++ b/extra/redisotel/tracing.go @@ -91,7 +91,12 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook { return hook(ctx, network, addr) } - ctx, span := th.conf.tracer.Start(ctx, "redis.dial", th.spanOpts...) + spanName := "redis.dial" + if th.conf.spanNameFn != nil { + spanName = th.conf.spanNameFn(TracingHookDial, spanName) + } + + ctx, span := th.conf.tracer.Start(ctx, spanName, th.spanOpts...) defer span.End() conn, err := hook(ctx, network, addr) @@ -126,7 +131,12 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook { opts := th.spanOpts opts = append(opts, trace.WithAttributes(attrs...)) - ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...) + spanName := cmd.FullName() + if th.conf.spanNameFn != nil { + spanName = th.conf.spanNameFn(TracingHookProcess, spanName) + } + + ctx, span := th.conf.tracer.Start(ctx, spanName, opts...) defer span.End() if err := hook(ctx, cmd); err != nil { @@ -163,7 +173,12 @@ func (th *tracingHook) ProcessPipelineHook( opts := th.spanOpts opts = append(opts, trace.WithAttributes(attrs...)) - ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...) + spanName := "redis.pipeline " + summary + if th.conf.spanNameFn != nil { + spanName = th.conf.spanNameFn(TracingHookProcessPipeline, spanName) + } + + ctx, span := th.conf.tracer.Start(ctx, spanName, opts...) defer span.End() if err := hook(ctx, cmds); err != nil { From 140a1d26d7ab3c7b5caad24f465a2c7b8373d00f Mon Sep 17 00:00:00 2001 From: Rangel Reale Date: Tue, 14 Feb 2023 10:04:31 -0300 Subject: [PATCH 2/2] otel test --- extra/redisotel/redisotel_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/extra/redisotel/redisotel_test.go b/extra/redisotel/redisotel_test.go index fd75361e8f..bb0fcee1ae 100644 --- a/extra/redisotel/redisotel_test.go +++ b/extra/redisotel/redisotel_test.go @@ -59,3 +59,32 @@ func TestWithDBStatement(t *testing.T) { t.Fatal(err) } } + +func TestWithSpanName(t *testing.T) { + provider := sdktrace.NewTracerProvider() + hook := newTracingHook( + "", + WithTracerProvider(provider), + WithSpanName(func(hook TracingHook, defaultName string) string { + if hook == TracingHookProcess { + return "redis." + defaultName + } + return defaultName + }), + ) + ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test") + cmd := redis.NewCmd(ctx, "ping") + defer span.End() + + processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error { + name := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Name() + if name != "redis.ping" { + t.Fatal("Span name was not changed by the function") + } + return nil + }) + err := processHook(ctx, cmd) + if err != nil { + t.Fatal(err) + } +}