Skip to content

Commit 9bc2aa5

Browse files
committed
add server address and port attributes to redis otel trace instrumentation
1 parent 2512123 commit 9bc2aa5

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

extra/redisotel/tracing.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"fmt"
66
"net"
77
"runtime"
8+
"strconv"
89
"strings"
910

1011
"go.opentelemetry.io/otel/attribute"
1112
"go.opentelemetry.io/otel/codes"
12-
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
13+
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
1314
"go.opentelemetry.io/otel/trace"
1415

1516
"github.com/redis/go-redis/extra/rediscmd/v9"
@@ -24,6 +25,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
2425
switch rdb := rdb.(type) {
2526
case *redis.Client:
2627
opt := rdb.Options()
28+
host, portString, err := net.SplitHostPort(opt.Addr)
29+
if err == nil {
30+
opts = append(opts, WithAttributes(
31+
semconv.ServerAddress(host),
32+
))
33+
// Parse the port string to an integer
34+
port, err := strconv.Atoi(portString)
35+
if err == nil {
36+
opts = append(opts, WithAttributes(
37+
semconv.ServerPort(port),
38+
))
39+
}
40+
}
2741
connString := formatDBConnString(opt.Network, opt.Addr)
2842
rdb.AddHook(newTracingHook(connString, opts...))
2943
return nil
@@ -32,6 +46,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
3246

3347
rdb.OnNewNode(func(rdb *redis.Client) {
3448
opt := rdb.Options()
49+
host, portString, err := net.SplitHostPort(opt.Addr)
50+
if err == nil {
51+
opts = append(opts, WithAttributes(
52+
semconv.ServerAddress(host),
53+
))
54+
// Parse the port string to an integer
55+
port, err := strconv.Atoi(portString)
56+
if err == nil {
57+
opts = append(opts, WithAttributes(
58+
semconv.ServerPort(port),
59+
))
60+
}
61+
}
3562
connString := formatDBConnString(opt.Network, opt.Addr)
3663
rdb.AddHook(newTracingHook(connString, opts...))
3764
})
@@ -41,6 +68,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
4168

4269
rdb.OnNewNode(func(rdb *redis.Client) {
4370
opt := rdb.Options()
71+
host, portString, err := net.SplitHostPort(opt.Addr)
72+
if err == nil {
73+
opts = append(opts, WithAttributes(
74+
semconv.ServerAddress(host),
75+
))
76+
// Parse the port string to an integer
77+
port, err := strconv.Atoi(portString)
78+
if err == nil {
79+
opts = append(opts, WithAttributes(
80+
semconv.ServerPort(port),
81+
))
82+
}
83+
}
4484
connString := formatDBConnString(opt.Network, opt.Addr)
4585
rdb.AddHook(newTracingHook(connString, opts...))
4686
})
@@ -72,7 +112,7 @@ func newTracingHook(connString string, opts ...TracingOption) *tracingHook {
72112
)
73113
}
74114
if connString != "" {
75-
conf.attrs = append(conf.attrs, semconv.DBConnectionStringKey.String(connString))
115+
conf.attrs = append(conf.attrs, semconv.DBConnectionString(connString))
76116
}
77117

78118
return &tracingHook{
@@ -113,14 +153,14 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
113153

114154
attrs := make([]attribute.KeyValue, 0, 8)
115155
attrs = append(attrs,
116-
semconv.CodeFunctionKey.String(fn),
117-
semconv.CodeFilepathKey.String(file),
118-
semconv.CodeLineNumberKey.Int(line),
156+
semconv.CodeFunction(fn),
157+
semconv.CodeFilepath(file),
158+
semconv.CodeLineNumber(line),
119159
)
120160

121161
if th.conf.dbStmtEnabled {
122162
cmdString := rediscmd.CmdString(cmd)
123-
attrs = append(attrs, semconv.DBStatementKey.String(cmdString))
163+
attrs = append(attrs, semconv.DBStatement(cmdString))
124164
}
125165

126166
opts := th.spanOpts
@@ -149,15 +189,15 @@ func (th *tracingHook) ProcessPipelineHook(
149189

150190
attrs := make([]attribute.KeyValue, 0, 8)
151191
attrs = append(attrs,
152-
semconv.CodeFunctionKey.String(fn),
153-
semconv.CodeFilepathKey.String(file),
154-
semconv.CodeLineNumberKey.Int(line),
192+
semconv.CodeFunction(fn),
193+
semconv.CodeFilepath(file),
194+
semconv.CodeLineNumber(line),
155195
attribute.Int("db.redis.num_cmd", len(cmds)),
156196
)
157197

158198
summary, cmdsString := rediscmd.CmdsString(cmds)
159199
if th.conf.dbStmtEnabled {
160-
attrs = append(attrs, semconv.DBStatementKey.String(cmdsString))
200+
attrs = append(attrs, semconv.DBStatement(cmdsString))
161201
}
162202

163203
opts := th.spanOpts

0 commit comments

Comments
 (0)