Skip to content

Commit

Permalink
add server address and port attributes to redis otel trace instrument…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
esara committed Jan 25, 2024
1 parent 2512123 commit 9bc2aa5
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 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.21.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.ServerAddress(host),
))
// Parse the port string to an integer
port, err := strconv.Atoi(portString)
if err == nil {
opts = append(opts, WithAttributes(
semconv.ServerPort(port),
))
}
}
connString := formatDBConnString(opt.Network, opt.Addr)
rdb.AddHook(newTracingHook(connString, opts...))
return nil
Expand All @@ -32,6 +46,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {

rdb.OnNewNode(func(rdb *redis.Client) {
opt := rdb.Options()
host, portString, err := net.SplitHostPort(opt.Addr)
if err == nil {
opts = append(opts, WithAttributes(
semconv.ServerAddress(host),
))
// Parse the port string to an integer
port, err := strconv.Atoi(portString)
if err == nil {
opts = append(opts, WithAttributes(
semconv.ServerPort(port),
))
}
}
connString := formatDBConnString(opt.Network, opt.Addr)
rdb.AddHook(newTracingHook(connString, opts...))
})
Expand All @@ -41,6 +68,19 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {

rdb.OnNewNode(func(rdb *redis.Client) {
opt := rdb.Options()
host, portString, err := net.SplitHostPort(opt.Addr)
if err == nil {
opts = append(opts, WithAttributes(
semconv.ServerAddress(host),
))
// Parse the port string to an integer
port, err := strconv.Atoi(portString)
if err == nil {
opts = append(opts, WithAttributes(
semconv.ServerPort(port),
))
}
}
connString := formatDBConnString(opt.Network, opt.Addr)
rdb.AddHook(newTracingHook(connString, opts...))
})
Expand Down Expand Up @@ -72,7 +112,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 +153,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 +189,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 9bc2aa5

Please sign in to comment.