Debian Codegraph @

dependencies/pkg/mod/github.com/redis/go-redis/[email protected]/internal/maintnotifications/logs/log_messages.go

raw
1 package logs
2
3 import (
4 "encoding/json"
5 "fmt"
6 "regexp"
7
8 "github.com/redis/go-redis/v9/internal"
9 )
10
11 // appendJSONIfDebug appends JSON data to a message only if the global log level is Debug
12 func appendJSONIfDebug(message string, data map[string]interface{}) string {
13 if internal.LogLevel.DebugOrAbove() {
14 jsonData, _ := json.Marshal(data)
15 return fmt.Sprintf("%s %s", message, string(jsonData))
16 }
17 return message
18 }
19
20 const (
21 // ========================================
22 // CIRCUIT_BREAKER.GO - Circuit breaker management
23 // ========================================
24 CircuitBreakerTransitioningToHalfOpenMessage = "circuit breaker transitioning to half-open"
25 CircuitBreakerOpenedMessage = "circuit breaker opened"
26 CircuitBreakerReopenedMessage = "circuit breaker reopened"
27 CircuitBreakerClosedMessage = "circuit breaker closed"
28 CircuitBreakerCleanupMessage = "circuit breaker cleanup"
29 CircuitBreakerOpenMessage = "circuit breaker is open, failing fast"
30
31 // ========================================
32 // CONFIG.GO - Configuration and debug
33 // ========================================
34 DebugLoggingEnabledMessage = "debug logging enabled"
35 ConfigDebugMessage = "config debug"
36
37 // ========================================
38 // ERRORS.GO - Error message constants
39 // ========================================
40 InvalidRelaxedTimeoutErrorMessage = "relaxed timeout must be greater than 0"
41 InvalidHandoffTimeoutErrorMessage = "handoff timeout must be greater than 0"
42 InvalidHandoffWorkersErrorMessage = "MaxWorkers must be greater than or equal to 0"
43 InvalidHandoffQueueSizeErrorMessage = "handoff queue size must be greater than 0"
44 InvalidPostHandoffRelaxedDurationErrorMessage = "post-handoff relaxed duration must be greater than or equal to 0"
45 InvalidEndpointTypeErrorMessage = "invalid endpoint type"
46 InvalidMaintNotificationsErrorMessage = "invalid maintenance notifications setting (must be 'disabled', 'enabled', or 'auto')"
47 InvalidHandoffRetriesErrorMessage = "MaxHandoffRetries must be between 1 and 10"
48 InvalidClientErrorMessage = "invalid client type"
49 InvalidNotificationErrorMessage = "invalid notification format"
50 MaxHandoffRetriesReachedErrorMessage = "max handoff retries reached"
51 HandoffQueueFullErrorMessage = "handoff queue is full, cannot queue new handoff requests - consider increasing HandoffQueueSize or MaxWorkers in configuration"
52 InvalidCircuitBreakerFailureThresholdErrorMessage = "circuit breaker failure threshold must be >= 1"
53 InvalidCircuitBreakerResetTimeoutErrorMessage = "circuit breaker reset timeout must be >= 0"
54 InvalidCircuitBreakerMaxRequestsErrorMessage = "circuit breaker max requests must be >= 1"
55 ConnectionMarkedForHandoffErrorMessage = "connection marked for handoff"
56 ConnectionInvalidHandoffStateErrorMessage = "connection is in invalid state for handoff"
57 ShutdownErrorMessage = "shutdown"
58 CircuitBreakerOpenErrorMessage = "circuit breaker is open, failing fast"
59
60 // ========================================
61 // EXAMPLE_HOOKS.GO - Example metrics hooks
62 // ========================================
63 MetricsHookProcessingNotificationMessage = "metrics hook processing"
64 MetricsHookRecordedErrorMessage = "metrics hook recorded error"
65
66 // ========================================
67 // HANDOFF_WORKER.GO - Connection handoff processing
68 // ========================================
69 HandoffStartedMessage = "handoff started"
70 HandoffFailedMessage = "handoff failed"
71 ConnectionNotMarkedForHandoffMessage = "is not marked for handoff and has no retries"
72 ConnectionNotMarkedForHandoffErrorMessage = "is not marked for handoff"
73 HandoffRetryAttemptMessage = "Performing handoff"
74 CannotQueueHandoffForRetryMessage = "can't queue handoff for retry"
75 HandoffQueueFullMessage = "handoff queue is full"
76 FailedToDialNewEndpointMessage = "failed to dial new endpoint"
77 ApplyingRelaxedTimeoutDueToPostHandoffMessage = "applying relaxed timeout due to post-handoff"
78 HandoffSuccessMessage = "handoff succeeded"
79 RemovingConnectionFromPoolMessage = "removing connection from pool"
80 NoPoolProvidedMessageCannotRemoveMessage = "no pool provided, cannot remove connection, closing it"
81 WorkerExitingDueToShutdownMessage = "worker exiting due to shutdown"
82 WorkerExitingDueToShutdownWhileProcessingMessage = "worker exiting due to shutdown while processing request"
83 WorkerPanicRecoveredMessage = "worker panic recovered"
84 WorkerExitingDueToInactivityTimeoutMessage = "worker exiting due to inactivity timeout"
85 ReachedMaxHandoffRetriesMessage = "reached max handoff retries"
86
87 // ========================================
88 // MANAGER.GO - Moving operation tracking and handler registration
89 // ========================================
90 DuplicateMovingOperationMessage = "duplicate MOVING operation ignored"
91 TrackingMovingOperationMessage = "tracking MOVING operation"
92 UntrackingMovingOperationMessage = "untracking MOVING operation"
93 OperationNotTrackedMessage = "operation not tracked"
94 FailedToRegisterHandlerMessage = "failed to register handler"
95
96 // ========================================
97 // HOOKS.GO - Notification processing hooks
98 // ========================================
99 ProcessingNotificationMessage = "processing notification started"
100 ProcessingNotificationFailedMessage = "proccessing notification failed"
101 ProcessingNotificationSucceededMessage = "processing notification succeeded"
102
103 // ========================================
104 // POOL_HOOK.GO - Pool connection management
105 // ========================================
106 FailedToQueueHandoffMessage = "failed to queue handoff"
107 MarkedForHandoffMessage = "connection marked for handoff"
108
109 // ========================================
110 // PUSH_NOTIFICATION_HANDLER.GO - Push notification validation and processing
111 // ========================================
112 InvalidNotificationFormatMessage = "invalid notification format"
113 InvalidNotificationTypeFormatMessage = "invalid notification type format"
114 InvalidSeqIDInMovingNotificationMessage = "invalid seqID in MOVING notification"
115 InvalidTimeSInMovingNotificationMessage = "invalid timeS in MOVING notification"
116 InvalidNewEndpointInMovingNotificationMessage = "invalid newEndpoint in MOVING notification"
117 NoConnectionInHandlerContextMessage = "no connection in handler context"
118 InvalidConnectionTypeInHandlerContextMessage = "invalid connection type in handler context"
119 SchedulingHandoffToCurrentEndpointMessage = "scheduling handoff to current endpoint"
120 RelaxedTimeoutDueToNotificationMessage = "applying relaxed timeout due to notification"
121 UnrelaxedTimeoutMessage = "clearing relaxed timeout"
122 ManagerNotInitializedMessage = "manager not initialized"
123 FailedToMarkForHandoffMessage = "failed to mark connection for handoff"
124
125 // ========================================
126 // used in pool/conn
127 // ========================================
128 UnrelaxedTimeoutAfterDeadlineMessage = "clearing relaxed timeout after deadline"
129 )
130
131 func HandoffStarted(connID uint64, newEndpoint string) string {
132 message := fmt.Sprintf("conn[%d] %s to %s", connID, HandoffStartedMessage, newEndpoint)
133 return appendJSONIfDebug(message, map[string]interface{}{
134 "connID": connID,
135 "endpoint": newEndpoint,
136 })
137 }
138
139 func HandoffFailed(connID uint64, newEndpoint string, attempt int, maxAttempts int, err error) string {
140 message := fmt.Sprintf("conn[%d] %s to %s (attempt %d/%d): %v", connID, HandoffFailedMessage, newEndpoint, attempt, maxAttempts, err)
141 return appendJSONIfDebug(message, map[string]interface{}{
142 "connID": connID,
143 "endpoint": newEndpoint,
144 "attempt": attempt,
145 "maxAttempts": maxAttempts,
146 "error": err.Error(),
147 })
148 }
149
150 func HandoffSucceeded(connID uint64, newEndpoint string) string {
151 message := fmt.Sprintf("conn[%d] %s to %s", connID, HandoffSuccessMessage, newEndpoint)
152 return appendJSONIfDebug(message, map[string]interface{}{
153 "connID": connID,
154 "endpoint": newEndpoint,
155 })
156 }
157
158 // Timeout-related log functions
159 func RelaxedTimeoutDueToNotification(connID uint64, notificationType string, timeout interface{}) string {
160 message := fmt.Sprintf("conn[%d] %s %s (%v)", connID, RelaxedTimeoutDueToNotificationMessage, notificationType, timeout)
161 return appendJSONIfDebug(message, map[string]interface{}{
162 "connID": connID,
163 "notificationType": notificationType,
164 "timeout": fmt.Sprintf("%v", timeout),
165 })
166 }
167
168 func UnrelaxedTimeout(connID uint64) string {
169 message := fmt.Sprintf("conn[%d] %s", connID, UnrelaxedTimeoutMessage)
170 return appendJSONIfDebug(message, map[string]interface{}{
171 "connID": connID,
172 })
173 }
174
175 func UnrelaxedTimeoutAfterDeadline(connID uint64) string {
176 message := fmt.Sprintf("conn[%d] %s", connID, UnrelaxedTimeoutAfterDeadlineMessage)
177 return appendJSONIfDebug(message, map[string]interface{}{
178 "connID": connID,
179 })
180 }
181
182 // Handoff queue and marking functions
183 func HandoffQueueFull(queueLen, queueCap int) string {
184 message := fmt.Sprintf("%s (%d/%d), cannot queue new handoff requests - consider increasing HandoffQueueSize or MaxWorkers in configuration", HandoffQueueFullMessage, queueLen, queueCap)
185 return appendJSONIfDebug(message, map[string]interface{}{
186 "queueLen": queueLen,
187 "queueCap": queueCap,
188 })
189 }
190
191 func FailedToQueueHandoff(connID uint64, err error) string {
192 message := fmt.Sprintf("conn[%d] %s: %v", connID, FailedToQueueHandoffMessage, err)
193 return appendJSONIfDebug(message, map[string]interface{}{
194 "connID": connID,
195 "error": err.Error(),
196 })
197 }
198
199 func FailedToMarkForHandoff(connID uint64, err error) string {
200 message := fmt.Sprintf("conn[%d] %s: %v", connID, FailedToMarkForHandoffMessage, err)
201 return appendJSONIfDebug(message, map[string]interface{}{
202 "connID": connID,
203 "error": err.Error(),
204 })
205 }
206
207 func FailedToDialNewEndpoint(connID uint64, endpoint string, err error) string {
208 message := fmt.Sprintf("conn[%d] %s %s: %v", connID, FailedToDialNewEndpointMessage, endpoint, err)
209 return appendJSONIfDebug(message, map[string]interface{}{
210 "connID": connID,
211 "endpoint": endpoint,
212 "error": err.Error(),
213 })
214 }
215
216 func ReachedMaxHandoffRetries(connID uint64, endpoint string, maxRetries int) string {
217 message := fmt.Sprintf("conn[%d] %s to %s (max retries: %d)", connID, ReachedMaxHandoffRetriesMessage, endpoint, maxRetries)
218 return appendJSONIfDebug(message, map[string]interface{}{
219 "connID": connID,
220 "endpoint": endpoint,
221 "maxRetries": maxRetries,
222 })
223 }
224
225 // Notification processing functions
226 func ProcessingNotification(connID uint64, seqID int64, notificationType string, notification interface{}) string {
227 message := fmt.Sprintf("conn[%d] seqID[%d] %s %s: %v", connID, seqID, ProcessingNotificationMessage, notificationType, notification)
228 return appendJSONIfDebug(message, map[string]interface{}{
229 "connID": connID,
230 "seqID": seqID,
231 "notificationType": notificationType,
232 "notification": fmt.Sprintf("%v", notification),
233 })
234 }
235
236 func ProcessingNotificationFailed(connID uint64, notificationType string, err error, notification interface{}) string {
237 message := fmt.Sprintf("conn[%d] %s %s: %v - %v", connID, ProcessingNotificationFailedMessage, notificationType, err, notification)
238 return appendJSONIfDebug(message, map[string]interface{}{
239 "connID": connID,
240 "notificationType": notificationType,
241 "error": err.Error(),
242 "notification": fmt.Sprintf("%v", notification),
243 })
244 }
245
246 func ProcessingNotificationSucceeded(connID uint64, notificationType string) string {
247 message := fmt.Sprintf("conn[%d] %s %s", connID, ProcessingNotificationSucceededMessage, notificationType)
248 return appendJSONIfDebug(message, map[string]interface{}{
249 "connID": connID,
250 "notificationType": notificationType,
251 })
252 }
253
254 // Moving operation tracking functions
255 func DuplicateMovingOperation(connID uint64, endpoint string, seqID int64) string {
256 message := fmt.Sprintf("conn[%d] %s for %s seqID[%d]", connID, DuplicateMovingOperationMessage, endpoint, seqID)
257 return appendJSONIfDebug(message, map[string]interface{}{
258 "connID": connID,
259 "endpoint": endpoint,
260 "seqID": seqID,
261 })
262 }
263
264 func TrackingMovingOperation(connID uint64, endpoint string, seqID int64) string {
265 message := fmt.Sprintf("conn[%d] %s for %s seqID[%d]", connID, TrackingMovingOperationMessage, endpoint, seqID)
266 return appendJSONIfDebug(message, map[string]interface{}{
267 "connID": connID,
268 "endpoint": endpoint,
269 "seqID": seqID,
270 })
271 }
272
273 func UntrackingMovingOperation(connID uint64, seqID int64) string {
274 message := fmt.Sprintf("conn[%d] %s seqID[%d]", connID, UntrackingMovingOperationMessage, seqID)
275 return appendJSONIfDebug(message, map[string]interface{}{
276 "connID": connID,
277 "seqID": seqID,
278 })
279 }
280
281 func OperationNotTracked(connID uint64, seqID int64) string {
282 message := fmt.Sprintf("conn[%d] %s seqID[%d]", connID, OperationNotTrackedMessage, seqID)
283 return appendJSONIfDebug(message, map[string]interface{}{
284 "connID": connID,
285 "seqID": seqID,
286 })
287 }
288
289 // Connection pool functions
290 func RemovingConnectionFromPool(connID uint64, reason error) string {
291 message := fmt.Sprintf("conn[%d] %s due to: %v", connID, RemovingConnectionFromPoolMessage, reason)
292 return appendJSONIfDebug(message, map[string]interface{}{
293 "connID": connID,
294 "reason": reason.Error(),
295 })
296 }
297
298 func NoPoolProvidedCannotRemove(connID uint64, reason error) string {
299 message := fmt.Sprintf("conn[%d] %s due to: %v", connID, NoPoolProvidedMessageCannotRemoveMessage, reason)
300 return appendJSONIfDebug(message, map[string]interface{}{
301 "connID": connID,
302 "reason": reason.Error(),
303 })
304 }
305
306 // Circuit breaker functions
307 func CircuitBreakerOpen(connID uint64, endpoint string) string {
308 message := fmt.Sprintf("conn[%d] %s for %s", connID, CircuitBreakerOpenMessage, endpoint)
309 return appendJSONIfDebug(message, map[string]interface{}{
310 "connID": connID,
311 "endpoint": endpoint,
312 })
313 }
314
315 // Additional handoff functions for specific cases
316 func ConnectionNotMarkedForHandoff(connID uint64) string {
317 message := fmt.Sprintf("conn[%d] %s", connID, ConnectionNotMarkedForHandoffMessage)
318 return appendJSONIfDebug(message, map[string]interface{}{
319 "connID": connID,
320 })
321 }
322
323 func ConnectionNotMarkedForHandoffError(connID uint64) string {
324 return fmt.Sprintf("conn[%d] %s", connID, ConnectionNotMarkedForHandoffErrorMessage)
325 }
326
327 func HandoffRetryAttempt(connID uint64, retries int, newEndpoint string, oldEndpoint string) string {
328 message := fmt.Sprintf("conn[%d] Retry %d: %s to %s(was %s)", connID, retries, HandoffRetryAttemptMessage, newEndpoint, oldEndpoint)
329 return appendJSONIfDebug(message, map[string]interface{}{
330 "connID": connID,
331 "retries": retries,
332 "newEndpoint": newEndpoint,
333 "oldEndpoint": oldEndpoint,
334 })
335 }
336
337 func CannotQueueHandoffForRetry(err error) string {
338 message := fmt.Sprintf("%s: %v", CannotQueueHandoffForRetryMessage, err)
339 return appendJSONIfDebug(message, map[string]interface{}{
340 "error": err.Error(),
341 })
342 }
343
344 // Validation and error functions
345 func InvalidNotificationFormat(notification interface{}) string {
346 message := fmt.Sprintf("%s: %v", InvalidNotificationFormatMessage, notification)
347 return appendJSONIfDebug(message, map[string]interface{}{
348 "notification": fmt.Sprintf("%v", notification),
349 })
350 }
351
352 func InvalidNotificationTypeFormat(notificationType interface{}) string {
353 message := fmt.Sprintf("%s: %v", InvalidNotificationTypeFormatMessage, notificationType)
354 return appendJSONIfDebug(message, map[string]interface{}{
355 "notificationType": fmt.Sprintf("%v", notificationType),
356 })
357 }
358
359 // InvalidNotification creates a log message for invalid notifications of any type
360 func InvalidNotification(notificationType string, notification interface{}) string {
361 message := fmt.Sprintf("invalid %s notification: %v", notificationType, notification)
362 return appendJSONIfDebug(message, map[string]interface{}{
363 "notificationType": notificationType,
364 "notification": fmt.Sprintf("%v", notification),
365 })
366 }
367
368 func InvalidSeqIDInMovingNotification(seqID interface{}) string {
369 message := fmt.Sprintf("%s: %v", InvalidSeqIDInMovingNotificationMessage, seqID)
370 return appendJSONIfDebug(message, map[string]interface{}{
371 "seqID": fmt.Sprintf("%v", seqID),
372 })
373 }
374
375 func InvalidTimeSInMovingNotification(timeS interface{}) string {
376 message := fmt.Sprintf("%s: %v", InvalidTimeSInMovingNotificationMessage, timeS)
377 return appendJSONIfDebug(message, map[string]interface{}{
378 "timeS": fmt.Sprintf("%v", timeS),
379 })
380 }
381
382 func InvalidNewEndpointInMovingNotification(newEndpoint interface{}) string {
383 message := fmt.Sprintf("%s: %v", InvalidNewEndpointInMovingNotificationMessage, newEndpoint)
384 return appendJSONIfDebug(message, map[string]interface{}{
385 "newEndpoint": fmt.Sprintf("%v", newEndpoint),
386 })
387 }
388
389 func NoConnectionInHandlerContext(notificationType string) string {
390 message := fmt.Sprintf("%s for %s notification", NoConnectionInHandlerContextMessage, notificationType)
391 return appendJSONIfDebug(message, map[string]interface{}{
392 "notificationType": notificationType,
393 })
394 }
395
396 func InvalidConnectionTypeInHandlerContext(notificationType string, conn interface{}, handlerCtx interface{}) string {
397 message := fmt.Sprintf("%s for %s notification - %T %#v", InvalidConnectionTypeInHandlerContextMessage, notificationType, conn, handlerCtx)
398 return appendJSONIfDebug(message, map[string]interface{}{
399 "notificationType": notificationType,
400 "connType": fmt.Sprintf("%T", conn),
401 })
402 }
403
404 func SchedulingHandoffToCurrentEndpoint(connID uint64, seconds float64) string {
405 message := fmt.Sprintf("conn[%d] %s in %v seconds", connID, SchedulingHandoffToCurrentEndpointMessage, seconds)
406 return appendJSONIfDebug(message, map[string]interface{}{
407 "connID": connID,
408 "seconds": seconds,
409 })
410 }
411
412 func ManagerNotInitialized() string {
413 return appendJSONIfDebug(ManagerNotInitializedMessage, map[string]interface{}{})
414 }
415
416 func FailedToRegisterHandler(notificationType string, err error) string {
417 message := fmt.Sprintf("%s for %s: %v", FailedToRegisterHandlerMessage, notificationType, err)
418 return appendJSONIfDebug(message, map[string]interface{}{
419 "notificationType": notificationType,
420 "error": err.Error(),
421 })
422 }
423
424 func ShutdownError() string {
425 return appendJSONIfDebug(ShutdownErrorMessage, map[string]interface{}{})
426 }
427
428 // Configuration validation error functions
429 func InvalidRelaxedTimeoutError() string {
430 return appendJSONIfDebug(InvalidRelaxedTimeoutErrorMessage, map[string]interface{}{})
431 }
432
433 func InvalidHandoffTimeoutError() string {
434 return appendJSONIfDebug(InvalidHandoffTimeoutErrorMessage, map[string]interface{}{})
435 }
436
437 func InvalidHandoffWorkersError() string {
438 return appendJSONIfDebug(InvalidHandoffWorkersErrorMessage, map[string]interface{}{})
439 }
440
441 func InvalidHandoffQueueSizeError() string {
442 return appendJSONIfDebug(InvalidHandoffQueueSizeErrorMessage, map[string]interface{}{})
443 }
444
445 func InvalidPostHandoffRelaxedDurationError() string {
446 return appendJSONIfDebug(InvalidPostHandoffRelaxedDurationErrorMessage, map[string]interface{}{})
447 }
448
449 func InvalidEndpointTypeError() string {
450 return appendJSONIfDebug(InvalidEndpointTypeErrorMessage, map[string]interface{}{})
451 }
452
453 func InvalidMaintNotificationsError() string {
454 return appendJSONIfDebug(InvalidMaintNotificationsErrorMessage, map[string]interface{}{})
455 }
456
457 func InvalidHandoffRetriesError() string {
458 return appendJSONIfDebug(InvalidHandoffRetriesErrorMessage, map[string]interface{}{})
459 }
460
461 func InvalidClientError() string {
462 return appendJSONIfDebug(InvalidClientErrorMessage, map[string]interface{}{})
463 }
464
465 func InvalidNotificationError() string {
466 return appendJSONIfDebug(InvalidNotificationErrorMessage, map[string]interface{}{})
467 }
468
469 func MaxHandoffRetriesReachedError() string {
470 return appendJSONIfDebug(MaxHandoffRetriesReachedErrorMessage, map[string]interface{}{})
471 }
472
473 func HandoffQueueFullError() string {
474 return appendJSONIfDebug(HandoffQueueFullErrorMessage, map[string]interface{}{})
475 }
476
477 func InvalidCircuitBreakerFailureThresholdError() string {
478 return appendJSONIfDebug(InvalidCircuitBreakerFailureThresholdErrorMessage, map[string]interface{}{})
479 }
480
481 func InvalidCircuitBreakerResetTimeoutError() string {
482 return appendJSONIfDebug(InvalidCircuitBreakerResetTimeoutErrorMessage, map[string]interface{}{})
483 }
484
485 func InvalidCircuitBreakerMaxRequestsError() string {
486 return appendJSONIfDebug(InvalidCircuitBreakerMaxRequestsErrorMessage, map[string]interface{}{})
487 }
488
489 // Configuration and debug functions
490 func DebugLoggingEnabled() string {
491 return appendJSONIfDebug(DebugLoggingEnabledMessage, map[string]interface{}{})
492 }
493
494 func ConfigDebug(config interface{}) string {
495 message := fmt.Sprintf("%s: %+v", ConfigDebugMessage, config)
496 return appendJSONIfDebug(message, map[string]interface{}{
497 "config": fmt.Sprintf("%+v", config),
498 })
499 }
500
501 // Handoff worker functions
502 func WorkerExitingDueToShutdown() string {
503 return appendJSONIfDebug(WorkerExitingDueToShutdownMessage, map[string]interface{}{})
504 }
505
506 func WorkerExitingDueToShutdownWhileProcessing() string {
507 return appendJSONIfDebug(WorkerExitingDueToShutdownWhileProcessingMessage, map[string]interface{}{})
508 }
509
510 func WorkerPanicRecovered(panicValue interface{}) string {
511 message := fmt.Sprintf("%s: %v", WorkerPanicRecoveredMessage, panicValue)
512 return appendJSONIfDebug(message, map[string]interface{}{
513 "panic": fmt.Sprintf("%v", panicValue),
514 })
515 }
516
517 func WorkerExitingDueToInactivityTimeout(timeout interface{}) string {
518 message := fmt.Sprintf("%s (%v)", WorkerExitingDueToInactivityTimeoutMessage, timeout)
519 return appendJSONIfDebug(message, map[string]interface{}{
520 "timeout": fmt.Sprintf("%v", timeout),
521 })
522 }
523
524 func ApplyingRelaxedTimeoutDueToPostHandoff(connID uint64, timeout interface{}, until string) string {
525 message := fmt.Sprintf("conn[%d] %s (%v) until %s", connID, ApplyingRelaxedTimeoutDueToPostHandoffMessage, timeout, until)
526 return appendJSONIfDebug(message, map[string]interface{}{
527 "connID": connID,
528 "timeout": fmt.Sprintf("%v", timeout),
529 "until": until,
530 })
531 }
532
533 // Example hooks functions
534 func MetricsHookProcessingNotification(notificationType string, connID uint64) string {
535 message := fmt.Sprintf("%s %s notification on conn[%d]", MetricsHookProcessingNotificationMessage, notificationType, connID)
536 return appendJSONIfDebug(message, map[string]interface{}{
537 "notificationType": notificationType,
538 "connID": connID,
539 })
540 }
541
542 func MetricsHookRecordedError(notificationType string, connID uint64, err error) string {
543 message := fmt.Sprintf("%s for %s notification on conn[%d]: %v", MetricsHookRecordedErrorMessage, notificationType, connID, err)
544 return appendJSONIfDebug(message, map[string]interface{}{
545 "notificationType": notificationType,
546 "connID": connID,
547 "error": err.Error(),
548 })
549 }
550
551 // Pool hook functions
552 func MarkedForHandoff(connID uint64) string {
553 message := fmt.Sprintf("conn[%d] %s", connID, MarkedForHandoffMessage)
554 return appendJSONIfDebug(message, map[string]interface{}{
555 "connID": connID,
556 })
557 }
558
559 // Circuit breaker additional functions
560 func CircuitBreakerTransitioningToHalfOpen(endpoint string) string {
561 message := fmt.Sprintf("%s for %s", CircuitBreakerTransitioningToHalfOpenMessage, endpoint)
562 return appendJSONIfDebug(message, map[string]interface{}{
563 "endpoint": endpoint,
564 })
565 }
566
567 func CircuitBreakerOpened(endpoint string, failures int64) string {
568 message := fmt.Sprintf("%s for endpoint %s after %d failures", CircuitBreakerOpenedMessage, endpoint, failures)
569 return appendJSONIfDebug(message, map[string]interface{}{
570 "endpoint": endpoint,
571 "failures": failures,
572 })
573 }
574
575 func CircuitBreakerReopened(endpoint string) string {
576 message := fmt.Sprintf("%s for endpoint %s due to failure in half-open state", CircuitBreakerReopenedMessage, endpoint)
577 return appendJSONIfDebug(message, map[string]interface{}{
578 "endpoint": endpoint,
579 })
580 }
581
582 func CircuitBreakerClosed(endpoint string, successes int64) string {
583 message := fmt.Sprintf("%s for endpoint %s after %d successful requests", CircuitBreakerClosedMessage, endpoint, successes)
584 return appendJSONIfDebug(message, map[string]interface{}{
585 "endpoint": endpoint,
586 "successes": successes,
587 })
588 }
589
590 func CircuitBreakerCleanup(removed int, total int) string {
591 message := fmt.Sprintf("%s removed %d/%d entries", CircuitBreakerCleanupMessage, removed, total)
592 return appendJSONIfDebug(message, map[string]interface{}{
593 "removed": removed,
594 "total": total,
595 })
596 }
597
598 // ExtractDataFromLogMessage extracts structured data from maintnotifications log messages
599 // Returns a map containing the parsed key-value pairs from the structured data section
600 // Example: "conn[123] handoff started to localhost:6379 {"connID":123,"endpoint":"localhost:6379"}"
601 // Returns: map[string]interface{}{"connID": 123, "endpoint": "localhost:6379"}
602 func ExtractDataFromLogMessage(logMessage string) map[string]interface{} {
603 result := make(map[string]interface{})
604
605 // Find the JSON data section at the end of the message
606 re := regexp.MustCompile(`(\{.*\})$`)
607 matches := re.FindStringSubmatch(logMessage)
608 if len(matches) < 2 {
609 return result
610 }
611
612 jsonStr := matches[1]
613 if jsonStr == "" {
614 return result
615 }
616
617 // Parse the JSON directly
618 var jsonResult map[string]interface{}
619 if err := json.Unmarshal([]byte(jsonStr), &jsonResult); err == nil {
620 return jsonResult
621 }
622
623 // If JSON parsing fails, return empty map
624 return result
625 }