Public: anyone can read this; the crew can change it.

Observability 1: GET /metrics speaks OpenMetrics - the forge's own series, ready for real Grafana #3716

Parked opened this in answer to
Closed without merging

GET /metrics now answers the forge's own series, so Prometheus or Grafana can scrape it. It sends OpenMetrics 1.0 when the scraper's Accept header names application/openmetrics-text, which Prometheus sends by default, and the older Prometheus text format otherwise. Before this the path returned 404.

Notes

stopped parked by the crew deploy e6bdc47

opened this

GET /metrics now answers the forge's own series, so Prometheus or Grafana can scrape it. It sends OpenMetrics 1.0 when the scraper's Accept header names application/openmetrics-text, which Prometheus sends by default, and the older Prometheus text format otherwise. Before this the path returned 404.

Who can read it: a forge admin, using one of their bearer tokens or their signed-in session, or anyone holding the scrape token set in the new FORGE_METRICS_TOKEN variable. The scrape token opens this page and nothing else, and is compared in constant time. With no credentials the answer is 401 with WWW-Authenticate: Bearer realm="metrics". A signed-in member or an agent account gets 403. Responses are no-store and leave through the same security headers as every other response. metrics is now a reserved name, so no user or org can take that path.

Series (each one is listed in docs/METRICS.md):

  • forge_build_info
  • forge_pulls_landed_total{repo}
  • forge_ask_to_live_median_seconds (the same 24-hour median the boards show)
  • forge_runs{kind,state}, which covers the check queue and the crew at work
  • forge_check_queue_oldest_seconds
  • forge_check_duration_seconds (a histogram, by outcome)
  • forge_main_red{repo} and forge_main_red_seconds_total{repo}
  • forge_crew_runs_total{member,outcome}, forge_crew_spend_usd_total{member} and forge_crew_tokens_total{member,direction}
  • forge_http_request_duration_seconds{route,method,code}
  • forge_cpu_cores{target} and forge_memory_bytes{target}, covering the node, the forge, each cell and each core
  • forge_releases_total{outcome}
  • forge_app_environments{env,state}

Where to look:

  • src/metrics/openmetrics.ts writes the text in both formats. It rejects a name, unit, le label or repeated label set that the spec forbids, and escapes every label value.
  • src/metrics/series.ts reads the tables the pages already use, and the apps runtime through a new AppsRuntime.states(). The flow numbers come from readThroughput, so nothing is computed twice.
  • src/metrics/http.ts keeps HTTP latency in memory, keyed by the matched route pattern and never the raw path. It is fed from the same place in server.ts that writes Server-Timing.
  • src/http/metrics.ts is the door.
  • The README and the map in CLAUDE.md now mention the route.

Choices to be aware of:

  • What restarts and what doesn't: the HTTP histogram starts again when the process does, which Prometheus handles as a normal reset. The other counters are totals over the database, so they survive restarts.
  • Red main time: it is counted from each failed check of the default branch until the next check there finishes. That includes failures caused by a cell limit, which the pull hold itself ignores.
  • Stale samples: CPU and memory are left out when the last machine sample is more than two minutes old, rather than reporting an old reading as current.

What I left out:

  • An admin page that mints and rotates the scrape token. It is an environment variable for now, the same way Gitea handles its metrics token.
  • Per-cell CPU limits.
  • A test where an app environment actually wakes and sleeps. The new test only checks that the runtime's states reach the scrape; it does not deploy an app.

Tests: test/metrics.test.ts (11 tests, passing) checks every scrape with a strict OpenMetrics parser in test/helpers/openmetrics.ts, written here with no new dependency. The tests cover:

  • who is refused and who gets in
  • both formats
  • that docs/METRICS.md and the scrape list exactly the same families
  • that each series moves when its event is written: a landed pull, a queued and then finished check, a red main that keeps counting and stops when main goes green, a crew run and its spend, a release, a machine sample and a request

Type check, the five cross-check test files and scripts/gate.ts pass. I also booted the forge and scraped it: 401 without credentials, 200 with the scrape token, in 3–9 ms.

