# Custom CNPG monitoring queries for Forgejo's Actions tables. # # Why this ConfigMap exists: Forgejo's Actions queue lives in PostgreSQL, not in # any exporter. `action_run_job` is the only place that knows how many jobs are # waiting for a runner and how the last N runs ended, so the runner dashboard # reads it through CNPG's built-in metrics exporter (`:9187`) instead of # inventing a new exporter. # # How it is wired: `Cluster/pg` lists this ConfigMap under # `spec.monitoring.customQueriesConfigMap` (alongside the operator's own # `cnpg-default-monitoring`), so every query here runs on the cluster's *main* # database — `forgejo` for this cluster, per CNPG's initdb.database rule — and is # exported as `cnpg__` on the instance's metrics port. # # Privileges: CNPG runs custom queries as the `cnpg_metrics_exporter` role # (pg_monitor, NOT superuser). That role cannot read Forgejo's tables until it # is granted, and the grant is a database-side step that cannot live in this # file. When the dashboard shows an empty queue metric, run: # # GRANT USAGE ON SCHEMA public TO cnpg_metrics_exporter; # GRANT SELECT ON action_run_job, action_task, # action_runner TO cnpg_metrics_exporter; # # (see docs/monitoring-dashboards.md for the exact kubectl one-liner). # # Semantics, verified against this instance rather than guessed (task t_c45b2d73): # action_run_job.status / action_run.status: 1 = success, 2 = failure, # 3 = cancelled, 4 = skipped. Cross-checked against # GET /api/v1/repos/sirius/onionwire/actions/runs, where DB run 1189 # (status 1) reports conclusion "success" and run 1191 (status 2) "failure". # A job that has not started yet carries started = 0; a job that has not # finished carries stopped = 0. Rows for skipped jobs never start, never stop # and are kept forever, so queue depth deliberately excludes status 4 — # otherwise the queue would read "3" at all times with nothing queued. --- apiVersion: v1 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 # of the oldest of them. Always returns exactly one row, so the panel shows # a hard 0 instead of "No data" when the queue is empty. actions_queue: query: | SELECT count(*) AS depth , COALESCE(max(EXTRACT(EPOCH FROM pg_catalog.now())::bigint - NULLIF(j.created, 0)), 0) AS oldest_pending_seconds FROM action_run_job j WHERE j.started = 0 AND j.stopped = 0 AND j.status <> 4 metrics: - depth: usage: "GAUGE" description: "Jobs assigned to a runner that have not started yet" - oldest_pending_seconds: usage: "GAUGE" description: "Age in seconds of the oldest not-yet-started job" # Jobs currently executing (a runner picked them up and has not finished). actions_running: query: | SELECT count(*) AS total FROM action_run_job j WHERE j.started > 0 AND j.stopped = 0 metrics: - total: usage: "GAUGE" description: "Jobs currently running on a runner" # Job outcomes inside a rolling window. A rolling count (not a counter): # Forgejo prunes old rows, so rate() over a row count would lie; asking for # "stopped within the last hour/day" is always right. actions_jobs_window: query: | SELECT w.label AS window , s.status AS status , COALESCE(( SELECT count(*) FROM action_run_job j WHERE j.status = s.status AND j.stopped > (EXTRACT(EPOCH FROM pg_catalog.now())::bigint - w.secs) ), 0) AS total FROM (VALUES ('1h', 3600), ('24h', 86400)) AS w(label, secs) CROSS JOIN (VALUES (0), (1), (2), (3), (4)) AS s(status) metrics: - window: usage: "LABEL" description: "Rolling window: 1h or 24h" - status: usage: "LABEL" description: "1 success, 2 failure, 3 cancelled, 4 skipped" - total: usage: "GAUGE" description: "Jobs that finished inside the window with this status" # 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(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" description: "Runner name as registered with Forgejo" - last_online_seconds: usage: "GAUGE" description: "Seconds since the runner last polled (-1 = never)"