feat: implement WhatsApp connection and message metrics
Added tracking for all WhatsApp-related metrics: Connection metrics: - active_connections: Gauge tracking active connections (inc on open, dec on close) - connection_attempts_total: Counter for connection attempts (success/failure) Message metrics: - messages_sent_total: Counter with type label (text, image, video, audio, etc) - messages_received_total: Counter with type label - history_sync_messages_total: Counter for history sync messages Added helper functions in prometheus-metrics.ts: - incrementActiveConnections(), decrementActiveConnections(), setActiveConnections() - recordConnectionAttempt(status) - recordMessageSent(type), recordMessageReceived(type) - recordMessageRetry(type), recordMessageFailure(type, reason) - setMessagesQueued(count, priority) - recordHistorySyncMessages(count)
This commit is contained in:
@@ -16,7 +16,7 @@ import type {
|
||||
WAMessage,
|
||||
WAMessageKey
|
||||
} from '../Types'
|
||||
import { metrics } from './prometheus-metrics.js'
|
||||
import { metrics, recordHistorySyncMessages } from './prometheus-metrics.js'
|
||||
import { WAMessageStubType } from '../Types'
|
||||
import { getContentType, normalizeMessageContent } from '../Utils/messages'
|
||||
import {
|
||||
@@ -353,6 +353,11 @@ const processMessage = async (
|
||||
isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
|
||||
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
||||
})
|
||||
|
||||
// Record history sync metrics
|
||||
if (data.messages?.length) {
|
||||
recordHistorySyncMessages(data.messages.length)
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
|
||||
@@ -1971,6 +1971,120 @@ export function updateAdaptiveMetrics(eventRate: number, isHealthy: boolean): vo
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// WhatsApp Connection & Message Metrics
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* Increment active connections gauge
|
||||
*/
|
||||
export function incrementActiveConnections(): void {
|
||||
try {
|
||||
metrics.activeConnections?.inc({})
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement active connections gauge
|
||||
*/
|
||||
export function decrementActiveConnections(): void {
|
||||
try {
|
||||
metrics.activeConnections?.dec({})
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set active connections to specific value
|
||||
*/
|
||||
export function setActiveConnections(count: number): void {
|
||||
try {
|
||||
metrics.activeConnections?.set({}, count)
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a connection attempt
|
||||
*/
|
||||
export function recordConnectionAttempt(status: 'success' | 'failure'): void {
|
||||
try {
|
||||
metrics.connectionAttempts?.inc({ status })
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a message sent
|
||||
*/
|
||||
export function recordMessageSent(type: string = 'text'): void {
|
||||
try {
|
||||
metrics.messagesSent?.inc({ type })
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a message received
|
||||
*/
|
||||
export function recordMessageReceived(type: string = 'text'): void {
|
||||
try {
|
||||
metrics.messagesReceived?.inc({ type })
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a message retry attempt
|
||||
*/
|
||||
export function recordMessageRetry(type: string = 'text'): void {
|
||||
try {
|
||||
metrics.messageRetries?.inc({ type })
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a message failure
|
||||
*/
|
||||
export function recordMessageFailure(type: string = 'text', reason: string = 'unknown'): void {
|
||||
try {
|
||||
metrics.messageFailures?.inc({ type, reason })
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update messages queued gauge
|
||||
*/
|
||||
export function setMessagesQueued(count: number, priority: string = 'normal'): void {
|
||||
try {
|
||||
metrics.messagesQueued?.set({ priority }, count)
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record history sync messages
|
||||
*/
|
||||
export function recordHistorySyncMessages(count: number = 1): void {
|
||||
try {
|
||||
metrics.historySyncMessages?.inc({}, count)
|
||||
} catch {
|
||||
// Metrics not initialized, ignore silently
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Global Instance
|
||||
// ============================================
|
||||
|
||||
Reference in New Issue
Block a user