feat: add adaptive metrics and circuit breaker trips tracking
Added metrics tracking for: - circuit_breaker_trips_total: Incremented when circuit opens - adaptive_health_status: 1 if healthy (not in aggressive mode), 0 otherwise - adaptive_event_rate: Current events per second Added methods to AdaptiveTimeoutCalculator: - getEventRate(): Returns current event rate in events/second - isHealthy(): Returns true if not in aggressive mode Metrics are updated on each buffer flush when adaptive timeout is enabled.
This commit is contained in:
@@ -329,6 +329,31 @@ class AdaptiveTimeoutCalculator {
|
||||
}
|
||||
return 'balanced'
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current event rate in events per second
|
||||
*/
|
||||
getEventRate(): number {
|
||||
if (this.eventTimestamps.length < 2) {
|
||||
return 0
|
||||
}
|
||||
const oldest = this.eventTimestamps[0]!
|
||||
const newest = this.eventTimestamps[this.eventTimestamps.length - 1]!
|
||||
const timeSpan = newest - oldest
|
||||
if (timeSpan === 0) return 0
|
||||
return (this.eventTimestamps.length / timeSpan) * 1000
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if system is healthy based on current mode
|
||||
* Conservative mode with low event rate is healthy
|
||||
* Aggressive mode might indicate high load
|
||||
*/
|
||||
isHealthy(): boolean {
|
||||
const mode = this.getMode()
|
||||
// System is considered healthy if not in aggressive mode (high load)
|
||||
return mode !== 'aggressive'
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -563,6 +588,11 @@ export const makeEventBuffer = (
|
||||
// Record metrics
|
||||
recordFlushMetrics(eventCount, force, historyCache.size)
|
||||
|
||||
// Update adaptive metrics
|
||||
if (config.enableAdaptiveTimeout && metricsModule) {
|
||||
metricsModule.updateAdaptiveMetrics(adaptiveTimeout.getEventRate(), adaptiveTimeout.isHealthy())
|
||||
}
|
||||
|
||||
// Log with [BAILEYS] prefix - use getMode() to avoid duplicating mode calculation logic
|
||||
const flushDuration = Date.now() - flushStartTime
|
||||
logEventBuffer('buffer_flush', {
|
||||
|
||||
Reference in New Issue
Block a user