Skip to content

Commit 3892986

Browse files
committed
feat(redisotel): add code attributes
1 parent 4bda6ec commit 3892986

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

example/otel/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- '9000:9000'
1919

2020
uptrace:
21-
image: 'uptrace/uptrace:1.2.2'
21+
image: 'uptrace/uptrace:1.2.4'
2222
#image: 'uptrace/uptrace-dev:latest'
2323
restart: on-failure
2424
volumes:

extra/redisotel/tracing.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"runtime"
8+
"strings"
79

810
"go.opentelemetry.io/otel/attribute"
911
"go.opentelemetry.io/otel/codes"
@@ -107,13 +109,23 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
107109
return hook(ctx, cmd)
108110
}
109111

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+
111121
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))
115124
}
116125

126+
opts := th.spanOpts
127+
opts = append(opts, trace.WithAttributes(attrs...))
128+
117129
ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
118130
defer span.End()
119131

@@ -133,16 +145,24 @@ func (th *tracingHook) ProcessPipelineHook(
133145
return hook(ctx, cmds)
134146
}
135147

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),
138155
attribute.Int("db.redis.num_cmd", len(cmds)),
139-
))
156+
)
140157

141158
summary, cmdsString := rediscmd.CmdsString(cmds)
142159
if th.conf.dbStmtEnabled {
143-
opts = append(opts, trace.WithAttributes(semconv.DBStatementKey.String(cmdsString)))
160+
attrs = append(attrs, semconv.DBStatementKey.String(cmdsString))
144161
}
145162

163+
opts := th.spanOpts
164+
opts = append(opts, trace.WithAttributes(attrs...))
165+
146166
ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)
147167
defer span.End()
148168

@@ -167,3 +187,29 @@ func formatDBConnString(network, addr string) string {
167187
}
168188
return fmt.Sprintf("%s://%s", network, addr)
169189
}
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

Comments
 (0)