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:
@@ -4,7 +4,7 @@ import { randomBytes } from 'crypto'
|
||||
import Long from 'long'
|
||||
import { proto } from '../../WAProto/index.js'
|
||||
import { DEFAULT_CACHE_TTLS, KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import { metrics } from '../Utils/prometheus-metrics.js'
|
||||
import { metrics, recordMessageReceived, recordHistorySyncMessages } from '../Utils/prometheus-metrics.js'
|
||||
import type {
|
||||
GroupParticipant,
|
||||
MessageReceiptType,
|
||||
@@ -1387,6 +1387,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
if (msg.key.id && msg.key.remoteJid) {
|
||||
logMessageReceived(msg.key.id, msg.key.remoteJid)
|
||||
}
|
||||
|
||||
// Record message received metric
|
||||
const msgContent = msg.message
|
||||
const msgType = msgContent?.conversation ? 'text'
|
||||
: msgContent?.imageMessage ? 'image'
|
||||
: msgContent?.videoMessage ? 'video'
|
||||
: msgContent?.audioMessage ? 'audio'
|
||||
: msgContent?.documentMessage ? 'document'
|
||||
: msgContent?.stickerMessage ? 'sticker'
|
||||
: msgContent?.reactionMessage ? 'reaction'
|
||||
: 'other'
|
||||
recordMessageReceived(msgType)
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error({ error, node: binaryNodeToString(node) }, 'error in handling message')
|
||||
|
||||
@@ -56,6 +56,7 @@ import {
|
||||
S_WHATSAPP_NET
|
||||
} from '../WABinary'
|
||||
import { USyncQuery, USyncUser } from '../WAUSync'
|
||||
import { recordMessageSent, recordMessageRetry, recordMessageFailure } from '../Utils/prometheus-metrics'
|
||||
import { makeNewsletterSocket } from './newsletter'
|
||||
|
||||
export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
@@ -1038,6 +1039,17 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
// Log with [BAILEYS] prefix
|
||||
logMessageSent(msgId, destinationJid)
|
||||
|
||||
// Record message sent metric
|
||||
const msgType = message.conversation ? 'text'
|
||||
: message.imageMessage ? 'image'
|
||||
: message.videoMessage ? 'video'
|
||||
: message.audioMessage ? 'audio'
|
||||
: message.documentMessage ? 'document'
|
||||
: message.stickerMessage ? 'sticker'
|
||||
: message.reactionMessage ? 'reaction'
|
||||
: 'other'
|
||||
recordMessageSent(msgType)
|
||||
|
||||
// Add message to retry cache if enabled
|
||||
if (messageRetryManager && !participant) {
|
||||
messageRetryManager.addRecentMessage(destinationJid, msgId, message)
|
||||
|
||||
+14
-1
@@ -54,7 +54,12 @@ import {
|
||||
jidEncode,
|
||||
S_WHATSAPP_NET
|
||||
} from '../WABinary'
|
||||
import { recordConnectionError } from '../Utils/prometheus-metrics'
|
||||
import {
|
||||
recordConnectionError,
|
||||
recordConnectionAttempt,
|
||||
incrementActiveConnections,
|
||||
decrementActiveConnections
|
||||
} from '../Utils/prometheus-metrics'
|
||||
import { BinaryInfo } from '../WAM/BinaryInfo.js'
|
||||
import { USyncQuery, USyncUser } from '../WAUSync/'
|
||||
import { WebSocketClient } from './Client'
|
||||
@@ -764,8 +769,12 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
errorType = `error_${statusCode}`
|
||||
}
|
||||
recordConnectionError(errorType)
|
||||
recordConnectionAttempt('failure')
|
||||
}
|
||||
|
||||
// Decrement active connections
|
||||
decrementActiveConnections()
|
||||
|
||||
clearInterval(keepAliveReq)
|
||||
clearTimeout(qrTimer)
|
||||
|
||||
@@ -1080,6 +1089,10 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
|
||||
ev.emit('connection.update', { connection: 'open' })
|
||||
|
||||
// Record successful connection metrics
|
||||
recordConnectionAttempt('success')
|
||||
incrementActiveConnections()
|
||||
|
||||
if (node.attrs.lid && authState.creds.me?.id) {
|
||||
const myLID = node.attrs.lid
|
||||
process.nextTick(async () => {
|
||||
|
||||
Reference in New Issue
Block a user