package redis import ( "context" "errors" "github.com/redis/go-redis/v9" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" ) type tracingHook struct{ tracer trace.Tracer } func (h tracingHook) DialHook(next redis.DialHook) redis.DialHook { return next } func (h tracingHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { return func(ctx context.Context, cmd redis.Cmder) error { return h.run(ctx, cmd.Name(), func(ctx context.Context) error { return next(ctx, cmd) }) } } func (h tracingHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook { return func(ctx context.Context, cmds []redis.Cmder) error { return h.run(ctx, "pipeline", func(ctx context.Context) error { return next(ctx, cmds) }) } } func (h tracingHook) run(ctx context.Context, op string, call func(context.Context) error) error { ctx, span := h.tracer.Start(ctx, "redis."+op, trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attribute.String("db.system.name", "redis"), attribute.String("db.operation.name", op))) defer span.End() err := call(ctx) if err != nil && !errors.Is(err, redis.Nil) { span.SetStatus(codes.Error, "операция Redis не выполнена") } return err }