Fix metric wiring gaps audited against live processor output

Several Phase 1 metrics were registered in observability/metrics.ts but
either unwired at the call sites or wired with wrong counts. Production
output showed 11 records ingested per logs but only 4 in metrics. The
fixes below align metric values with actual hot-path activity.

Wiring gaps closed (consumer.ts):
- processor_consumer_reads_total{result=ok|empty|error} — was registered
  but never inc'd. Now fires on each XREADGROUP outcome.
- processor_consumer_records_total — was registered but never inc'd.
  Now fires once per XREADGROUP, with the entry count.

Counts corrected (writer.ts):
- processor_position_writes_total{status} — was inc'd unconditionally
  by 1 per chunk for each of inserted/duplicate. Now inc'd by the
  actual per-status count, and only when count > 0.
- processor_position_writes_total{status='failed'} — was inc'd by 1
  per failed chunk. Now inc'd by chunk.length so every failed record
  is counted.

Counts corrected (main.ts):
- processor_acks_total — was inc'd by 1 per non-empty batch. Now
  inc'd by ackIds.length so every ACK'd ID is counted.

Wiring gap closed (state.ts):
- processor_device_state_evictions_total — internal `evicted` counter
  existed but was never published to metrics. createDeviceStateStore
  now accepts a Metrics injection and inc's on each eviction.

Metrics interface extended (types.ts, metrics.ts):
- Metrics.inc gained an optional third `value` parameter (defaults to 1)
  for batched increments. dispatchInc passes it through to prom-client's
  Counter.inc(labels, value).

Tests updated to reflect the new third arg and the state.ts factory's
new metrics parameter. Total 134 unit tests passing (no count change —
existing tests adjusted, no new tests added; the real verification is
on stage where the metrics are now meaningful again).
This commit is contained in:
2026-05-01 11:38:25 +02:00
parent 4a9f55cdf0
commit d758c211ae
8 changed files with 75 additions and 38 deletions
+2 -2
View File
@@ -65,7 +65,7 @@ async function main(): Promise<void> {
const redis: Redis = await connectRedis(config.REDIS_URL, logger);
// 5. Build pipeline components
const state = createDeviceStateStore(config, logger);
const state = createDeviceStateStore(config, logger, metrics);
const writer = createWriter(pool, config, logger, metrics);
// 6. Postgres health check — background cached SELECT 1 for /readyz.
@@ -122,7 +122,7 @@ async function main(): Promise<void> {
.map((r) => r.id);
if (ackIds.length > 0) {
metrics.inc('processor_acks_total');
metrics.inc('processor_acks_total', undefined, ackIds.length);
}
return ackIds;