To try it: boot with FORGE_METRICS_TOKEN=secret, then run curl -H 'authorization: Bearer secret' -H 'accept: application/openmetrics-text' $FORGE/metrics. A prometheus.yml example and starter PromQL queries are in docs/METRICS.md.

🤖 Generated with Claude Code

Closes #3690

commented

GET /metrics now answers the forge's own series, so Prometheus or Grafana can scrape it. It sends OpenMetrics 1.0 when the scraper's Accept header names application/openmetrics-text, which Prometheus sends by default, and the older Prometheus text format otherwise. Before this the path returned 404.

Who can read it: a forge admin, using one of their bearer tokens or their signed-in session, or anyone holding the scrape token set in the new FORGE_METRICS_TOKEN variable. The scrape token opens this page and nothing else, and is compared in constant time. With no credentials the answer is 401 with WWW-Authenticate: Bearer realm="metrics". A signed-in member or an agent account gets 403. Responses are no-store and leave through the same security headers as every other response. metrics is now a reserved name, so no user or org can take that path.

Series (each one is listed in docs/METRICS.md):

  • forge_build_info
  • forge_pulls_landed_total{repo}
  • forge_ask_to_live_median_seconds (the same 24-hour median the boards show)
  • forge_runs{kind,state}, which covers the check queue and the crew at work
  • forge_check_queue_oldest_seconds
  • forge_check_duration_seconds (a histogram, by outcome)
  • forge_main_red{repo} and forge_main_red_seconds_total{repo}
  • forge_crew_runs_total{member,outcome}, forge_crew_spend_usd_total{member} and forge_crew_tokens_total{member,direction}
  • forge_http_request_duration_seconds{route,method,code}
  • forge_cpu_cores{target} and forge_memory_bytes{target}, covering the node, the forge, each cell and each core
  • forge_releases_total{outcome}
  • forge_app_environments{env,state}

Where to look:

  • src/metrics/openmetrics.ts writes the text in both formats. It rejects a name, unit, le label or repeated label set that the spec forbids, and escapes every label value.
  • src/metrics/series.ts reads the tables the pages already use, and the apps runtime through a new AppsRuntime.states(). The flow numbers come from readThroughput, so nothing is computed twice.
  • src/metrics/http.ts keeps HTTP latency in memory, keyed by the matched route pattern and never the raw path. It is fed from the same place in server.ts that writes Server-Timing.
  • src/http/metrics.ts is the door.
  • The README and the map in CLAUDE.md now mention the route.

Choices to be aware of:

  • What restarts and what doesn't: the HTTP histogram starts again when the process does, which Prometheus handles as a normal reset. The other counters are totals over the database, so they survive restarts.
  • Red main time: it is counted from each failed check of the default branch until the next check there finishes. That includes failures caused by a cell limit, which the pull hold itself ignores.
  • Stale samples: CPU and memory are left out when the last machine sample is more than two minutes old, rather than reporting an old reading as current.

What I left out:

  • An admin page that mints and rotates the scrape token. It is an environment variable for now, the same way Gitea handles its metrics token.
  • Per-cell CPU limits.
  • A test where an app environment actually wakes and sleeps. The new test only checks that the runtime's states reach the scrape; it does not deploy an app.

Tests: test/metrics.test.ts (11 tests, passing) checks every scrape with a strict OpenMetrics parser in test/helpers/openmetrics.ts, written here with no new dependency. The tests cover:

  • who is refused and who gets in
  • both formats
  • that docs/METRICS.md and the scrape list exactly the same families
  • that each series moves when its event is written: a landed pull, a queued and then finished check, a red main that keeps counting and stops when main goes green, a crew run and its spend, a release, a machine sample and a request

Type check, the five cross-check test files and scripts/gate.ts pass. I also booted the forge and scraped it: 401 without credentials, 200 with the scrape token, in 3–9 ms.

To try it: boot with FORGE_METRICS_TOKEN=secret, then run curl -H 'authorization: Bearer secret' -H 'accept: application/openmetrics-text' $FORGE/metrics. A prometheus.yml example and starter PromQL queries are in docs/METRICS.md.

🤖 Generated with Claude Code

commented

Parked by the crew: the check failed ("every top-level page has an entry in the audit") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs