Skip to content

Commit

Permalink
feat(redisotel): add code attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Dec 6, 2022
1 parent 4bda6ec commit 3892986
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example/otel/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
- '9000:9000'

uptrace:
image: 'uptrace/uptrace:1.2.2'
image: 'uptrace/uptrace:1.2.4'
#image: 'uptrace/uptrace-dev:latest'
restart: on-failure
volumes:
Expand Down
62 changes: 54 additions & 8 deletions extra/redisotel/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net"
"runtime"
"strings"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -107,13 +109,23 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
return hook(ctx, cmd)
}

opts := th.spanOpts
fn, file, line := funcFileLine("github.com/go-redis/redis")

attrs := make([]attribute.KeyValue, 0, 8)
attrs = append(attrs,
semconv.CodeFunctionKey.String(fn),
semconv.CodeFilepathKey.String(file),
semconv.CodeLineNumberKey.Int(line),
)

if th.conf.dbStmtEnabled {
opts = append(opts, trace.WithAttributes(
semconv.DBStatementKey.String(rediscmd.CmdString(cmd))),
)
cmdString := rediscmd.CmdString(cmd)
attrs = append(attrs, semconv.DBStatementKey.String(cmdString))
}

opts := th.spanOpts
opts = append(opts, trace.WithAttributes(attrs...))

ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
defer span.End()

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

opts := th.spanOpts
opts = append(opts, trace.WithAttributes(
fn, file, line := funcFileLine("github.com/go-redis/redis")

attrs := make([]attribute.KeyValue, 0, 8)
attrs = append(attrs,
semconv.CodeFunctionKey.String(fn),
semconv.CodeFilepathKey.String(file),
semconv.CodeLineNumberKey.Int(line),
attribute.Int("db.redis.num_cmd", len(cmds)),
))
)

summary, cmdsString := rediscmd.CmdsString(cmds)
if th.conf.dbStmtEnabled {
opts = append(opts, trace.WithAttributes(semconv.DBStatementKey.String(cmdsString)))
attrs = append(attrs, semconv.DBStatementKey.String(cmdsString))
}

opts := th.spanOpts
opts = append(opts, trace.WithAttributes(attrs...))

ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)
defer span.End()

Expand All @@ -167,3 +187,29 @@ func formatDBConnString(network, addr string) string {
}
return fmt.Sprintf("%s://%s", network, addr)
}

func funcFileLine(pkg string) (string, string, int) {
const depth = 16
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
ff := runtime.CallersFrames(pcs[:n])

var fn, file string
var line int
for {
f, ok := ff.Next()
if !ok {
break
}
fn, file, line = f.Function, f.File, f.Line
if !strings.Contains(fn, pkg) {
break
}
}

if ind := strings.LastIndexByte(fn, '/'); ind != -1 {
fn = fn[ind+1:]
}

return fn, file, line
}

0 comments on commit 3892986

Please sign in to comment.