Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion extra/redisotel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type config struct {
tracer trace.Tracer

dbStmtEnabled bool
callerEnabled bool

// Metrics options.

Expand Down Expand Up @@ -57,6 +58,7 @@ func newConfig(opts ...baseOption) *config {
tp: otel.GetTracerProvider(),
mp: otel.GetMeterProvider(),
dbStmtEnabled: true,
callerEnabled: true,
}

for _, opt := range opts {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 17 additions & 11 deletions extra/redisotel/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
29 changes: 29 additions & 0 deletions extra/redisotel/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading