@@ -5,11 +5,12 @@ import (
5
5
"fmt"
6
6
"net"
7
7
"runtime"
8
+ "strconv"
8
9
"strings"
9
10
10
11
"go.opentelemetry.io/otel/attribute"
11
12
"go.opentelemetry.io/otel/codes"
12
- semconv "go.opentelemetry.io/otel/semconv/v1.10 .0"
13
+ semconv "go.opentelemetry.io/otel/semconv/v1.24 .0"
13
14
"go.opentelemetry.io/otel/trace"
14
15
15
16
"github.com/redis/go-redis/extra/rediscmd/v9"
@@ -25,13 +26,15 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
25
26
case * redis.Client :
26
27
opt := rdb .Options ()
27
28
connString := formatDBConnString (opt .Network , opt .Addr )
29
+ opts = addServerAttributes (opts , opt .Addr )
28
30
rdb .AddHook (newTracingHook (connString , opts ... ))
29
31
return nil
30
32
case * redis.ClusterClient :
31
33
rdb .AddHook (newTracingHook ("" , opts ... ))
32
34
33
35
rdb .OnNewNode (func (rdb * redis.Client ) {
34
36
opt := rdb .Options ()
37
+ opts = addServerAttributes (opts , opt .Addr )
35
38
connString := formatDBConnString (opt .Network , opt .Addr )
36
39
rdb .AddHook (newTracingHook (connString , opts ... ))
37
40
})
@@ -41,6 +44,7 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
41
44
42
45
rdb .OnNewNode (func (rdb * redis.Client ) {
43
46
opt := rdb .Options ()
47
+ opts = addServerAttributes (opts , opt .Addr )
44
48
connString := formatDBConnString (opt .Network , opt .Addr )
45
49
rdb .AddHook (newTracingHook (connString , opts ... ))
46
50
})
@@ -72,7 +76,7 @@ func newTracingHook(connString string, opts ...TracingOption) *tracingHook {
72
76
)
73
77
}
74
78
if connString != "" {
75
- conf .attrs = append (conf .attrs , semconv .DBConnectionStringKey . String (connString ))
79
+ conf .attrs = append (conf .attrs , semconv .DBConnectionString (connString ))
76
80
}
77
81
78
82
return & tracingHook {
@@ -113,14 +117,14 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
113
117
114
118
attrs := make ([]attribute.KeyValue , 0 , 8 )
115
119
attrs = append (attrs ,
116
- semconv .CodeFunctionKey . String (fn ),
117
- semconv .CodeFilepathKey . String (file ),
118
- semconv .CodeLineNumberKey . Int (line ),
120
+ semconv .CodeFunction (fn ),
121
+ semconv .CodeFilepath (file ),
122
+ semconv .CodeLineNumber (line ),
119
123
)
120
124
121
125
if th .conf .dbStmtEnabled {
122
126
cmdString := rediscmd .CmdString (cmd )
123
- attrs = append (attrs , semconv .DBStatementKey . String (cmdString ))
127
+ attrs = append (attrs , semconv .DBStatement (cmdString ))
124
128
}
125
129
126
130
opts := th .spanOpts
@@ -149,15 +153,15 @@ func (th *tracingHook) ProcessPipelineHook(
149
153
150
154
attrs := make ([]attribute.KeyValue , 0 , 8 )
151
155
attrs = append (attrs ,
152
- semconv .CodeFunctionKey . String (fn ),
153
- semconv .CodeFilepathKey . String (file ),
154
- semconv .CodeLineNumberKey . Int (line ),
156
+ semconv .CodeFunction (fn ),
157
+ semconv .CodeFilepath (file ),
158
+ semconv .CodeLineNumber (line ),
155
159
attribute .Int ("db.redis.num_cmd" , len (cmds )),
156
160
)
157
161
158
162
summary , cmdsString := rediscmd .CmdsString (cmds )
159
163
if th .conf .dbStmtEnabled {
160
- attrs = append (attrs , semconv .DBStatementKey . String (cmdsString ))
164
+ attrs = append (attrs , semconv .DBStatement (cmdsString ))
161
165
}
162
166
163
167
opts := th .spanOpts
@@ -213,3 +217,28 @@ func funcFileLine(pkg string) (string, string, int) {
213
217
214
218
return fn , file , line
215
219
}
220
+
221
+ // Database span attributes semantic conventions recommended server address and port
222
+ // https://opentelemetry.io/docs/specs/semconv/database/database-spans/#connection-level-attributes
223
+ func addServerAttributes (opts []TracingOption , addr string ) []TracingOption {
224
+ host , portString , err := net .SplitHostPort (addr )
225
+ if err != nil {
226
+ return opts
227
+ }
228
+
229
+ opts = append (opts , WithAttributes (
230
+ semconv .ServerAddress (host ),
231
+ ))
232
+
233
+ // Parse the port string to an integer
234
+ port , err := strconv .Atoi (portString )
235
+ if err != nil {
236
+ return opts
237
+ }
238
+
239
+ opts = append (opts , WithAttributes (
240
+ semconv .ServerPort (port ),
241
+ ))
242
+
243
+ return opts
244
+ }
0 commit comments