Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

docs(metrics): include the unprefixed HTTP series and bind the render composition

Yudhi Armyndharis9b223da69220
diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.md
index f9a381d7a..64abbed14 100644
--- a/docs/10-devops-infrastructure.md
+++ b/docs/10-devops-infrastructure.md
@@ -884,7 +884,9 @@ export class MetricsService {
) {}
async render(): Promise<string> {
- const overview = await this.statsService.getOverview();
+ // Guarded: an unreachable data database must cost the DB-derived series, not the whole scrape.
+ // `overview` is null on failure, which is what openwa_stats_available reports.
+ const overview = await this.readOverviewOrNull();
const mem = process.memoryUsage();
const lines: string[] = [];
// ... gauge() helper pushes `# HELP` / `# TYPE` / value lines ...
@@ -910,23 +912,29 @@ 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 |
-| `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 |
+| 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 |
+| `http_requests_total` | counter | `method`, `route`, `status` | HTTP requests served, by method, route and status |
+| `http_request_duration_seconds` | histogram | `method`, `route` | HTTP request duration (`_bucket` / `_sum` / `_count`) |
+
+The last two are deliberately **unprefixed** so a generic RED dashboard or alert rule matches them
+without knowing anything about OpenWA. They come from `src/common/metrics/request-metrics.ts`, which
+`render()` splices into the same output.
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
@@ -934,8 +942,11 @@ overview cannot be read — `openwa_stats_available` is what tells the two cases
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.
+`src/common/docs-metrics-list.spec.ts` compares this table against the metric names declared in
+`metrics.service.ts` and `request-metrics.ts`, and checks that every helper `render()` splices in is
+one of the files it reads. A series added to either file without a row here fails CI; one emitted
+from a module that is neither — and not spliced through `lines.push(...renderX())` — would not be
+seen, so keep new renderers on that composition.
> **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
index 86007483d..ca8c65a5e 100644
--- a/src/common/docs-metrics-list.spec.ts
+++ b/src/common/docs-metrics-list.spec.ts
@@ -18,23 +18,53 @@ 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.
+ * Files whose lines end up in one scrape. `render()` composes its output from helpers in other
+ * modules, so scanning the service alone missed the unprefixed HTTP RED series entirely — and a
+ * gate that cannot see a series cannot bind it.
+ */
+ const RENDER_SOURCES = [
+ ['src', 'modules', 'metrics', 'metrics.service.ts'],
+ ['src', 'common', 'metrics', 'request-metrics.ts'],
+ ];
+
+ /**
+ * Emitted series, read from the two forms the renderers use:
+ * - `gauge('name', …)` — the helper pushes HELP/TYPE/value, always a gauge.
+ * - `# TYPE name counter|gauge` — the hand-written blocks.
+ * Deliberately NOT restricted to the `openwa_` prefix: two series are conventionally unprefixed so
+ * generic RED dashboards match them, and a prefix filter is exactly how they escaped this gate.
*/
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);
+ for (const parts of RENDER_SOURCES) {
+ const source = read(...parts);
+ for (const [, name] of source.matchAll(/\bgauge\(\s*'([a-z][a-z0-9_]*)'/g)) {
+ found.set(name, 'gauge');
+ }
+ for (const [, name, type] of source.matchAll(/# TYPE ([a-z][a-z0-9_]*) (gauge|counter|histogram)/g)) {
+ found.set(name, type);
+ }
}
return found;
};
+ /**
+ * The composition itself. RENDER_SOURCES is a hand-kept list, and a third helper spliced into
+ * `render()` would escape this gate the same way the HTTP series did — silently, because the
+ * comparison would still pass on the files it does know. Every `...renderX()` splice must resolve
+ * to a module this spec reads.
+ */
+ it('reads every module whose lines render() splices in', () => {
+ const service = read('src', 'modules', 'metrics', 'metrics.service.ts');
+ const splices = [...service.matchAll(/lines\.push\(\.\.\.(\w+)\(/g)].map(m => m[1]);
+ expect(splices.length).toBeGreaterThan(0); // control: the pattern must match something
+
+ const scanned = RENDER_SOURCES.map(parts => read(...parts)).join('\n');
+ for (const helper of splices) {
+ expect(scanned).toContain(`export function ${helper}`);
+ }
+ });
+
/** The table rows: `| \`openwa_x\` | type | labels | meaning |`. */
const documented = (): Map<string, string> => {
const doc = read('docs', '10-devops-infrastructure.md');
@@ -48,7 +78,7 @@ describe('docs/10 metric table matches MetricsService', () => {
continue;
}
const cells = line.split('|').map(cell => cell.trim());
- const name = cells[1]?.match(/^`(openwa_[a-z_]+)`$/)?.[1];
+ const name = cells[1]?.match(/^`([a-z][a-z0-9_]*)`$/)?.[1];
if (name) rows.set(name, cells[2]);
}
return rows;