-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathwebhookMetrics.ts
More file actions
80 lines (72 loc) · 2.99 KB
/
Copy pathwebhookMetrics.ts
File metadata and controls
80 lines (72 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Counter, Registry } from 'prom-client';
import {
DlqOperationSchema,
DlqReplayOutcomeSchema,
DLQ_OPERATIONS,
DLQ_REPLAY_OUTCOMES,
} from '../observability/metrics-validation';
// ---------------------------------------------------------------------------
// Isolated Registry for WebhookMetrics DLQ counters
// ---------------------------------------------------------------------------
/**
* Isolated prom-client Registry for webhook DLQ metrics.
* Using a dedicated registry prevents duplicate metric registration errors
* in test environments and allows test isolation via registry.clear().
*/
export const webhookDlqRegistry = new Registry();
// ---------------------------------------------------------------------------
// Existing Metrics (From Project Specifications)
// ---------------------------------------------------------------------------
/**
* Total count of standard webhook DLQ lifecycle operations.
* Labels cover: 'enqueue', 'drop_overflow', 'drop_poison'
*/
export const webhookDlqOperationsTotal = new Counter({
name: 'webhook_dlq_operations_total',
help: 'Total number of webhook DLQ core operations.',
labelNames: ['operation'],
registers: [webhookDlqRegistry],
});
/**
* Helper to increment standard DLQ storage lifecycle events.
* @param operation - The storage operation type executed
* @throws {TypeError} when operation is not a recognised DLQ operation string
*/
export function incrementDlqOperation(operation: 'enqueue' | 'drop_overflow' | 'drop_poison'): void {
const result = DlqOperationSchema.safeParse(operation);
if (!result.success) {
throw new TypeError(
`Invalid DLQ operation: ${JSON.stringify(operation)}. ` +
`Must be one of: ${DLQ_OPERATIONS.join(', ')}`,
);
}
webhookDlqOperationsTotal.labels(result.data).inc();
}
// ---------------------------------------------------------------------------
// New Issue #256 Metrics (DLQ Idempotent Replays)
// ---------------------------------------------------------------------------
/**
* Total tracking counts of manual or batch DLQ replay operations.
* Labels cover: 'success', 'failed', 'idempotent_noop', 'error'
*/
export const webhookDlqReplaysTotal = new Counter({
name: 'webhook_dlq_replays_total',
help: 'Total tracking counts of webhook DLQ manual or batch replay jobs executed.',
labelNames: ['outcome'],
registers: [webhookDlqRegistry],
});
/**
* Helper to increment metrics counters following a DLQ replay attempt.
* @param outcome - The resulting resolution path of the replay action
* @throws {TypeError} when outcome is not a recognised DLQ replay outcome string
*/
export function incrementDlqReplay(outcome: 'success' | 'failed' | 'idempotent_noop' | 'error'): void {
const result = DlqReplayOutcomeSchema.safeParse(outcome);
if (!result.success) {
throw new TypeError(
`Invalid DLQ replay outcome: ${JSON.stringify(outcome)}. ` +
`Must be one of: ${DLQ_REPLAY_OUTCOMES.join(', ')}`,
);
}
webhookDlqReplaysTotal.labels(result.data).inc();
}