diff --git a/extra/redisotel/config.go b/extra/redisotel/config.go index c02ee0b31..6ebd4bd56 100644 --- a/extra/redisotel/config.go +++ b/extra/redisotel/config.go @@ -20,6 +20,7 @@ type config struct { tracer trace.Tracer dbStmtEnabled bool + callerEnabled bool // Metrics options. @@ -57,6 +58,7 @@ func newConfig(opts ...baseOption) *config { tp: otel.GetTracerProvider(), mp: otel.GetMeterProvider(), dbStmtEnabled: true, + callerEnabled: true, } for _, opt := range opts { @@ -106,13 +108,20 @@ func WithTracerProvider(provider trace.TracerProvider) TracingOption { }) } -// WithDBStatement tells the tracing hook not to log raw redis commands. +// WithDBStatement tells the tracing hook to log raw redis commands. func WithDBStatement(on bool) TracingOption { return tracingOption(func(conf *config) { conf.dbStmtEnabled = on }) } +// WithCallerEnabled tells the tracing hook to log the calling function, file and line. +func WithCallerEnabled(on bool) TracingOption { + return tracingOption(func(conf *config) { + conf.callerEnabled = on + }) +} + //------------------------------------------------------------------------------ type MetricsOption interface { diff --git a/extra/redisotel/tracing.go b/extra/redisotel/tracing.go index 33b7abac1..40df5a202 100644 --- a/extra/redisotel/tracing.go +++ b/extra/redisotel/tracing.go @@ -101,14 +101,16 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook { func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook { return func(ctx context.Context, cmd redis.Cmder) error { - fn, file, line := funcFileLine("github.com/redis/go-redis") attrs := make([]attribute.KeyValue, 0, 8) - attrs = append(attrs, - semconv.CodeFunction(fn), - semconv.CodeFilepath(file), - semconv.CodeLineNumber(line), - ) + if th.conf.callerEnabled { + fn, file, line := funcFileLine("github.com/redis/go-redis") + attrs = append(attrs, + semconv.CodeFunction(fn), + semconv.CodeFilepath(file), + semconv.CodeLineNumber(line), + ) + } if th.conf.dbStmtEnabled { cmdString := rediscmd.CmdString(cmd) @@ -133,16 +135,20 @@ func (th *tracingHook) ProcessPipelineHook( hook redis.ProcessPipelineHook, ) redis.ProcessPipelineHook { return func(ctx context.Context, cmds []redis.Cmder) error { - fn, file, line := funcFileLine("github.com/redis/go-redis") - attrs := make([]attribute.KeyValue, 0, 8) attrs = append(attrs, - semconv.CodeFunction(fn), - semconv.CodeFilepath(file), - semconv.CodeLineNumber(line), attribute.Int("db.redis.num_cmd", len(cmds)), ) + if th.conf.callerEnabled { + fn, file, line := funcFileLine("github.com/redis/go-redis") + attrs = append(attrs, + semconv.CodeFunction(fn), + semconv.CodeFilepath(file), + semconv.CodeLineNumber(line), + ) + } + summary, cmdsString := rediscmd.CmdsString(cmds) if th.conf.dbStmtEnabled { attrs = append(attrs, semconv.DBStatement(cmdsString)) diff --git a/extra/redisotel/tracing_test.go b/extra/redisotel/tracing_test.go index e5ef86edc..a3e3ccc62 100644 --- a/extra/redisotel/tracing_test.go +++ b/extra/redisotel/tracing_test.go @@ -66,6 +66,35 @@ func TestWithDBStatement(t *testing.T) { } } +func TestWithoutCaller(t *testing.T) { + provider := sdktrace.NewTracerProvider() + hook := newTracingHook( + "", + WithTracerProvider(provider), + WithCallerEnabled(false), + ) + 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 { + attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes() + for _, attr := range attrs { + switch attr.Key { + case semconv.CodeFunctionKey, + semconv.CodeFilepathKey, + semconv.CodeLineNumberKey: + t.Fatalf("Attribute with %s statement should not exist", attr.Key) + } + } + return nil + }) + err := processHook(ctx, cmd) + if err != nil { + t.Fatal(err) + } +} + func TestTracingHook_DialHook(t *testing.T) { imsb := tracetest.NewInMemoryExporter() provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb))