4
4
"context"
5
5
"fmt"
6
6
"net"
7
+ "runtime"
8
+ "strings"
7
9
8
10
"go.opentelemetry.io/otel/attribute"
9
11
"go.opentelemetry.io/otel/codes"
@@ -107,13 +109,23 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
107
109
return hook (ctx , cmd )
108
110
}
109
111
110
- opts := th .spanOpts
112
+ fn , file , line := funcFileLine ("github.com/go-redis/redis" )
113
+
114
+ attrs := make ([]attribute.KeyValue , 0 , 8 )
115
+ attrs = append (attrs ,
116
+ semconv .CodeFunctionKey .String (fn ),
117
+ semconv .CodeFilepathKey .String (file ),
118
+ semconv .CodeLineNumberKey .Int (line ),
119
+ )
120
+
111
121
if th .conf .dbStmtEnabled {
112
- opts = append (opts , trace .WithAttributes (
113
- semconv .DBStatementKey .String (rediscmd .CmdString (cmd ))),
114
- )
122
+ cmdString := rediscmd .CmdString (cmd )
123
+ attrs = append (attrs , semconv .DBStatementKey .String (cmdString ))
115
124
}
116
125
126
+ opts := th .spanOpts
127
+ opts = append (opts , trace .WithAttributes (attrs ... ))
128
+
117
129
ctx , span := th .conf .tracer .Start (ctx , cmd .FullName (), opts ... )
118
130
defer span .End ()
119
131
@@ -133,16 +145,24 @@ func (th *tracingHook) ProcessPipelineHook(
133
145
return hook (ctx , cmds )
134
146
}
135
147
136
- opts := th .spanOpts
137
- opts = append (opts , trace .WithAttributes (
148
+ fn , file , line := funcFileLine ("github.com/go-redis/redis" )
149
+
150
+ attrs := make ([]attribute.KeyValue , 0 , 8 )
151
+ attrs = append (attrs ,
152
+ semconv .CodeFunctionKey .String (fn ),
153
+ semconv .CodeFilepathKey .String (file ),
154
+ semconv .CodeLineNumberKey .Int (line ),
138
155
attribute .Int ("db.redis.num_cmd" , len (cmds )),
139
- ))
156
+ )
140
157
141
158
summary , cmdsString := rediscmd .CmdsString (cmds )
142
159
if th .conf .dbStmtEnabled {
143
- opts = append (opts , trace . WithAttributes ( semconv .DBStatementKey .String (cmdsString ) ))
160
+ attrs = append (attrs , semconv .DBStatementKey .String (cmdsString ))
144
161
}
145
162
163
+ opts := th .spanOpts
164
+ opts = append (opts , trace .WithAttributes (attrs ... ))
165
+
146
166
ctx , span := th .conf .tracer .Start (ctx , "redis.pipeline " + summary , opts ... )
147
167
defer span .End ()
148
168
@@ -167,3 +187,29 @@ func formatDBConnString(network, addr string) string {
167
187
}
168
188
return fmt .Sprintf ("%s://%s" , network , addr )
169
189
}
190
+
191
+ func funcFileLine (pkg string ) (string , string , int ) {
192
+ const depth = 16
193
+ var pcs [depth ]uintptr
194
+ n := runtime .Callers (3 , pcs [:])
195
+ ff := runtime .CallersFrames (pcs [:n ])
196
+
197
+ var fn , file string
198
+ var line int
199
+ for {
200
+ f , ok := ff .Next ()
201
+ if ! ok {
202
+ break
203
+ }
204
+ fn , file , line = f .Function , f .File , f .Line
205
+ if ! strings .Contains (fn , pkg ) {
206
+ break
207
+ }
208
+ }
209
+
210
+ if ind := strings .LastIndexByte (fn , '/' ); ind != - 1 {
211
+ fn = fn [ind + 1 :]
212
+ }
213
+
214
+ return fn , file , line
215
+ }
0 commit comments