From 5b8ea3bb2bdba9ce1d03ef0a2a84661d684bd4f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Jan 2026 13:47:48 +0000 Subject: [PATCH] fix: add buffer_destroyed_total and buffer_final_flush_total metrics Added recordBufferDestroyed() and recordBufferFinalFlush() functions and integrated into destroy() function in event-buffer.ts. Tracks: - Buffer destruction with reason and whether there was pending flush - Final flushes that occur during buffer destruction --- src/Utils/event-buffer.ts | 12 +++++++++++- src/Utils/prometheus-metrics.ts | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Utils/event-buffer.ts b/src/Utils/event-buffer.ts index 262eb3c0..c5f67c1b 100644 --- a/src/Utils/event-buffer.ts +++ b/src/Utils/event-buffer.ts @@ -607,11 +607,16 @@ export const makeEventBuffer = ( } logger.debug('Destroying event buffer') + const hadPendingFlush = isBuffering destroyed = true // Flush any remaining events - if (isBuffering) { + if (hadPendingFlush) { flush(true) + // Record final flush metric + if (metricsModule) { + metricsModule.recordBufferFinalFlush() + } } // Clear all timers @@ -624,6 +629,11 @@ export const makeEventBuffer = ( // Remove all event listeners ev.removeAllListeners() + // Record buffer destroyed metric + if (metricsModule) { + metricsModule.recordBufferDestroyed('normal', hadPendingFlush) + } + logger.debug('Event buffer destroyed successfully') } diff --git a/src/Utils/prometheus-metrics.ts b/src/Utils/prometheus-metrics.ts index d9728b20..7eadbf3d 100644 --- a/src/Utils/prometheus-metrics.ts +++ b/src/Utils/prometheus-metrics.ts @@ -1934,6 +1934,30 @@ export function recordConnectionError(errorType: string): void { } } +/** + * Record buffer destruction + * Used by event-buffer.ts when buffer is destroyed + */ +export function recordBufferDestroyed(reason: string, hadPendingFlush: boolean): void { + try { + metrics.bufferDestroyed?.inc({ reason, had_pending_flush: hadPendingFlush ? 'true' : 'false' }) + } catch { + // Metrics not initialized, ignore silently + } +} + +/** + * Record final flush during buffer destruction + * Used by event-buffer.ts when buffer flushes remaining events on destroy + */ +export function recordBufferFinalFlush(): void { + try { + metrics.bufferFinalFlush?.inc({}) + } catch { + // Metrics not initialized, ignore silently + } +} + // ============================================ // Global Instance // ============================================