2020-10-21 15:19:27 +03:00
|
|
|
package redisotel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-11-21 10:56:52 +03:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-03-04 04:43:10 +03:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-11-21 10:56:52 +03:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2022-01-18 09:44:05 +03:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
|
2020-11-21 10:56:52 +03:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-09-08 16:00:52 +03:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/extra/rediscmd/v8"
|
|
|
|
"github.com/go-redis/redis/v8"
|
2020-10-21 15:19:27 +03:00
|
|
|
)
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
const (
|
2022-02-02 10:26:14 +03:00
|
|
|
defaultTracerName = "github.com/go-redis/redis/extra/redisotel"
|
2022-01-18 09:44:05 +03:00
|
|
|
)
|
2020-10-21 15:19:27 +03:00
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
type TracingHook struct {
|
|
|
|
tracer trace.Tracer
|
2022-02-22 15:56:31 +03:00
|
|
|
attrs []attribute.KeyValue
|
2022-01-18 09:44:05 +03:00
|
|
|
}
|
2020-10-21 15:19:27 +03:00
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
func NewTracingHook(opts ...Option) *TracingHook {
|
|
|
|
cfg := &config{
|
|
|
|
tp: otel.GetTracerProvider(),
|
2022-02-22 15:56:31 +03:00
|
|
|
attrs: []attribute.KeyValue{
|
|
|
|
semconv.DBSystemRedis,
|
|
|
|
},
|
2022-01-18 09:44:05 +03:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.apply(cfg)
|
|
|
|
}
|
2021-04-16 15:00:10 +03:00
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
tracer := cfg.tp.Tracer(
|
|
|
|
defaultTracerName,
|
2022-02-02 10:26:14 +03:00
|
|
|
trace.WithInstrumentationVersion("semver:"+redis.Version()),
|
2022-01-18 09:44:05 +03:00
|
|
|
)
|
2022-02-22 15:56:31 +03:00
|
|
|
return &TracingHook{tracer: tracer, attrs: cfg.attrs}
|
2021-04-16 15:00:10 +03:00
|
|
|
}
|
2020-10-21 15:19:27 +03:00
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
func (th *TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
2020-10-21 15:19:27 +03:00
|
|
|
if !trace.SpanFromContext(ctx).IsRecording() {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:55:57 +03:00
|
|
|
opts := []trace.SpanStartOption{
|
|
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
2022-02-22 15:56:31 +03:00
|
|
|
trace.WithAttributes(th.attrs...),
|
2022-01-18 09:44:05 +03:00
|
|
|
trace.WithAttributes(
|
|
|
|
semconv.DBStatementKey.String(rediscmd.CmdString(cmd)),
|
|
|
|
),
|
2021-10-26 16:55:57 +03:00
|
|
|
}
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
ctx, _ = th.tracer.Start(ctx, cmd.FullName(), opts...)
|
2020-10-21 15:19:27 +03:00
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
func (th *TracingHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
|
2020-10-21 15:19:27 +03:00
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
recordError(ctx, span, err)
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
func (th *TracingHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
2020-10-21 15:19:27 +03:00
|
|
|
if !trace.SpanFromContext(ctx).IsRecording() {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, cmdsString := rediscmd.CmdsString(cmds)
|
|
|
|
|
2021-10-26 16:55:57 +03:00
|
|
|
opts := []trace.SpanStartOption{
|
|
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
2022-02-22 15:56:31 +03:00
|
|
|
trace.WithAttributes(th.attrs...),
|
2022-01-18 09:44:05 +03:00
|
|
|
trace.WithAttributes(
|
|
|
|
semconv.DBStatementKey.String(cmdsString),
|
|
|
|
attribute.Int("db.redis.num_cmd", len(cmds)),
|
|
|
|
),
|
2021-10-26 16:55:57 +03:00
|
|
|
}
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
ctx, _ = th.tracer.Start(ctx, "pipeline "+summary, opts...)
|
2020-10-21 15:19:27 +03:00
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:44:05 +03:00
|
|
|
func (th *TracingHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
|
2020-10-21 15:19:27 +03:00
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
if err := cmds[0].Err(); err != nil {
|
|
|
|
recordError(ctx, span, err)
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func recordError(ctx context.Context, span trace.Span, err error) {
|
|
|
|
if err != redis.Nil {
|
2020-11-21 10:56:52 +03:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2020-10-21 15:19:27 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 09:44:05 +03:00
|
|
|
|
|
|
|
type config struct {
|
2022-02-22 15:56:31 +03:00
|
|
|
tp trace.TracerProvider
|
|
|
|
attrs []attribute.KeyValue
|
2022-01-18 09:44:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option specifies instrumentation configuration options.
|
|
|
|
type Option interface {
|
|
|
|
apply(*config)
|
|
|
|
}
|
|
|
|
|
|
|
|
type optionFunc func(*config)
|
|
|
|
|
|
|
|
func (o optionFunc) apply(c *config) {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
|
|
|
|
// If none is specified, the global provider is used.
|
|
|
|
func WithTracerProvider(provider trace.TracerProvider) Option {
|
|
|
|
return optionFunc(func(cfg *config) {
|
|
|
|
if provider != nil {
|
|
|
|
cfg.tp = provider
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-02-22 15:56:31 +03:00
|
|
|
|
|
|
|
// WithAttributes specifies additional attributes to be added to the span.
|
|
|
|
func WithAttributes(attrs ...attribute.KeyValue) Option {
|
|
|
|
return optionFunc(func(cfg *config) {
|
|
|
|
cfg.attrs = append(cfg.attrs, attrs...)
|
|
|
|
})
|
|
|
|
}
|