Files
coder/scripts/metricsdocgen/README.md
T
Nick Vigilante 72e31ffb4c fix: prevent Prometheus metrics reference drift (#29314)
## Summary

The Prometheus metrics reference merged scanner output with a
hand-maintained file, and changes to the hand-maintained half could not
fail CI. 66 of the 76 static families had no scanner coverage: four
files sat in a skip list pending `WrapRegistererWithPrefix` support, and
the `aibridge/` tree was outside the scan roots entirely. Rename or add
a metric in any of them and `make gen` regenerated the page unchanged,
so `check_unstaged.sh` passed while the published table quietly went
wrong.

The scanner now resolves the prefixes a registerer applies:

- Indexes `WrapRegistererWithPrefix` and `NewMetricAliasRegisterer` call
sites, resolving the prefix from a string literal or an imported
constant.
- Maps that prefix to the package whose constructor receives the wrapped
registerer, covering the two shapes the codebase uses: a constructor
that **forwards** its registerer to another package
(`aibridge.NewMetrics` to `aibridge/metrics.NewMetrics`), and a
collector **registered directly** against the wrapped registerer
(`gatewayRegisterer.MustRegister(keypool.NewStateCollector(...))`).
- Indexes only the canonical prefix of an alias registerer. The legacy
`coder_aibridged_` and `coder_aibridgeproxyd_` aliases are deprecated
per AIGOV-447 and should not be taught in the reference.

With prefixes resolved: skip list deleted, `aibridge` added to the scan
roots, and all 29 `coder_ai_gateway_*` families removed from the static
file, which now holds only genuine externals (`go_*`, `process_*`, and
metrics built dynamically). `enterprise/scaletest/agentfake` is excluded
deliberately: its fake-agent metrics are not part of a deployment's
surface, so documenting them would send operators looking for series
they will never see.

**Also fixes two extraction and merge gaps.** Label lists written as
`append(baseLabels, "status", "route")` now resolve, and any
unresolvable element returns no labels rather than a partial list.
Static runtime-label overrides now replace generated labels only:
generated name, type, and HELP remain authoritative.

## Verification

The prefix-scanning refactor itself preserves the existing AI Gateway
rows. However, the page is **not byte-identical** to `main` because this
round fixes two already-published stale HELP strings. Its SHA-256
changes from
`7ced1e7e9f2d4caf5e3b5e484ef4af14f955b8b4cd1a14fe81360bbdd49bb150` to
`a68774275a3b21d5bae65024cc8ee65b52c51e52786c9aea45e3981e4e7f1643`.

| Metric | Before | After |
| --- | --- | --- |
| `coderd_agentstats_connection_median_latency_seconds` | The median
agent connection latency | The median agent connection latency in
seconds |
| `coderd_agentstats_startup_script_seconds` | The number of seconds the
startup script took to execute. | Amount of time taken to run the
startup script in seconds. |

- `go test ./scripts/metricsdocgen/...` covers prefix resolution from a
literal and imported constant, alias-registerer argument selection,
collector registration, forwarder propagation, scan-to-emit prefix
application, all-or-nothing label extraction, static-label merging, and
the exclusion predicate.
- `golangci-lint run ./scripts/metricsdocgen/...` and full `make
pre-commit` pass.

## What this does not fix

Ten metrics are still skipped for having no `HELP` text, with a warning
to stderr rather than an error. Making that fatal means writing accurate
descriptions for ten metrics in the agent and SSH server packages, which
is a behavioral claim I would rather not guess at. Follow-up filed as
DOCS-906.

The scanner still uses package-level syntactic prefix inference. Broader
validation of prefix scan roots, mixed registration within a package,
and parameter-passed registerers belongs in DOCS-908 rather than this
focused drift fix.

<details>
<summary>Local pre-push note</summary>

The pre-push hook cannot run in this workspace: GNU Make 4.4.1 segfaults
on the repo's `.SHELLFLAGS = $@ -ceu` configuration for a target named
`test`, tracked in DOCS-903 with a minimal reproduction. I verified with
the package tests, `golangci-lint`, and full pre-commit, and disabled
the hook for this worktree only through its documented `git config
coder.pre-push` knob, restoring it right after the push. No
`--no-verify`.

</details>

---

Generated with Coder Agents on behalf of @nickvigilante. Linear:
[DOCS-897](https://linear.app/codercom/issue/DOCS-897/metricsdocgen-static-metrics-file-can-go-stale-without-failing-ci)
2026-10-01 16:06:32 -04:00

3.0 KiB

Metrics Documentation Generator

This tool generates the Prometheus metrics documentation at docs/admin/integrations/prometheus.md.

How It Works

The documentation is generated from two metrics files:

  1. metrics (static, manually maintained)
  2. generated_metrics (auto-generated, do not edit)

These files are merged and used to produce the final documentation.

metrics (static)

Contains metrics that are not defined in Coder source code, such as:

  • go_*: Go runtime metrics
  • process_*: Process metrics from prometheus/client_golang
  • promhttp_*: Prometheus HTTP handler metrics

Note

This file also supplies label metadata for source-defined metrics that the scanner cannot observe. For example, coderd_agentstats_* labels are configured at runtime. When a metric appears in both files, its generated name, type, and description take priority, and the two label sets are unioned and deduplicated by name.

Edit this file to add metrics that should appear in the documentation but are not scanned from the Coder codebase, or to supply labels that only exist at runtime. Static labels are additive: an entry here can document a label the scanner cannot see, but it cannot remove or replace one the scanner found. To correct a label the scanner derives wrongly, fix the source declaration or the scanner, because a static entry will not override it. Do not add source-defined metrics solely because their registerer has a static prefix: the scanner resolves prefixes from prometheus.WrapRegistererWithPrefix and prometheusmetrics.NewMetricAliasRegisterer.

generated_metrics (auto-generated)

Contains metrics extracted from Coder source code by the AST scanner (scanner/scanner.go).

Do not edit this file directly. It is regenerated by running:

make scripts/metricsdocgen/generated_metrics

Configuring the scanner

Configure these options in scanner/scanner.go:

  • Scan scope (scanDirs): Directories searched recursively for metric definitions. Add a directory when definitions live outside the existing scan scope. Test files are excluded.
  • Prefix scope (prefixScanDirs): Directories searched for registerer wrapping. The scanner resolves name prefixes from prometheus.WrapRegistererWithPrefix and prometheusmetrics.NewMetricAliasRegisterer, so prefixes do not need to be listed by hand. Add a directory when a registerer is wrapped outside the existing scope.
  • Exclusions (excludeDirs): Subtrees excluded from scanning, for metrics a deployment never exposes. Document a metric in the static file instead when the scanner cannot extract it correctly.

Updating Metrics Documentation

To regenerate the documentation after code changes:

make docs/admin/integrations/prometheus.md

This will:

  • Run the scanner to update generated_metrics
  • Merge metrics and generated_metrics metric files
  • Update the documentation file