package otel
import (
"context"
"crypto/rand"
"encoding/hex"
"sync"
"time"
"github.com/redis/go-redis/v9/internal/pool"
)
func generateUniqueID() string {
b := make ([]byte , 4 )
if _ , err := rand .Read (b ); err != nil {
return ""
}
return hex .EncodeToString (b )
}
type Cmder interface {
Name () string
FullName () string
Args () []interface {}
Err () error
}
type Recorder interface {
RecordOperationDuration (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn *pool .Conn , dbIndex int )
RecordPipelineOperationDuration (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int )
RecordConnectionCreateTime (ctx context .Context , duration time .Duration , cn *pool .Conn )
RecordConnectionRelaxedTimeout (ctx context .Context , delta int , cn *pool .Conn , poolName, notificationType string )
RecordConnectionHandoff (ctx context .Context , cn *pool .Conn , poolName string )
RecordError (ctx context .Context , errorType string , cn *pool .Conn , statusCode string , isInternal bool , retryAttempts int )
RecordMaintenanceNotification (ctx context .Context , cn *pool .Conn , notificationType string )
RecordConnectionWaitTime (ctx context .Context , duration time .Duration , cn *pool .Conn )
RecordConnectionClosed (ctx context .Context , cn *pool .Conn , reason string , err error )
RecordPubSubMessage (ctx context .Context , cn *pool .Conn , direction, channel string , sharded bool )
RecordStreamLag (ctx context .Context , lag time .Duration , cn *pool .Conn , streamName, consumerGroup, consumerName string )
RecordConnectionCount (ctx context .Context , delta int , cn *pool .Conn , state string , isPubSub bool )
RecordPendingRequests (ctx context .Context , delta int , cn *pool .Conn , poolName string )
}
type PubSubPooler interface {
Stats () *pool .PubSubStats
}
type PoolRegistrar interface {
RegisterPool (poolName string , pool pool .Pooler )
UnregisterPool (pool pool .Pooler )
RegisterPubSubPool (poolName string , pool PubSubPooler )
UnregisterPubSubPool (pool PubSubPooler )
}
var (
recorderMu sync .RWMutex
globalRecorder Recorder = noopRecorder {}
operationDurationCallback func (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn *pool .Conn , dbIndex int )
pipelineOperationDurationCallback func (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int )
)
func GetOperationDurationCallback () func (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn *pool .Conn , dbIndex int ) {
recorderMu .RLock ()
cb := operationDurationCallback
recorderMu .RUnlock ()
return cb
}
func GetPipelineOperationDurationCallback () func (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int ) {
recorderMu .RLock ()
cb := pipelineOperationDurationCallback
recorderMu .RUnlock ()
return cb
}
func getRecorder() Recorder {
recorderMu .RLock ()
r := globalRecorder
recorderMu .RUnlock ()
return r
}
func Enabled () bool {
_ , noop := getRecorder ().(noopRecorder )
return !noop
}
func SetGlobalRecorder (r Recorder ) {
recorderMu .Lock ()
if r == nil {
globalRecorder = noopRecorder {}
operationDurationCallback = nil
pipelineOperationDurationCallback = nil
recorderMu .Unlock ()
pool .SetAllMetricCallbacks (nil )
return
}
globalRecorder = r
operationDurationCallback = func (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn *pool .Conn , dbIndex int ) {
getRecorder ().RecordOperationDuration (ctx , duration , cmd , attempts , err , cn , dbIndex )
}
pipelineOperationDurationCallback = func (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int ) {
getRecorder ().RecordPipelineOperationDuration (ctx , duration , operationName , cmdCount , attempts , err , cn , dbIndex )
}
recorderMu .Unlock ()
pool .SetAllMetricCallbacks (&pool .MetricCallbacks {
ConnectionCreateTime : func (ctx context .Context , duration time .Duration , cn *pool .Conn ) {
getRecorder ().RecordConnectionCreateTime (ctx , duration , cn )
},
ConnectionRelaxedTimeout : func (ctx context .Context , delta int , cn *pool .Conn , poolName , notificationType string ) {
getRecorder ().RecordConnectionRelaxedTimeout (ctx , delta , cn , poolName , notificationType )
},
ConnectionHandoff : func (ctx context .Context , cn *pool .Conn , poolName string ) {
getRecorder ().RecordConnectionHandoff (ctx , cn , poolName )
},
Error : func (ctx context .Context , errorType string , cn *pool .Conn , statusCode string , isInternal bool , retryAttempts int ) {
getRecorder ().RecordError (ctx , errorType , cn , statusCode , isInternal , retryAttempts )
},
MaintenanceNotification : func (ctx context .Context , cn *pool .Conn , notificationType string ) {
getRecorder ().RecordMaintenanceNotification (ctx , cn , notificationType )
},
ConnectionWaitTime : func (ctx context .Context , duration time .Duration , cn *pool .Conn ) {
getRecorder ().RecordConnectionWaitTime (ctx , duration , cn )
},
ConnectionClosed : func (ctx context .Context , cn *pool .Conn , reason string , err error ) {
getRecorder ().RecordConnectionClosed (ctx , cn , reason , err )
},
ConnectionCount : func (ctx context .Context , delta int , cn *pool .Conn , state string , isPubSub bool ) {
getRecorder ().RecordConnectionCount (ctx , delta , cn , state , isPubSub )
},
PendingRequests : func (ctx context .Context , delta int , cn *pool .Conn , poolName string ) {
getRecorder ().RecordPendingRequests (ctx , delta , cn , poolName )
},
})
}
func RecordOperationDuration (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn *pool .Conn , dbIndex int ) {
getRecorder ().RecordOperationDuration (ctx , duration , cmd , attempts , err , cn , dbIndex )
}
func RecordPipelineOperationDuration (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int ) {
getRecorder ().RecordPipelineOperationDuration (ctx , duration , operationName , cmdCount , attempts , err , cn , dbIndex )
}
func RecordConnectionCreateTime (ctx context .Context , duration time .Duration , cn *pool .Conn ) {
getRecorder ().RecordConnectionCreateTime (ctx , duration , cn )
}
func RecordPubSubMessage (ctx context .Context , cn *pool .Conn , direction , channel string , sharded bool ) {
getRecorder ().RecordPubSubMessage (ctx , cn , direction , channel , sharded )
}
func RecordStreamLag (ctx context .Context , lag time .Duration , cn *pool .Conn , streamName , consumerGroup , consumerName string ) {
getRecorder ().RecordStreamLag (ctx , lag , cn , streamName , consumerGroup , consumerName )
}
type noopRecorder struct {}
func (noopRecorder ) RecordOperationDuration (context .Context , time .Duration , Cmder , int , error , *pool .Conn , int ) {
}
func (noopRecorder ) RecordPipelineOperationDuration (context .Context , time .Duration , string , int , int , error , *pool .Conn , int ) {
}
func (noopRecorder ) RecordConnectionCreateTime (context .Context , time .Duration , *pool .Conn ) {}
func (noopRecorder ) RecordConnectionRelaxedTimeout (context .Context , int , *pool .Conn , string , string ) {
}
func (noopRecorder ) RecordConnectionHandoff (context .Context , *pool .Conn , string ) {}
func (noopRecorder ) RecordError (context .Context , string , *pool .Conn , string , bool , int ) {}
func (noopRecorder ) RecordMaintenanceNotification (context .Context , *pool .Conn , string ) {}
func (noopRecorder ) RecordConnectionWaitTime (context .Context , time .Duration , *pool .Conn ) {}
func (noopRecorder ) RecordConnectionClosed (context .Context , *pool .Conn , string , error ) {}
func (noopRecorder ) RecordPubSubMessage (context .Context , *pool .Conn , string , string , bool ) {}
func (noopRecorder ) RecordStreamLag (context .Context , time .Duration , *pool .Conn , string , string , string ) {
}
func (noopRecorder ) RecordConnectionCount (context .Context , int , *pool .Conn , string , bool ) {}
func (noopRecorder ) RecordPendingRequests (context .Context , int , *pool .Conn , string ) {}
func RegisterPools (connPool pool .Pooler , pubSubPool PubSubPooler , pipelinePool pool .Pooler , addr string ) {
if registrar , ok := getRecorder ().(PoolRegistrar ); ok {
uniqueID := generateUniqueID ()
if connPool != nil {
poolName := addr + "_" + uniqueID
registrar .RegisterPool (poolName , connPool )
}
if pubSubPool != nil {
poolName := addr + "_" + uniqueID + "_pubsub"
registrar .RegisterPubSubPool (poolName , pubSubPool )
}
if pipelinePool != nil {
poolName := addr + "_" + uniqueID + "_pipeline"
registrar .RegisterPool (poolName , pipelinePool )
}
}
}
func UnregisterPools (connPool pool .Pooler , pubSubPool PubSubPooler , pipelinePool pool .Pooler ) {
if registrar , ok := getRecorder ().(PoolRegistrar ); ok {
if connPool != nil {
registrar .UnregisterPool (connPool )
}
if pubSubPool != nil {
registrar .UnregisterPubSubPool (pubSubPool )
}
if pipelinePool != nil {
registrar .UnregisterPool (pipelinePool )
}
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .