Repository navigation
Conversation
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]>
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]>
This reverts commit a1b0cda.
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]>
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
left a comment
There was a problem hiding this comment.
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.
|
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. |
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.*Optsstruct literals and extractsName,Help,Namespace, andSubsystemfields. It resolves package-level constants and cross-package references (e.g.discovery.KubernetesMetricsNamespace), and applies known runtime prefixes for metrics registered viaWrapRegistererWithPrefix.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.mdis committed to git following the project convention for generated docs (same ascli-documentationandgenerate-promql-functions). Makefile targets are provided:Previous attempt #14487 was a static list of ~160 metrics obtained from
/api/v1/metadataon the demo instance, which was incomplete (missed conditional metrics) and not maintainable. This approach addresses both concerns raised by maintainers:Fixes #12397