Skip to content

docs: add auto-generated internal metrics reference - #18377

Open
kovan wants to merge 8 commits into
prometheus:mainfrom
kovan:docs/auto-generate-metrics-reference
Open

kovan wants to merge 8 commits into
prometheus:mainfrom
kovan:docs/auto-generate-metrics-reference

Conversation

@kovan

@kovan kovan commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a Go tool (tools/metric-docs) that generates a complete reference of all metrics Prometheus exposes about itself at /metrics, using static analysis of the source code.

The tool parses Go AST to find prometheus.*Opts struct literals and extracts Name, Help, Namespace, and Subsystem fields. It resolves package-level constants and cross-package references (e.g. discovery.KubernetesMetricsNamespace), and applies known runtime prefixes for metrics registered via WrapRegistererWithPrefix.

This captures 246 metrics including those that only appear under specific configurations or runtime conditions (per-SD-provider metrics, corruption counters, etc.), organized by category (TSDB, Scrape, Rules, PromQL, Remote storage, Service discovery, etc.).

The generated docs/internal-metrics.md is committed to git following the project convention for generated docs (same as cli-documentation and generate-promql-functions). Makefile targets are provided:

make internal-metrics-docs        # regenerate
make check-internal-metrics-docs  # verify up to date (for CI)

Previous attempt #14487 was a static list of ~160 metrics obtained from /api/v1/metadata on the demo instance, which was incomplete (missed conditional metrics) and not maintainable. This approach addresses both concerns raised by maintainers:

  • @bwplotka asked for documentation of how the list was obtained
  • @bboreham said a static list was OK to merge but automation would be better
[ENHANCEMENT] Docs: Add tool to auto-generate internal metrics reference from source code (246 metrics).

Fixes #12397

Add a Go tool (tools/metric-docs) that uses static analysis of the
source code to generate a complete list of all metrics Prometheus
exposes about itself at /metrics. The tool parses Go AST to find
prometheus.*Opts struct literals and extracts Name, Help, Namespace,
and Subsystem fields, resolving package-level constants and
cross-package references.

This approach captures all metrics including those that only appear
under specific configurations or runtime conditions (e.g. per-SD
metrics, corruption counters), without requiring a running instance.

The generated docs/internal-metrics.md lists 246 metrics organized
by category (TSDB, Scrape, Rules, PromQL, Remote storage, etc.).

To regenerate: go run ./tools/metric-docs > docs/internal-metrics.md

Fixes prometheus#12397

Signed-off-by: Kit Dallege <[email protected]>
@kovan
kovan requested a review from a team as a code owner March 26, 2026 16:35
@kovan
kovan requested a review from aknuds1 March 26, 2026 16:35
kovan added 3 commits March 26, 2026 17:37
The internal-metrics.md file is auto-generated by tools/metric-docs
and should not be tracked in git. Add it to .gitignore instead.

Signed-off-by: Kit Dallege <[email protected]>
Add 'internal-metrics-docs' target to generate the metrics reference
and 'check-internal-metrics-docs' to verify it is up to date in CI,
following the same pattern as cli-documentation and
check-generated-promql-functions.

The generated docs/internal-metrics.md is committed to git (matching
the project convention for generated docs) and can be verified with:

    make check-internal-metrics-docs

Signed-off-by: Kit Dallege <[email protected]>
kovan added 4 commits March 26, 2026 17:42
Use file-target dependencies instead of a separate check target.
Make will only regenerate docs/internal-metrics.md when any Go
source file is newer than the output.

Signed-off-by: Kit Dallege <[email protected]>
- Replace single-case switch with if statement (gocritic singleCaseSwitch)
- Remove unused extractStringValue function (unused)

Signed-off-by: Kit Dallege <[email protected]>
Test the core functionality:
- fqName construction from Namespace/Subsystem/Name
- Category assignment from package paths
- Parsing basic metric definitions (counter, gauge, histogram)
- Package-level constant resolution
- Concatenated constant resolution
- String concatenation in Help fields
- Skipping metrics without a Name field
- Cross-package constant resolution
- collectPkgConstants with concatenated values

Signed-off-by: Kit Dallege <[email protected]>
Verify that when a Go source file is modified to add a new metric
definition, scanDir picks it up on the next scan.

Signed-off-by: Kit Dallege <[email protected]>

@SuperQ SuperQ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really interesting Go tool. I wonder if we should make this it's own repo/project so it can be re-used by others.

There was also this PR submitted by someone else that does something similar.

prometheus/node_exporter#3585

@kovan

kovan commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @SuperQ! Making it a standalone tool is an interesting idea - the AST parsing approach is generic enough to work with any Go project that uses the prometheus client library.

I saw the node_exporter PR #3585 - it takes a different approach (runtime-based) but addresses the same need. A standalone tool could potentially serve both use cases.

Happy to refactor it into a separate repo if maintainers think that's the right direction. For now, keeping it in-tree means the Makefile target and CI integration are simpler. Let me know what you prefer.

@github-actions github-actions Bot added the stale label May 28, 2026
@krajorama

Copy link
Copy Markdown
Member

Hello from the bug scrub!

Agree with @SuperQ about having this as a separate tool for reuse. @kovan if you're still interested in, please consider going that route.

@github-actions github-actions Bot removed the stale label Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: Add auto-generated page for current metrics Prometheus exposes about itself.

3 participants