unify span names
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
aed9512b93
commit
44d8169631
72
tracer.go
72
tracer.go
@ -16,23 +16,20 @@ func setTracing(rdb redis.UniversalClient, tr tracer.Tracer, opts ...tracer.Span
|
|||||||
case *redis.Client:
|
case *redis.Client:
|
||||||
opt := rdb.Options()
|
opt := rdb.Options()
|
||||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
opts = addServerAttributes(opts, opt.Addr)
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
rdb.AddHook(newTracingHook(connString, tr, opts...))
|
|
||||||
case *redis.ClusterClient:
|
case *redis.ClusterClient:
|
||||||
rdb.AddHook(newTracingHook("", tr, opts...))
|
rdb.AddHook(newTracingHook("", tr, opts...))
|
||||||
rdb.OnNewNode(func(rdb *redis.Client) {
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
||||||
opt := rdb.Options()
|
opt := rdb.Options()
|
||||||
opts = addServerAttributes(opts, opt.Addr)
|
|
||||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
rdb.AddHook(newTracingHook(connString, tr, opts...))
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
})
|
})
|
||||||
case *redis.Ring:
|
case *redis.Ring:
|
||||||
rdb.AddHook(newTracingHook("", tr, opts...))
|
rdb.AddHook(newTracingHook("", tr, opts...))
|
||||||
rdb.OnNewNode(func(rdb *redis.Client) {
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
||||||
opt := rdb.Options()
|
opt := rdb.Options()
|
||||||
opts = addServerAttributes(opts, opt.Addr)
|
|
||||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||||
rdb.AddHook(newTracingHook(connString, tr, opts...))
|
rdb.AddHook(newTracingHook(connString, tr))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,15 +55,13 @@ func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOpti
|
|||||||
|
|
||||||
func (h *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
func (h *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
||||||
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
ctx, span := h.tr.Start(ctx, "redis.dial", h.opts...)
|
sctx, span := h.tr.Start(ctx, "redis.dial", h.opts...)
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
conn, err := hook(ctx, network, addr)
|
conn, err := hook(sctx, network, addr)
|
||||||
if err != nil {
|
recordError(span, err)
|
||||||
recordError(span, err)
|
|
||||||
return nil, err
|
return conn, err
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,41 +69,37 @@ func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|||||||
return func(ctx context.Context, cmd redis.Cmder) error {
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||||
cmdString := rediscmd.CmdString(cmd)
|
cmdString := rediscmd.CmdString(cmd)
|
||||||
|
|
||||||
ctx, span := h.tr.Start(ctx, cmd.FullName(), append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
sctx, span := h.tr.Start(ctx, "redis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
if err := hook(ctx, cmd); err != nil {
|
err := hook(sctx, cmd)
|
||||||
recordError(span, err)
|
recordError(span, err)
|
||||||
return err
|
|
||||||
}
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *tracingHook) ProcessPipelineHook(
|
func (h *tracingHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
||||||
hook redis.ProcessPipelineHook,
|
|
||||||
) redis.ProcessPipelineHook {
|
|
||||||
return func(ctx context.Context, cmds []redis.Cmder) error {
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
||||||
summary, cmdsString := rediscmd.CmdsString(cmds)
|
_, cmdsString := rediscmd.CmdsString(cmds)
|
||||||
|
|
||||||
opts := append(h.opts, tracer.WithSpanLabels(
|
opts := append(h.opts, tracer.WithSpanLabels(
|
||||||
"db.redis.num_cmd", strconv.Itoa(len(cmds)),
|
"db.redis.num_cmd", strconv.Itoa(len(cmds)),
|
||||||
"db.statement", cmdsString,
|
"db.statement", cmdsString,
|
||||||
))
|
))
|
||||||
|
|
||||||
ctx, span := h.tr.Start(ctx, "redis.pipeline "+summary, opts...)
|
sctx, span := h.tr.Start(ctx, "redis.process_pipeline", opts...)
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
if err := hook(ctx, cmds); err != nil {
|
err := hook(sctx, cmds)
|
||||||
recordError(span, err)
|
recordError(span, err)
|
||||||
return err
|
|
||||||
}
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recordError(span tracer.Span, err error) {
|
func recordError(span tracer.Span, err error) {
|
||||||
if err != redis.Nil {
|
if err != nil && err != redis.Nil {
|
||||||
span.SetStatus(tracer.SpanStatusError, err.Error())
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,24 +110,3 @@ func formatDBConnString(network, addr string) string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("%s://%s", network, addr)
|
return fmt.Sprintf("%s://%s", network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 []tracer.SpanOption, addr string) []tracer.SpanOption {
|
|
||||||
host, portString, err := net.SplitHostPort(addr)
|
|
||||||
if err != nil {
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = append(opts, tracer.WithSpanLabels("server.address", host))
|
|
||||||
|
|
||||||
// Parse the port string to an integer
|
|
||||||
port, err := strconv.Atoi(portString)
|
|
||||||
if err != nil {
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = append(opts, tracer.WithSpanLabels("server.port", port))
|
|
||||||
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user