Ensure metrics exist before collecting them

GitOrigin-RevId: 811301ddebb794341dccc0f27954c66dc5c44aa4
This commit is contained in:
Andrew Rumble
2025-03-24 10:48:58 +00:00
committed by Copybot
parent 47d8e59938
commit 800b151024
@@ -13,11 +13,16 @@ import { setTimeout } from 'node:timers/promises'
const MS_PER_30_DAYS = 30 * 24 * 60 * 60 * 1000 const MS_PER_30_DAYS = 30 * 24 * 60 * 60 * 1000
const METRICS = { const failureCounter = new metrics.prom.Counter({
backup_project_verification_failed: 'backup_project_verification_failed', name: 'backup_project_verification_failed',
backup_project_verification_succeeded: help: 'Number of projects that failed verification',
'backup_project_verification_succeeded', labelNames: ['name'],
} })
const successCounter = new metrics.prom.Counter({
name: 'backup_project_verification_succeeded',
help: 'Number of projects that succeeded verification',
})
let WRITE_METRICS = false let WRITE_METRICS = false
@@ -42,8 +47,7 @@ function handleVerificationError(error, historyId) {
const name = error instanceof Error ? error.name : 'UnknownError' const name = error instanceof Error ? error.name : 'UnknownError'
logger.error({ historyId, error, name }, 'error verifying project backup') logger.error({ historyId, error, name }, 'error verifying project backup')
WRITE_METRICS && WRITE_METRICS && failureCounter.inc({ name })
metrics.inc(METRICS.backup_project_verification_failed, 1, { name })
return name return name
} }
@@ -97,8 +101,7 @@ async function verifyProjectsFromCursor(
try { try {
await verifyProjectWithErrorContext(historyId) await verifyProjectWithErrorContext(historyId)
logger.debug({ historyId }, 'verified project backup successfully') logger.debug({ historyId }, 'verified project backup successfully')
WRITE_METRICS && WRITE_METRICS && successCounter.inc()
metrics.inc(METRICS.backup_project_verification_succeeded)
verified++ verified++
} catch (error) { } catch (error) {
const errorType = handleVerificationError(error, historyId) const errorType = handleVerificationError(error, historyId)