feat(redisotel): add code attributes

This commit is contained in:
Vladimir Mihailenco 2022-12-06 11:49:56 +02:00
parent 4bda6ec2fb
commit 3892986f01
2 changed files with 55 additions and 9 deletions

View File

@ -18,7 +18,7 @@ services:
- '9000:9000' - '9000:9000'
uptrace: uptrace:
image: 'uptrace/uptrace:1.2.2' image: 'uptrace/uptrace:1.2.4'
#image: 'uptrace/uptrace-dev:latest' #image: 'uptrace/uptrace-dev:latest'
restart: on-failure restart: on-failure
volumes: volumes:

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"runtime"
"strings"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
@ -107,13 +109,23 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
return hook(ctx, cmd) return hook(ctx, cmd)
} }
opts := th.spanOpts fn, file, line := funcFileLine("github.com/go-redis/redis")
if th.conf.dbStmtEnabled {
opts = append(opts, trace.WithAttributes( attrs := make([]attribute.KeyValue, 0, 8)
semconv.DBStatementKey.String(rediscmd.CmdString(cmd))), attrs = append(attrs,
semconv.CodeFunctionKey.String(fn),
semconv.CodeFilepathKey.String(file),
semconv.CodeLineNumberKey.Int(line),
) )
if th.conf.dbStmtEnabled {
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...) ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
defer span.End() defer span.End()
@ -133,16 +145,24 @@ func (th *tracingHook) ProcessPipelineHook(
return hook(ctx, cmds) return hook(ctx, cmds)
} }
opts := th.spanOpts fn, file, line := funcFileLine("github.com/go-redis/redis")
opts = append(opts, trace.WithAttributes(
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)), attribute.Int("db.redis.num_cmd", len(cmds)),
)) )
summary, cmdsString := rediscmd.CmdsString(cmds) summary, cmdsString := rediscmd.CmdsString(cmds)
if th.conf.dbStmtEnabled { 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...) ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)
defer span.End() defer span.End()
@ -167,3 +187,29 @@ func formatDBConnString(network, addr string) string {
} }
return fmt.Sprintf("%s://%s", network, addr) 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
}