package redis
import (
"context"
"net"
"time"
"github.com/redis/go-redis/v9/internal/otel"
"github.com/redis/go-redis/v9/internal/pool"
)
const ErrorTypeNil = "NIL"
type ConnInfo interface {
RemoteAddr () net .Addr
PoolName () string
}
type Pooler interface {
PoolStats () *pool .Stats
}
type PubSubPooler interface {
Stats () *pool .PubSubStats
}
type OTelRecorder interface {
RecordOperationDuration (ctx context .Context , duration time .Duration , cmd Cmder , attempts int , err error , cn ConnInfo , dbIndex int )
RecordPipelineOperationDuration (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn ConnInfo , dbIndex int )
RecordConnectionCreateTime (ctx context .Context , duration time .Duration , cn ConnInfo )
RecordConnectionRelaxedTimeout (ctx context .Context , delta int , cn ConnInfo , poolName, notificationType string )
RecordConnectionHandoff (ctx context .Context , cn ConnInfo , poolName string )
RecordError (ctx context .Context , errorType string , cn ConnInfo , statusCode string , isInternal bool , retryAttempts int )
RecordMaintenanceNotification (ctx context .Context , cn ConnInfo , notificationType string )
RecordConnectionWaitTime (ctx context .Context , duration time .Duration , cn ConnInfo )
RecordConnectionClosed (ctx context .Context , cn ConnInfo , reason string , err error )
RecordPubSubMessage (ctx context .Context , cn ConnInfo , direction, channel string , sharded bool )
RecordStreamLag (ctx context .Context , lag time .Duration , cn ConnInfo , streamName, consumerGroup, consumerName string )
}
type OTelConnectionCounter interface {
RecordConnectionCount (ctx context .Context , delta int , cn ConnInfo , state string , isPubSub bool )
RecordPendingRequests (ctx context .Context , delta int , cn ConnInfo , poolName string )
}
type OTelPoolRegistrar interface {
RegisterPool (poolName string , pool Pooler )
UnregisterPool (pool Pooler )
RegisterPubSubPool (poolName string , pool PubSubPooler )
UnregisterPubSubPool (pool PubSubPooler )
}
func SetOTelRecorder (r OTelRecorder ) {
if r == nil {
otel .SetGlobalRecorder (nil )
return
}
otel .SetGlobalRecorder (&otelRecorderAdapter {r })
}
type otelRecorderAdapter struct {
recorder OTelRecorder
}
func toConnInfo(cn *pool .Conn ) ConnInfo {
if cn == nil {
return nil
}
return cn
}
func (a *otelRecorderAdapter ) RecordOperationDuration (ctx context .Context , duration time .Duration , cmd otel .Cmder , attempts int , err error , cn *pool .Conn , dbIndex int ) {
if publicCmd , ok := cmd .(Cmder ); ok {
a .recorder .RecordOperationDuration (ctx , duration , publicCmd , attempts , err , toConnInfo (cn ), dbIndex )
}
}
func (a *otelRecorderAdapter ) RecordPipelineOperationDuration (ctx context .Context , duration time .Duration , operationName string , cmdCount int , attempts int , err error , cn *pool .Conn , dbIndex int ) {
a .recorder .RecordPipelineOperationDuration (ctx , duration , operationName , cmdCount , attempts , err , toConnInfo (cn ), dbIndex )
}
func (a *otelRecorderAdapter ) RecordConnectionCreateTime (ctx context .Context , duration time .Duration , cn *pool .Conn ) {
a .recorder .RecordConnectionCreateTime (ctx , duration , toConnInfo (cn ))
}
func (a *otelRecorderAdapter ) RecordConnectionRelaxedTimeout (ctx context .Context , delta int , cn *pool .Conn , poolName , notificationType string ) {
a .recorder .RecordConnectionRelaxedTimeout (ctx , delta , toConnInfo (cn ), poolName , notificationType )
}
func (a *otelRecorderAdapter ) RecordConnectionHandoff (ctx context .Context , cn *pool .Conn , poolName string ) {
a .recorder .RecordConnectionHandoff (ctx , toConnInfo (cn ), poolName )
}
func (a *otelRecorderAdapter ) RecordError (ctx context .Context , errorType string , cn *pool .Conn , statusCode string , isInternal bool , retryAttempts int ) {
a .recorder .RecordError (ctx , errorType , toConnInfo (cn ), statusCode , isInternal , retryAttempts )
}
func (a *otelRecorderAdapter ) RecordMaintenanceNotification (ctx context .Context , cn *pool .Conn , notificationType string ) {
a .recorder .RecordMaintenanceNotification (ctx , toConnInfo (cn ), notificationType )
}
func (a *otelRecorderAdapter ) RecordConnectionWaitTime (ctx context .Context , duration time .Duration , cn *pool .Conn ) {
a .recorder .RecordConnectionWaitTime (ctx , duration , toConnInfo (cn ))
}
func (a *otelRecorderAdapter ) RecordConnectionClosed (ctx context .Context , cn *pool .Conn , reason string , err error ) {
a .recorder .RecordConnectionClosed (ctx , toConnInfo (cn ), reason , err )
}
func (a *otelRecorderAdapter ) RecordPubSubMessage (ctx context .Context , cn *pool .Conn , direction , channel string , sharded bool ) {
a .recorder .RecordPubSubMessage (ctx , toConnInfo (cn ), direction , channel , sharded )
}
func (a *otelRecorderAdapter ) RecordStreamLag (ctx context .Context , lag time .Duration , cn *pool .Conn , streamName , consumerGroup , consumerName string ) {
a .recorder .RecordStreamLag (ctx , lag , toConnInfo (cn ), streamName , consumerGroup , consumerName )
}
func (a *otelRecorderAdapter ) RecordConnectionCount (ctx context .Context , delta int , cn *pool .Conn , state string , isPubSub bool ) {
if counter , ok := a .recorder .(OTelConnectionCounter ); ok {
counter .RecordConnectionCount (ctx , delta , toConnInfo (cn ), state , isPubSub )
}
}
func (a *otelRecorderAdapter ) RecordPendingRequests (ctx context .Context , delta int , cn *pool .Conn , poolName string ) {
if counter , ok := a .recorder .(OTelConnectionCounter ); ok {
counter .RecordPendingRequests (ctx , delta , toConnInfo (cn ), poolName )
}
}
func (a *otelRecorderAdapter ) RegisterPool (poolName string , p pool .Pooler ) {
if registrar , ok := a .recorder .(OTelPoolRegistrar ); ok {
registrar .RegisterPool (poolName , &poolerAdapter {p })
}
}
func (a *otelRecorderAdapter ) UnregisterPool (p pool .Pooler ) {
if registrar , ok := a .recorder .(OTelPoolRegistrar ); ok {
registrar .UnregisterPool (&poolerAdapter {p })
}
}
func (a *otelRecorderAdapter ) RegisterPubSubPool (poolName string , p otel .PubSubPooler ) {
if registrar , ok := a .recorder .(OTelPoolRegistrar ); ok {
registrar .RegisterPubSubPool (poolName , &pubSubPoolerAdapter {p })
}
}
func (a *otelRecorderAdapter ) UnregisterPubSubPool (p otel .PubSubPooler ) {
if registrar , ok := a .recorder .(OTelPoolRegistrar ); ok {
registrar .UnregisterPubSubPool (&pubSubPoolerAdapter {p })
}
}
type poolerAdapter struct {
p pool .Pooler
}
func (a *poolerAdapter ) PoolStats () *pool .Stats {
return a .p .Stats ()
}
type pubSubPoolerAdapter struct {
p otel .PubSubPooler
}
func (a *pubSubPoolerAdapter ) Stats () *pool .PubSubStats {
return a .p .Stats ()
}
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 .