1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-05 06:22:07 +03:00
Files
go-redis/internal/otel/metrics.go
Elena Kolevska d588c3ca71 First draft. One metric - command duration.
Signed-off-by: Elena Kolevska <elena@kolevska.com>
2025-12-03 10:00:09 +02:00

48 lines
1.5 KiB
Go

package otel
import (
"context"
"time"
"github.com/redis/go-redis/v9/internal/pool"
)
// Cmder is a minimal interface for command information needed for metrics.
// This avoids circular dependencies with the main redis package.
type Cmder interface {
Name() string
FullName() string
Args() []interface{}
Err() error
}
// Recorder is the interface for recording metrics.
// Implementations are provided by extra/redisotel-native package.
type Recorder interface {
// RecordOperationDuration records the total operation duration (including all retries)
RecordOperationDuration(ctx context.Context, duration time.Duration, cmd Cmder, attempts int, cn *pool.Conn)
}
// Global recorder instance (initialized by extra/redisotel-native)
var globalRecorder Recorder = noopRecorder{}
// SetGlobalRecorder sets the global recorder (called by Init() in extra/redisotel-native)
func SetGlobalRecorder(r Recorder) {
if r == nil {
globalRecorder = noopRecorder{}
return
}
globalRecorder = r
}
// RecordOperationDuration records the total operation duration.
// This is called from redis.go after command execution completes.
func RecordOperationDuration(ctx context.Context, duration time.Duration, cmd Cmder, attempts int, cn *pool.Conn) {
globalRecorder.RecordOperationDuration(ctx, duration, cmd, attempts, cn)
}
// noopRecorder is a no-op implementation (zero overhead when metrics disabled)
type noopRecorder struct{}
func (noopRecorder) RecordOperationDuration(context.Context, time.Duration, Cmder, int, *pool.Conn) {}