Skip to content

Commit

Permalink
add net.peer attributes to redis otel trace instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
esara committed Dec 9, 2023
1 parent 21bd40a commit e9eee2c
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions extra/redisotel/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"net"
"runtime"
"strconv"
"strings"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"

"github.com/redis/go-redis/extra/rediscmd/v9"
Expand All @@ -24,6 +25,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
switch rdb := rdb.(type) {
case *redis.Client:
opt := rdb.Options()
host, portString, err := net.SplitHostPort(opt.Addr)
if err != nil {
opts = append(opts, WithAttributes(
semconv.NetPeerName(host),
))
// Parse the port string to an integer
port, err := strconv.Atoi(portString)
if err != nil {
opts = append(opts, WithAttributes(
semconv.NetPeerPort(port),
))
}
}
connString := formatDBConnString(opt.Network, opt.Addr)
rdb.AddHook(newTracingHook(connString, opts...))
return nil
Expand Down Expand Up @@ -72,7 +86,7 @@ func newTracingHook(connString string, opts ...TracingOption) *tracingHook {
)
}
if connString != "" {
conf.attrs = append(conf.attrs, semconv.DBConnectionStringKey.String(connString))
conf.attrs = append(conf.attrs, semconv.DBConnectionString(connString))
}

return &tracingHook{
Expand Down Expand Up @@ -113,14 +127,14 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {

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

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

opts := th.spanOpts
Expand Down Expand Up @@ -149,15 +163,15 @@ func (th *tracingHook) ProcessPipelineHook(

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

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

opts := th.spanOpts
Expand Down

0 comments on commit e9eee2c

Please sign in to comment.