mirror of
https://github.com/redis/go-redis.git
synced 2025-12-05 06:22:07 +03:00
Adds connection state metrics
Signed-off-by: Elena Kolevska <elena@kolevska.com>
This commit is contained in:
committed by
ofekshenawa
parent
d588c3ca71
commit
2a7725db63
@@ -21,6 +21,9 @@ type Cmder interface {
|
||||
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)
|
||||
|
||||
// RecordConnectionStateChange records when a connection changes state
|
||||
RecordConnectionStateChange(ctx context.Context, cn *pool.Conn, fromState, toState string)
|
||||
}
|
||||
|
||||
// Global recorder instance (initialized by extra/redisotel-native)
|
||||
@@ -30,9 +33,16 @@ var globalRecorder Recorder = noopRecorder{}
|
||||
func SetGlobalRecorder(r Recorder) {
|
||||
if r == nil {
|
||||
globalRecorder = noopRecorder{}
|
||||
// Unregister pool callback
|
||||
pool.SetConnectionStateChangeCallback(nil)
|
||||
return
|
||||
}
|
||||
globalRecorder = r
|
||||
|
||||
// Register pool callback to forward state changes to recorder
|
||||
pool.SetConnectionStateChangeCallback(func(ctx context.Context, cn *pool.Conn, fromState, toState string) {
|
||||
globalRecorder.RecordConnectionStateChange(ctx, cn, fromState, toState)
|
||||
})
|
||||
}
|
||||
|
||||
// RecordOperationDuration records the total operation duration.
|
||||
@@ -41,7 +51,14 @@ func RecordOperationDuration(ctx context.Context, duration time.Duration, cmd Cm
|
||||
globalRecorder.RecordOperationDuration(ctx, duration, cmd, attempts, cn)
|
||||
}
|
||||
|
||||
// RecordConnectionStateChange records when a connection changes state.
|
||||
// This is called from pool.go when connections transition between states.
|
||||
func RecordConnectionStateChange(ctx context.Context, cn *pool.Conn, fromState, toState string) {
|
||||
globalRecorder.RecordConnectionStateChange(ctx, cn, fromState, toState)
|
||||
}
|
||||
|
||||
// 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) {}
|
||||
func (noopRecorder) RecordConnectionStateChange(context.Context, *pool.Conn, string, string) {}
|
||||
|
||||
Reference in New Issue
Block a user