Skip to content

Commit 35adebd

Browse files
feat: port v9 WithDBStatement into v8
1 parent 52af8ba commit 35adebd

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

extra/redisotel/go.sum

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
4343
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
4444
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
4545
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
46-
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
4746
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
47+
github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
48+
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
4849
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
4950
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
5051
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
51-
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
5252
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
53+
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
54+
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
5355
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5456
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5557
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -74,8 +76,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
7476
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
7577
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
7678
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
77-
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
7879
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
80+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
81+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
7982
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8083
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8184
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -92,13 +95,16 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
9295
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9396
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9497
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
98+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9599
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
96100
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
97101
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
102+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
98103
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
99104
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
100-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
101105
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
106+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
107+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
102108
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
103109
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
104110
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=

extra/redisotel/redisotel.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const (
1818
)
1919

2020
type TracingHook struct {
21-
tracer trace.Tracer
22-
attrs []attribute.KeyValue
21+
tracer trace.Tracer
22+
attrs []attribute.KeyValue
23+
dbStmtEnabled bool
2324
}
2425

2526
func NewTracingHook(opts ...Option) *TracingHook {
@@ -28,6 +29,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
2829
attrs: []attribute.KeyValue{
2930
semconv.DBSystemRedis,
3031
},
32+
dbStmtEnabled: true,
3133
}
3234
for _, opt := range opts {
3335
opt.apply(cfg)
@@ -37,7 +39,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
3739
defaultTracerName,
3840
trace.WithInstrumentationVersion("semver:"+redis.Version()),
3941
)
40-
return &TracingHook{tracer: tracer, attrs: cfg.attrs}
42+
return &TracingHook{tracer: tracer, attrs: cfg.attrs, dbStmtEnabled: cfg.dbStmtEnabled}
4143
}
4244

4345
func (th *TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
@@ -48,9 +50,12 @@ func (th *TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (cont
4850
opts := []trace.SpanStartOption{
4951
trace.WithSpanKind(trace.SpanKindClient),
5052
trace.WithAttributes(th.attrs...),
51-
trace.WithAttributes(
53+
}
54+
55+
if th.dbStmtEnabled {
56+
opts = append(opts, trace.WithAttributes(
5257
semconv.DBStatementKey.String(rediscmd.CmdString(cmd)),
53-
),
58+
))
5459
}
5560

5661
ctx, _ = th.tracer.Start(ctx, cmd.FullName(), opts...)
@@ -78,11 +83,16 @@ func (th *TracingHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.C
7883
trace.WithSpanKind(trace.SpanKindClient),
7984
trace.WithAttributes(th.attrs...),
8085
trace.WithAttributes(
81-
semconv.DBStatementKey.String(cmdsString),
8286
attribute.Int("db.redis.num_cmd", len(cmds)),
8387
),
8488
}
8589

90+
if th.dbStmtEnabled {
91+
opts = append(opts, trace.WithAttributes(
92+
semconv.DBStatementKey.String(cmdsString),
93+
))
94+
}
95+
8696
ctx, _ = th.tracer.Start(ctx, "pipeline "+summary, opts...)
8797

8898
return ctx, nil
@@ -105,8 +115,9 @@ func recordError(ctx context.Context, span trace.Span, err error) {
105115
}
106116

107117
type config struct {
108-
tp trace.TracerProvider
109-
attrs []attribute.KeyValue
118+
tp trace.TracerProvider
119+
attrs []attribute.KeyValue
120+
dbStmtEnabled bool
110121
}
111122

112123
// Option specifies instrumentation configuration options.
@@ -136,3 +147,10 @@ func WithAttributes(attrs ...attribute.KeyValue) Option {
136147
cfg.attrs = append(cfg.attrs, attrs...)
137148
})
138149
}
150+
151+
// WithDBStatement tells the tracing hook not to log raw redis commands.
152+
func WithDBStatement(on bool) Option {
153+
return optionFunc(func(conf *config) {
154+
conf.dbStmtEnabled = on
155+
})
156+
}

extra/redisotel/redisotel_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package redisotel_test
22

33
import (
44
"context"
5-
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
65
"testing"
76

7+
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
8+
89
"github.com/go-redis/redis/extra/redisotel/v8"
910
"github.com/go-redis/redis/v8"
1011
"go.opentelemetry.io/otel"
@@ -62,7 +63,41 @@ func TestNewWithAttributes(t *testing.T) {
6263
if !(attrs[1] == semconv.NetPeerNameKey.String("localhost")) {
6364
t.Fatalf("expected attrs[1] to be semconv.NetPeerNameKey.String(\"localhost\"), got: %v", attrs[1])
6465
}
66+
6567
if !(attrs[2] == semconv.DBStatementKey.String("ping")) {
6668
t.Fatalf("expected attrs[2] to be semconv.DBStatementKey.String(\"ping\"), got: %v", attrs[2])
6769
}
6870
}
71+
72+
func TestNewWithAttributesWithoutDBStatement(t *testing.T) {
73+
provider := sdktrace.NewTracerProvider()
74+
hook := redisotel.NewTracingHook(
75+
redisotel.WithTracerProvider(provider),
76+
redisotel.WithAttributes(semconv.NetPeerNameKey.String("localhost")),
77+
redisotel.WithDBStatement(false),
78+
)
79+
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
80+
cmd := redis.NewCmd(ctx, "ping")
81+
defer span.End()
82+
83+
ctx, err := hook.BeforeProcess(ctx, cmd)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
err = hook.AfterProcess(ctx, cmd)
88+
if err != nil {
89+
t.Fatal(err)
90+
}
91+
92+
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
93+
if !(attrs[0] == semconv.DBSystemRedis) {
94+
t.Fatalf("expected attrs[0] to be semconv.DBSystemRedis, got: %v", attrs[0])
95+
}
96+
if !(attrs[1] == semconv.NetPeerNameKey.String("localhost")) {
97+
t.Fatalf("expected attrs[1] to be semconv.NetPeerNameKey.String(\"localhost\"), got: %v", attrs[1])
98+
}
99+
100+
if len(attrs) == 3 && (attrs[2] == semconv.DBStatementKey.String("ping")) {
101+
t.Fatalf("did not expect attrs[2] to be semconv.DBStatementKey.String(\"ping\"), got: %v", attrs[2])
102+
}
103+
}

0 commit comments

Comments
 (0)