Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

docs(metrics): list every exported series and bind the table to the renderer

Yudhi Armyndharisede0e50aa24d
diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.md
index a654b475f..f9a381d7a 100644
--- a/docs/10-devops-infrastructure.md
+++ b/docs/10-devops-infrastructure.md
@@ -900,6 +900,9 @@ export class MetricsService {
// openwa_messages_total{direction="outgoing"|"incoming"}
// openwa_messages_failed_total
}
+ // ... then the process-start counters (webhook delivery failures, session reconnect attempts
+ // and loop alerts), openwa_sessions_restricted, and the pacing refusals — see the table below
+ // for the full list. The real method also memoizes this string for METRICS_RENDER_TTL_MS.
return lines.join('\n') + '\n';
}
}
@@ -907,18 +910,32 @@ export class MetricsService {
**Exported metric names** (the complete set — nothing else is emitted):
-| Metric | Type | Labels | Meaning |
-| -------------------------------------- | ----- | ----------------------------------- | -------------------------------------------------------------------------------------------- |
-| `openwa_up` | gauge | — | Always `1` when scraped |
-| `openwa_process_uptime_seconds` | gauge | — | Process uptime |
-| `openwa_process_resident_memory_bytes` | gauge | — | RSS |
-| `openwa_process_heap_used_bytes` | gauge | — | V8 heap used |
-| `openwa_stats_available` | gauge | — | 1 when the database-derived series below could be read on this scrape, 0 when they could not |
-| `openwa_sessions_total` | gauge | — | Configured sessions |
-| `openwa_sessions_active` | gauge | — | READY (active) sessions |
-| `openwa_sessions` | gauge | `status` | Session count per status |
-| `openwa_messages_total` | gauge | `direction` (`incoming`/`outgoing`) | Current stored messages by direction |
-| `openwa_messages_failed_total` | gauge | — | Current messages in FAILED state |
+| Metric | Type | Labels | Meaning |
+| -------------------------------------------- | ------- | ----------------------------------- | -------------------------------------------------------------------------------------------- |
+| `openwa_up` | gauge | — | Always `1` when scraped |
+| `openwa_process_uptime_seconds` | gauge | — | Process uptime |
+| `openwa_process_resident_memory_bytes` | gauge | — | RSS |
+| `openwa_process_heap_used_bytes` | gauge | — | V8 heap used |
+| `openwa_stats_available` | gauge | — | 1 when the database-derived series below could be read on this scrape, 0 when they could not |
+| `openwa_sessions_total` | gauge | — | Configured sessions |
+| `openwa_sessions_active` | gauge | — | READY (active) sessions |
+| `openwa_sessions` | gauge | `status` | Session count per status |
+| `openwa_messages_total` | gauge | `direction` (`incoming`/`outgoing`) | Current stored messages by direction |
+| `openwa_messages_failed_total` | gauge | — | Current messages in FAILED state |
+| `openwa_webhook_delivery_failures_total` | counter | — | Webhook deliveries that terminally failed (all retries exhausted) since process start |
+| `openwa_session_reconnect_attempts_total` | counter | — | Reconnect attempts scheduled across all sessions since process start |
+| `openwa_session_reconnect_loop_alerts_total` | counter | — | Reconnect-loop alerts emitted since process start |
+| `openwa_sessions_restricted` | gauge | — | Sessions whose account WhatsApp is currently restricting |
+| `openwa_send_pacing_refusals_total` | counter | `reason` | Sends refused by the pacing governor since process start |
+
+Not every row appears on every scrape, and the difference matters when you write alerts. The
+database-derived series (`openwa_sessions*`, `openwa_messages*`) are **omitted entirely** when the
+overview cannot be read — `openwa_stats_available` is what tells the two cases apart, so alert on it
+rather than reading a missing series as zero. `openwa_send_pacing_refusals_total` appears only once
+the governor has refused something. For these, `absent()` is the correct alerting primitive.
+
+`src/common/docs-metrics-list.spec.ts` compares this table against the renderer's emission sites, so
+a metric added without a row fails CI.
> **The database-derived series can be absent.** `openwa_sessions_*`, `openwa_messages_*` and the per-status
> breakdown are read from the data database on each scrape. If that read fails — an outage, a statement
diff --git a/src/common/docs-metrics-list.spec.ts b/src/common/docs-metrics-list.spec.ts
new file mode 100644
index 000000000..86007483d
--- /dev/null
+++ b/src/common/docs-metrics-list.spec.ts
@@ -0,0 +1,73 @@
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+
+/**
+ * `docs/10-devops-infrastructure.md` introduces its metric table with "the complete set — nothing
+ * else is emitted". That is a claim about ABSENCE, and nothing bound it to the renderer: every
+ * metric added after the table was written kept the claim green while making it false. It had
+ * drifted to listing 10 of the 15 series `MetricsService` actually emits.
+ *
+ * An operator reads that table to decide what to alert on, so a missing row is not cosmetic — it is
+ * a signal nobody knows exists.
+ *
+ * This gate compares the table against the renderer's own emission sites. A metric added to the
+ * service without a row, a row left behind after the service stops emitting it, or a Type column
+ * that disagrees with the `# TYPE` line all fail here.
+ */
+describe('docs/10 metric table matches MetricsService', () => {
+ const read = (...parts: string[]): string => readFileSync(join(__dirname, '..', '..', ...parts), 'utf8');
+
+ /**
+ * Emitted series, read from the two forms the renderer uses:
+ * - `gauge('openwa_x', …)` — the helper pushes HELP/TYPE/value, always a gauge.
+ * - `# TYPE openwa_x counter|gauge` — the hand-written blocks.
+ * Both carry the type, so the doc's Type column is checked against the source rather than assumed.
+ */
+ const emitted = (): Map<string, string> => {
+ const source = read('src', 'modules', 'metrics', 'metrics.service.ts');
+ const found = new Map<string, string>();
+ for (const [, name] of source.matchAll(/\bgauge\(\s*'(openwa_[a-z_]+)'/g)) {
+ found.set(name, 'gauge');
+ }
+ for (const [, name, type] of source.matchAll(/# TYPE (openwa_[a-z_]+) (gauge|counter)/g)) {
+ found.set(name, type);
+ }
+ return found;
+ };
+
+ /** The table rows: `| \`openwa_x\` | type | labels | meaning |`. */
+ const documented = (): Map<string, string> => {
+ const doc = read('docs', '10-devops-infrastructure.md');
+ const start = doc.indexOf('**Exported metric names**');
+ expect(start).toBeGreaterThan(-1);
+ const table = doc.slice(start);
+ const rows = new Map<string, string>();
+ for (const line of table.split('\n')) {
+ if (!line.startsWith('|')) {
+ if (rows.size > 0) break; // past the table
+ continue;
+ }
+ const cells = line.split('|').map(cell => cell.trim());
+ const name = cells[1]?.match(/^`(openwa_[a-z_]+)`$/)?.[1];
+ if (name) rows.set(name, cells[2]);
+ }
+ return rows;
+ };
+
+ // Positive control. Both readers are regex-based, and a regex that matches nothing would make
+ // every comparison below pass vacuously — two empty sets are equal. Fail loudly instead.
+ it('both readers actually read something', () => {
+ expect(emitted().size).toBeGreaterThanOrEqual(10);
+ expect(emitted().get('openwa_up')).toBe('gauge');
+ expect(documented().size).toBeGreaterThanOrEqual(10);
+ expect(documented().get('openwa_up')).toBe('gauge');
+ });
+
+ it('lists exactly the series the renderer emits', () => {
+ expect([...documented().keys()].sort()).toEqual([...emitted().keys()].sort());
+ });
+
+ it('gives each series the type the renderer declares', () => {
+ expect(Object.fromEntries([...documented()].sort())).toEqual(Object.fromEntries([...emitted()].sort()));
+ });
+});