2022-10-11 15:37:34 +03:00
|
|
|
package redisotel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-12-06 12:49:56 +03:00
|
|
|
"runtime"
|
2024-03-27 09:56:11 +03:00
|
|
|
"strconv"
|
2022-12-06 12:49:56 +03:00
|
|
|
"strings"
|
2022-10-11 15:37:34 +03:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
2024-03-27 09:56:11 +03:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
|
2022-10-11 15:37:34 +03:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/extra/rediscmd/v9"
|
|
|
|
"github.com/redis/go-redis/v9"
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-23 09:48:54 +03:00
|
|
|
instrumName = "github.com/redis/go-redis/extra/redisotel"
|
2022-10-11 15:37:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error {
|
|
|
|
switch rdb := rdb.(type) {
|
|
|
|
case *redis.Client:
|
|
|
|
opt := rdb.Options()
|
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
2024-03-27 09:56:11 +03:00
|
|
|
opts = addServerAttributes(opts, opt.Addr)
|
2022-10-11 15:37:34 +03:00
|
|
|
rdb.AddHook(newTracingHook(connString, opts...))
|
|
|
|
return nil
|
|
|
|
case *redis.ClusterClient:
|
|
|
|
rdb.AddHook(newTracingHook("", opts...))
|
|
|
|
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
|
|
|
opt := rdb.Options()
|
2024-03-27 09:56:11 +03:00
|
|
|
opts = addServerAttributes(opts, opt.Addr)
|
2022-10-11 15:37:34 +03:00
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
|
|
|
rdb.AddHook(newTracingHook(connString, opts...))
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
case *redis.Ring:
|
|
|
|
rdb.AddHook(newTracingHook("", opts...))
|
|
|
|
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
|
|
|
opt := rdb.Options()
|
2024-03-27 09:56:11 +03:00
|
|
|
opts = addServerAttributes(opts, opt.Addr)
|
2022-10-11 15:37:34 +03:00
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
|
|
|
rdb.AddHook(newTracingHook(connString, opts...))
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("redisotel: %T not supported", rdb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tracingHook struct {
|
|
|
|
conf *config
|
|
|
|
|
|
|
|
spanOpts []trace.SpanStartOption
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ redis.Hook = (*tracingHook)(nil)
|
|
|
|
|
|
|
|
func newTracingHook(connString string, opts ...TracingOption) *tracingHook {
|
|
|
|
baseOpts := make([]baseOption, len(opts))
|
|
|
|
for i, opt := range opts {
|
|
|
|
baseOpts[i] = opt
|
|
|
|
}
|
|
|
|
conf := newConfig(baseOpts...)
|
|
|
|
|
|
|
|
if conf.tracer == nil {
|
|
|
|
conf.tracer = conf.tp.Tracer(
|
|
|
|
instrumName,
|
|
|
|
trace.WithInstrumentationVersion("semver:"+redis.Version()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if connString != "" {
|
2024-03-27 09:56:11 +03:00
|
|
|
conf.attrs = append(conf.attrs, semconv.DBConnectionString(connString))
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &tracingHook{
|
|
|
|
conf: conf,
|
|
|
|
|
|
|
|
spanOpts: []trace.SpanStartOption{
|
|
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
|
|
|
trace.WithAttributes(conf.attrs...),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2022-10-12 15:00:06 +03:00
|
|
|
ctx, span := th.conf.tracer.Start(ctx, "redis.dial", th.spanOpts...)
|
2022-10-11 15:37:34 +03:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
conn, err := hook(ctx, network, addr)
|
|
|
|
if err != nil {
|
2022-11-21 12:31:38 +03:00
|
|
|
recordError(span, err)
|
2022-10-11 15:37:34 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
2023-01-23 09:48:54 +03:00
|
|
|
fn, file, line := funcFileLine("github.com/redis/go-redis")
|
2022-12-06 12:49:56 +03:00
|
|
|
|
|
|
|
attrs := make([]attribute.KeyValue, 0, 8)
|
|
|
|
attrs = append(attrs,
|
2024-03-27 09:56:11 +03:00
|
|
|
semconv.CodeFunction(fn),
|
|
|
|
semconv.CodeFilepath(file),
|
|
|
|
semconv.CodeLineNumber(line),
|
2022-12-06 12:49:56 +03:00
|
|
|
)
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
if th.conf.dbStmtEnabled {
|
2022-12-06 12:49:56 +03:00
|
|
|
cmdString := rediscmd.CmdString(cmd)
|
2024-03-27 09:56:11 +03:00
|
|
|
attrs = append(attrs, semconv.DBStatement(cmdString))
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:49:56 +03:00
|
|
|
opts := th.spanOpts
|
|
|
|
opts = append(opts, trace.WithAttributes(attrs...))
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if err := hook(ctx, cmd); err != nil {
|
2022-11-21 12:31:38 +03:00
|
|
|
recordError(span, err)
|
2022-10-11 15:37:34 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *tracingHook) ProcessPipelineHook(
|
|
|
|
hook redis.ProcessPipelineHook,
|
|
|
|
) redis.ProcessPipelineHook {
|
|
|
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
2023-01-23 09:48:54 +03:00
|
|
|
fn, file, line := funcFileLine("github.com/redis/go-redis")
|
2022-12-06 12:49:56 +03:00
|
|
|
|
|
|
|
attrs := make([]attribute.KeyValue, 0, 8)
|
|
|
|
attrs = append(attrs,
|
2024-03-27 09:56:11 +03:00
|
|
|
semconv.CodeFunction(fn),
|
|
|
|
semconv.CodeFilepath(file),
|
|
|
|
semconv.CodeLineNumber(line),
|
2022-10-11 15:37:34 +03:00
|
|
|
attribute.Int("db.redis.num_cmd", len(cmds)),
|
2022-12-06 12:49:56 +03:00
|
|
|
)
|
2022-10-11 15:37:34 +03:00
|
|
|
|
|
|
|
summary, cmdsString := rediscmd.CmdsString(cmds)
|
|
|
|
if th.conf.dbStmtEnabled {
|
2024-03-27 09:56:11 +03:00
|
|
|
attrs = append(attrs, semconv.DBStatement(cmdsString))
|
2022-10-11 15:37:34 +03:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:49:56 +03:00
|
|
|
opts := th.spanOpts
|
|
|
|
opts = append(opts, trace.WithAttributes(attrs...))
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if err := hook(ctx, cmds); err != nil {
|
2022-11-21 12:31:38 +03:00
|
|
|
recordError(span, err)
|
2022-10-11 15:37:34 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 12:31:38 +03:00
|
|
|
func recordError(span trace.Span, err error) {
|
2022-10-11 15:37:34 +03:00
|
|
|
if err != redis.Nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatDBConnString(network, addr string) string {
|
|
|
|
if network == "tcp" {
|
|
|
|
network = "redis"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s://%s", network, addr)
|
|
|
|
}
|
2022-12-06 12:49:56 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-03-27 09:56:11 +03:00
|
|
|
|
|
|
|
// Database span attributes semantic conventions recommended server address and port
|
|
|
|
// https://opentelemetry.io/docs/specs/semconv/database/database-spans/#connection-level-attributes
|
|
|
|
func addServerAttributes(opts []TracingOption, addr string) []TracingOption {
|
|
|
|
host, portString, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, WithAttributes(
|
|
|
|
semconv.ServerAddress(host),
|
|
|
|
))
|
|
|
|
|
|
|
|
// Parse the port string to an integer
|
|
|
|
port, err := strconv.Atoi(portString)
|
|
|
|
if err != nil {
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, WithAttributes(
|
|
|
|
semconv.ServerPort(port),
|
|
|
|
))
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|