SetGlobalRecorder sets the global recorder (called by Init() in extra/redisotel-native)
(r Recorder)
| 157 | |
| 158 | // SetGlobalRecorder sets the global recorder (called by Init() in extra/redisotel-native) |
| 159 | func SetGlobalRecorder(r Recorder) { |
| 160 | recorderMu.Lock() |
| 161 | if r == nil { |
| 162 | globalRecorder = noopRecorder{} |
| 163 | operationDurationCallback = nil |
| 164 | pipelineOperationDurationCallback = nil |
| 165 | recorderMu.Unlock() |
| 166 | // Unregister all pool metric callbacks atomically |
| 167 | pool.SetAllMetricCallbacks(nil) |
| 168 | return |
| 169 | } |
| 170 | globalRecorder = r |
| 171 | |
| 172 | // Register operation duration callbacks |
| 173 | // These capture r directly since we want them to use the specific recorder |
| 174 | // that was set at this point in time |
| 175 | operationDurationCallback = func(ctx context.Context, duration time.Duration, cmd Cmder, attempts int, err error, cn *pool.Conn, dbIndex int) { |
| 176 | getRecorder().RecordOperationDuration(ctx, duration, cmd, attempts, err, cn, dbIndex) |
| 177 | } |
| 178 | pipelineOperationDurationCallback = func(ctx context.Context, duration time.Duration, operationName string, cmdCount int, attempts int, err error, cn *pool.Conn, dbIndex int) { |
| 179 | getRecorder().RecordPipelineOperationDuration(ctx, duration, operationName, cmdCount, attempts, err, cn, dbIndex) |
| 180 | } |
| 181 | recorderMu.Unlock() |
| 182 | |
| 183 | // Register all pool metric callbacks atomically |
| 184 | // These use getRecorder() to safely access the current recorder |
| 185 | pool.SetAllMetricCallbacks(&pool.MetricCallbacks{ |
| 186 | ConnectionCreateTime: func(ctx context.Context, duration time.Duration, cn *pool.Conn) { |
| 187 | getRecorder().RecordConnectionCreateTime(ctx, duration, cn) |
| 188 | }, |
| 189 | ConnectionRelaxedTimeout: func(ctx context.Context, delta int, cn *pool.Conn, poolName, notificationType string) { |
| 190 | getRecorder().RecordConnectionRelaxedTimeout(ctx, delta, cn, poolName, notificationType) |
| 191 | }, |
| 192 | ConnectionHandoff: func(ctx context.Context, cn *pool.Conn, poolName string) { |
| 193 | getRecorder().RecordConnectionHandoff(ctx, cn, poolName) |
| 194 | }, |
| 195 | Error: func(ctx context.Context, errorType string, cn *pool.Conn, statusCode string, isInternal bool, retryAttempts int) { |
| 196 | getRecorder().RecordError(ctx, errorType, cn, statusCode, isInternal, retryAttempts) |
| 197 | }, |
| 198 | MaintenanceNotification: func(ctx context.Context, cn *pool.Conn, notificationType string) { |
| 199 | getRecorder().RecordMaintenanceNotification(ctx, cn, notificationType) |
| 200 | }, |
| 201 | ConnectionWaitTime: func(ctx context.Context, duration time.Duration, cn *pool.Conn) { |
| 202 | getRecorder().RecordConnectionWaitTime(ctx, duration, cn) |
| 203 | }, |
| 204 | ConnectionClosed: func(ctx context.Context, cn *pool.Conn, reason string, err error) { |
| 205 | getRecorder().RecordConnectionClosed(ctx, cn, reason, err) |
| 206 | }, |
| 207 | ConnectionCount: func(ctx context.Context, delta int, cn *pool.Conn, state string, isPubSub bool) { |
| 208 | getRecorder().RecordConnectionCount(ctx, delta, cn, state, isPubSub) |
| 209 | }, |
| 210 | PendingRequests: func(ctx context.Context, delta int, cn *pool.Conn, poolName string) { |
| 211 | getRecorder().RecordPendingRequests(ctx, delta, cn, poolName) |
| 212 | }, |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | // RecordOperationDuration records the total operation duration. |
no test coverage detected