@@ -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.17 .0"
13
14
"go.opentelemetry.io/otel/trace"
14
15
15
16
"github.com/redis/go-redis/extra/rediscmd/v9"
@@ -24,6 +25,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
24
25
switch rdb := rdb .(type ) {
25
26
case * redis.Client :
26
27
opt := rdb .Options ()
28
+ host , portString , err := net .SplitHostPort (opt .Addr )
29
+ if err == nil {
30
+ opts = append (opts , WithAttributes (
31
+ semconv .NetPeerName (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 .NetPeerPort (port ),
38
+ ))
39
+ }
40
+ }
27
41
connString := formatDBConnString (opt .Network , opt .Addr )
28
42
rdb .AddHook (newTracingHook (connString , opts ... ))
29
43
return nil
@@ -32,6 +46,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
32
46
33
47
rdb .OnNewNode (func (rdb * redis.Client ) {
34
48
opt := rdb .Options ()
49
+ host , portString , err := net .SplitHostPort (opt .Addr )
50
+ if err == nil {
51
+ opts = append (opts , WithAttributes (
52
+ semconv .NetPeerName (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 .NetPeerPort (port ),
59
+ ))
60
+ }
61
+ }
35
62
connString := formatDBConnString (opt .Network , opt .Addr )
36
63
rdb .AddHook (newTracingHook (connString , opts ... ))
37
64
})
@@ -41,6 +68,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
41
68
42
69
rdb .OnNewNode (func (rdb * redis.Client ) {
43
70
opt := rdb .Options ()
71
+ host , portString , err := net .SplitHostPort (opt .Addr )
72
+ if err == nil {
73
+ opts = append (opts , WithAttributes (
74
+ semconv .NetPeerName (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 .NetPeerPort (port ),
81
+ ))
82
+ }
83
+ }
44
84
connString := formatDBConnString (opt .Network , opt .Addr )
45
85
rdb .AddHook (newTracingHook (connString , opts ... ))
46
86
})
@@ -72,7 +112,7 @@ func newTracingHook(connString string, opts ...TracingOption) *tracingHook {
72
112
)
73
113
}
74
114
if connString != "" {
75
- conf .attrs = append (conf .attrs , semconv .DBConnectionStringKey . String (connString ))
115
+ conf .attrs = append (conf .attrs , semconv .DBConnectionString (connString ))
76
116
}
77
117
78
118
return & tracingHook {
@@ -113,14 +153,14 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
113
153
114
154
attrs := make ([]attribute.KeyValue , 0 , 8 )
115
155
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 ),
119
159
)
120
160
121
161
if th .conf .dbStmtEnabled {
122
162
cmdString := rediscmd .CmdString (cmd )
123
- attrs = append (attrs , semconv .DBStatementKey . String (cmdString ))
163
+ attrs = append (attrs , semconv .DBStatement (cmdString ))
124
164
}
125
165
126
166
opts := th .spanOpts
@@ -149,15 +189,15 @@ func (th *tracingHook) ProcessPipelineHook(
149
189
150
190
attrs := make ([]attribute.KeyValue , 0 , 8 )
151
191
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 ),
155
195
attribute .Int ("db.redis.num_cmd" , len (cmds )),
156
196
)
157
197
158
198
summary , cmdsString := rediscmd .CmdsString (cmds )
159
199
if th .conf .dbStmtEnabled {
160
- attrs = append (attrs , semconv .DBStatementKey . String (cmdsString ))
200
+ attrs = append (attrs , semconv .DBStatement (cmdsString ))
161
201
}
162
202
163
203
opts := th .spanOpts
0 commit comments