homek8s/apps/base/forgejo/pg-actions-queries.yaml
Lance Walters 466161f87a 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.
2026-11-27 01:29:18 -05:00

135 lines
6.4 KiB
YAML

# 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_<name>_<column>` 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)"