Skip to content

Commit 01d8581

Browse files
committed
Add custom attributes to tracing
1 parent d7f27f5 commit 01d8581

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

extra/redisotel/tracing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
116116
}
117117

118118
opts := th.spanOpts
119+
opts = append(opts, trace.WithAttributes(th.conf.attrsFunc(ctx)...))
119120
opts = append(opts, trace.WithAttributes(attrs...))
120121

121122
ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
@@ -149,6 +150,7 @@ func (th *tracingHook) ProcessPipelineHook(
149150
}
150151

151152
opts := th.spanOpts
153+
opts = append(opts, trace.WithAttributes(th.conf.attrsFunc(ctx)...))
152154
opts = append(opts, trace.WithAttributes(attrs...))
153155

154156
ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)

extra/redisotel/tracing_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,23 @@ func TestTracingHook_DialHook(t *testing.T) {
117117
}
118118
}
119119

120+
func customAttrFn(ctx context.Context) []attribute.KeyValue {
121+
122+
attributes := make([]attribute.KeyValue, 0)
123+
124+
if method, ok := ctx.Value(semconv.RPCMethodKey).(string); ok {
125+
attributes = append(attributes, semconv.RPCMethodKey.String(method))
126+
}
127+
128+
return attributes
129+
}
120130
func TestTracingHook_ProcessHook(t *testing.T) {
121131
imsb := tracetest.NewInMemoryExporter()
122132
provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb))
123133
hook := newTracingHook(
124134
"redis://localhost:6379",
125135
WithTracerProvider(provider),
136+
WithAttributesFunc(customAttrFn),
126137
)
127138

128139
tests := []struct {
@@ -141,7 +152,9 @@ func TestTracingHook_ProcessHook(t *testing.T) {
141152
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
142153
return tt.errTest
143154
})
144-
assertEqual(t, tt.errTest, processHook(context.Background(), cmd))
155+
156+
ctx := context.WithValue(context.Background(), semconv.RPCMethodKey, "ping")
157+
assertEqual(t, tt.errTest, processHook(ctx, cmd))
145158
assertEqual(t, 1, len(imsb.GetSpans()))
146159

147160
spanData := imsb.GetSpans()[0]
@@ -151,6 +164,8 @@ func TestTracingHook_ProcessHook(t *testing.T) {
151164
assertAttributeContains(t, spanData.Attributes, semconv.DBSystemRedis)
152165
assertAttributeContains(t, spanData.Attributes, semconv.DBConnectionStringKey.String("redis://localhost:6379"))
153166
assertAttributeContains(t, spanData.Attributes, semconv.DBStatementKey.String("ping"))
167+
// check for custom attribute
168+
assertAttributeContains(t, spanData.Attributes, semconv.RPCMethodKey.String("ping"))
154169

155170
if tt.errTest == nil {
156171
assertEqual(t, 0, len(spanData.Events))

0 commit comments

Comments
 (0)