Skip to content

Commit fa475cb

Browse files
feat(redisotel): add WithCallerEnabled option (#3415)
* feat(redisotel): add WithCaller option Allow the disabling the collection of the `code.function`, `code.filepath` and `code.lineno` tracing attributes. When setting `WithCaller(false)` overall performance is increased as the "expensive" `runtime.Callers` and `runtime.(*Frames).Next` calls are no longer needed. * chore(redisotel): improve docblock language * chore(redisotel): rename `WithCaller` to `WithCallerEnabled` --------- Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 05f42e2 commit fa475cb

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

extra/redisotel/config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type config struct {
2020
tracer trace.Tracer
2121

2222
dbStmtEnabled bool
23+
callerEnabled bool
2324

2425
// Metrics options.
2526

@@ -57,6 +58,7 @@ func newConfig(opts ...baseOption) *config {
5758
tp: otel.GetTracerProvider(),
5859
mp: otel.GetMeterProvider(),
5960
dbStmtEnabled: true,
61+
callerEnabled: true,
6062
}
6163

6264
for _, opt := range opts {
@@ -106,13 +108,20 @@ func WithTracerProvider(provider trace.TracerProvider) TracingOption {
106108
})
107109
}
108110

109-
// WithDBStatement tells the tracing hook not to log raw redis commands.
111+
// WithDBStatement tells the tracing hook to log raw redis commands.
110112
func WithDBStatement(on bool) TracingOption {
111113
return tracingOption(func(conf *config) {
112114
conf.dbStmtEnabled = on
113115
})
114116
}
115117

118+
// WithCallerEnabled tells the tracing hook to log the calling function, file and line.
119+
func WithCallerEnabled(on bool) TracingOption {
120+
return tracingOption(func(conf *config) {
121+
conf.callerEnabled = on
122+
})
123+
}
124+
116125
//------------------------------------------------------------------------------
117126

118127
type MetricsOption interface {

extra/redisotel/tracing.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,16 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
101101

102102
func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
103103
return func(ctx context.Context, cmd redis.Cmder) error {
104-
fn, file, line := funcFileLine("github.com/redis/go-redis")
105104

106105
attrs := make([]attribute.KeyValue, 0, 8)
107-
attrs = append(attrs,
108-
semconv.CodeFunction(fn),
109-
semconv.CodeFilepath(file),
110-
semconv.CodeLineNumber(line),
111-
)
106+
if th.conf.callerEnabled {
107+
fn, file, line := funcFileLine("github.com/redis/go-redis")
108+
attrs = append(attrs,
109+
semconv.CodeFunction(fn),
110+
semconv.CodeFilepath(file),
111+
semconv.CodeLineNumber(line),
112+
)
113+
}
112114

113115
if th.conf.dbStmtEnabled {
114116
cmdString := rediscmd.CmdString(cmd)
@@ -133,16 +135,20 @@ func (th *tracingHook) ProcessPipelineHook(
133135
hook redis.ProcessPipelineHook,
134136
) redis.ProcessPipelineHook {
135137
return func(ctx context.Context, cmds []redis.Cmder) error {
136-
fn, file, line := funcFileLine("github.com/redis/go-redis")
137-
138138
attrs := make([]attribute.KeyValue, 0, 8)
139139
attrs = append(attrs,
140-
semconv.CodeFunction(fn),
141-
semconv.CodeFilepath(file),
142-
semconv.CodeLineNumber(line),
143140
attribute.Int("db.redis.num_cmd", len(cmds)),
144141
)
145142

143+
if th.conf.callerEnabled {
144+
fn, file, line := funcFileLine("github.com/redis/go-redis")
145+
attrs = append(attrs,
146+
semconv.CodeFunction(fn),
147+
semconv.CodeFilepath(file),
148+
semconv.CodeLineNumber(line),
149+
)
150+
}
151+
146152
summary, cmdsString := rediscmd.CmdsString(cmds)
147153
if th.conf.dbStmtEnabled {
148154
attrs = append(attrs, semconv.DBStatement(cmdsString))

extra/redisotel/tracing_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ func TestWithDBStatement(t *testing.T) {
6666
}
6767
}
6868

69+
func TestWithoutCaller(t *testing.T) {
70+
provider := sdktrace.NewTracerProvider()
71+
hook := newTracingHook(
72+
"",
73+
WithTracerProvider(provider),
74+
WithCallerEnabled(false),
75+
)
76+
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
77+
cmd := redis.NewCmd(ctx, "ping")
78+
defer span.End()
79+
80+
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
81+
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
82+
for _, attr := range attrs {
83+
switch attr.Key {
84+
case semconv.CodeFunctionKey,
85+
semconv.CodeFilepathKey,
86+
semconv.CodeLineNumberKey:
87+
t.Fatalf("Attribute with %s statement should not exist", attr.Key)
88+
}
89+
}
90+
return nil
91+
})
92+
err := processHook(ctx, cmd)
93+
if err != nil {
94+
t.Fatal(err)
95+
}
96+
}
97+
6998
func TestTracingHook_DialHook(t *testing.T) {
7099
imsb := tracetest.NewInMemoryExporter()
71100
provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb))

0 commit comments

Comments
 (0)