From 466161f87acf0b229e1ede02888451d5eb2e0e42 Mon Sep 17 00:00:00 2001 From: Lance Walters Date: Fri, 27 Nov 2026 01:29:18 -0500 Subject: [PATCH] monitoring(forgejo): make the actions_runners metric duplicate-proof The CNPG exporter answers the whole /metrics request with HTTP 500 when a custom query emits two rows with the same series name + label set, which also drops every pg_stat_*/cnpg_pg_* series (Prometheus target down). action_runner held three rows named siriussec-amd64 (one live, two soft-deleted tombstones from the runner renames), so cnpg_actions_runners_last_online_seconds was collected twice and the endpoint returned 500. - WHERE r.deleted IS NULL -> ignore Forgejo's soft-deleted runner rows. - GROUP BY r.name with max(last_online) -> the label is the name, so a duplicate name can never produce a duplicate series again; reporting the most recent poll per name is the intended semantic. - label cnpg.io/reload -> the instance manager re-reads the queries on change instead of needing a pod restart (or kubectl cnpg reload) before a query fix takes effect. --- apps/base/forgejo/pg-actions-queries.yaml | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/base/forgejo/pg-actions-queries.yaml b/apps/base/forgejo/pg-actions-queries.yaml index 0c937a0..6ad7492 100644 --- a/apps/base/forgejo/pg-actions-queries.yaml +++ b/apps/base/forgejo/pg-actions-queries.yaml @@ -38,6 +38,13 @@ kind: ConfigMap metadata: name: forgejo-actions-queries namespace: forgejo + labels: + # Tells the CNPG instance manager to re-read this ConfigMap on change. + # Without it a query fix (like the actions_runners dedupe below) does not + # reach the exporter until someone runs `kubectl cnpg reload` or the pod + # restarts — i.e. the fix appears to do nothing and /metrics keeps 500ing. + # This is a live reload of the queries, not a PostgreSQL restart. + cnpg.io/reload: "" data: queries: | # Queue depth: jobs handed to a runner that have not started, plus the age @@ -97,11 +104,28 @@ data: # How long ago each runner last polled Forgejo. This is the only signal that # says "the runner process is alive" independently of the pod being Ready — # a runner that cannot reach the forge stays Running but goes stale here. + # + # This query MUST return at most one row per `runner` label value. The + # exporter treats a duplicate label set as a programming error and answers + # the whole /metrics request with HTTP 500 — which also drops every + # pg_stat_*/cnpg_pg_* series, not just this one (incident t_290ff037). + # + # Two defences, both needed: + # * `r.deleted IS NULL` — Forgejo soft-deletes a runner (sets `deleted` to + # an epoch, keeps the row), so stale registrations survive in the table + # after a runner is removed. They are not real runners and stay hidden + # from the admin API, but a raw table read would see them. + # * `GROUP BY r.name` with `max(last_online)` — the label *is* the name, so + # two live rows that share a name (a re-registered runner, or one pod + # started from a copied runner.ini) would otherwise collide. Reporting + # the most recent poll per name is the semantic we want anyway. actions_runners: query: | SELECT r.name AS runner - , COALESCE(EXTRACT(EPOCH FROM pg_catalog.now())::bigint - NULLIF(r.last_online, 0), -1) AS last_online_seconds + , COALESCE(EXTRACT(EPOCH FROM pg_catalog.now())::bigint - NULLIF(max(r.last_online), 0), -1) AS last_online_seconds FROM action_runner r + WHERE r.deleted IS NULL + GROUP BY r.name metrics: - runner: usage: "LABEL"