Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(scripts/metricsdocgen): add prometheus.New*() and New*Vec() patterns to metrics scanner - #21462

Merged
ssncferreira merged 3 commits into
mainfrom
ssncferreira/metricsdocgen-ast-scanner-opts
Feb 13, 2026
Merged

feat(scripts/metricsdocgen): add prometheus.New*() and New*Vec() patterns to metrics scanner#21462
ssncferreira merged 3 commits into
mainfrom
ssncferreira/metricsdocgen-ast-scanner-opts

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Description

This PR implements extraction of metrics defined using prometheus.New*() and prometheus.New*Vec() patterns with *Opts{} structs.

Changes

  • Add extractOptsMetric() to handle:
    • prometheus.NewGauge(prometheus.GaugeOpts{...})
    • prometheus.NewCounter(prometheus.CounterOpts{...})
    • prometheus.NewHistogram(prometheus.HistogramOpts{...})
    • prometheus.NewSummary(prometheus.SummaryOpts{...})
    • prometheus.New*Vec(prometheus.*Opts{...}, labels)
  • Script generates an updated scripts/metricsdocgen/generated_metrics file

Related to: #13223

Disclosure: This PR was mainly developed with Claude Sonnet 4, with iterative review and refinement by @ssncferreira

@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from f64fdaf to d2a562c Compare January 8, 2026 21:22
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from ee2942e to b0e8dc4 Compare January 8, 2026 21:22
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from d2a562c to 4340913 Compare January 9, 2026 08:51
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from b0e8dc4 to c4b0acc Compare January 9, 2026 08:51
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from 4340913 to 1c42124 Compare January 9, 2026 08:52
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from c4b0acc to 0243d78 Compare January 9, 2026 08:52
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from 1c42124 to b52d9eb Compare January 9, 2026 09:29
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch 3 times, most recently from 602f312 to ca16038 Compare January 9, 2026 09:47
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from b52d9eb to 44b7bba Compare January 9, 2026 09:47
@@ -1,21 +1,147 @@
# HELP agent_reconnecting_pty_connections_total

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There are some metrics without a HELP definition (description). This indicates that those metrics should be updated in the source code, but that's out of scope for this PR.

As a follow-up, the scanner should probably warn about these metrics and exclude them from the generated_metrics file, since their descriptions would appear empty in the final documentation. Wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a warning log in scanFile when a metric has an empty HELP description. The metric is still included in the output for now. Added a TODO to consider filtering them out in a future PR.

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.

SGTM.

out of curiosity: how many metrics without HELP do we have? maybe we can fix them here, and agree to fail fast when HELP is missing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

2026/02/13 10:36:42 WARNING: metric "agent_scripts_executed_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_ssh_server_failed_connections_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_ssh_server_sftp_connections_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_ssh_server_sftp_server_errors_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_x11_handler_errors_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_sessions_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_sessions_errors_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_reconnecting_pty_connections_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "agent_reconnecting_pty_errors_total" has no HELP description, skipping
2026/02/13 10:36:42 WARNING: metric "coderd_notifications_dispatch_attempts_total" has no HELP description, skipping

We have 10 metrics without HELP. The script already logs a warning for these cases. The last one uses fmt.Sprintf for the description, which the scanner cannot evaluate at compile time. We can address this in a follow-up PR 👍

@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from ca16038 to 8bc9baa Compare January 9, 2026 10:37
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch 2 times, most recently from 70d2452 to a14ee38 Compare January 9, 2026 10:38
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch 2 times, most recently from 7c7892d to 8fc6317 Compare January 9, 2026 10:39
@ssncferreira ssncferreira changed the title feat(scripts): add prometheus.New*() and New*Vec() patterns to metrics scanner feat(scripts/metricsdocgen): add prometheus.New*() and New*Vec() patterns to metrics scanner Jan 9, 2026
@ssncferreira
ssncferreira marked this pull request as ready for review January 9, 2026 11:32
Comment on lines +404 to +413
// extractOptsMetric extracts a metric from prometheus.New*() or prometheus.New*Vec() calls.
// Supported patterns:
// - prometheus.NewGauge(prometheus.GaugeOpts{...})
// - prometheus.NewCounter(prometheus.CounterOpts{...})
// - prometheus.NewHistogram(prometheus.HistogramOpts{...})
// - prometheus.NewSummary(prometheus.SummaryOpts{...})
// - prometheus.NewGaugeVec(prometheus.GaugeOpts{...}, labels)
// - prometheus.NewCounterVec(prometheus.CounterOpts{...}, labels)
// - prometheus.NewHistogramVec(prometheus.HistogramOpts{...}, labels)
// - prometheus.NewSummaryVec(prometheus.SummaryOpts{...}, labels)

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.

since you identified these code snippets, maybe claude can generate fake .go files that use these patterns?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea 👍 we can create test fixtures with each pattern as a follow-up

if name != "" {
parts = append(parts, name)
}
return strings.Join(parts, "_")

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.

fmt.Sprintf("%s_%s_%s") ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can't use fmt.Sprintf here, namespace and subsystem are optional. This could generate something like "__name". The current approach only joins non-empty parts. Added a comment for clarity

@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from a14ee38 to 7464012 Compare January 16, 2026 19:01
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from b6a4028 to 9bae7b7 Compare January 16, 2026 19:01
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from 9bae7b7 to f8f98ee Compare January 23, 2026 19:18
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from 0462360 to 2caf0ad Compare January 28, 2026 08:57
@coderagents

coderagents Bot commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

Documentation Check

No Changes Needed

This PR enhances the internal metrics documentation generator () to recognize additional Prometheus metric patterns ( and ). Since this is an internal development tool that automatically generates metrics documentation and doesn't affect user-facing features or workflows, no documentation updates are required.

The generated metrics file () is auto-generated and properly updated by the script itself.


Automated review via Coder Tasks

@github-actions github-actions Bot added the stale This issue is like stale bread. label Feb 5, 2026
@ssncferreira ssncferreira removed the stale This issue is like stale bread. label Feb 6, 2026
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from a94b964 to cd66231 Compare February 12, 2026 15:57
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch 2 times, most recently from 49f44c7 to 2b637bb Compare February 12, 2026 16:52
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from cd66231 to 13618b3 Compare February 12, 2026 16:52
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from 2b637bb to fd7a05c Compare February 12, 2026 16:58
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from 13618b3 to 143e11f Compare February 12, 2026 16:58
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from fd7a05c to a65ba82 Compare February 12, 2026 17:22
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch 2 times, most recently from 2463ea5 to 89e829c Compare February 12, 2026 17:23
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from a65ba82 to 1ada5a6 Compare February 12, 2026 17:23
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch 4 times, most recently from c110fa1 to 9832bc8 Compare February 13, 2026 10:20
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-newdesc branch from 36ce1aa to 27c0d16 Compare February 13, 2026 10:20

ssncferreira commented Feb 13, 2026

Copy link
Copy Markdown
Contributor Author

Merge activity

  • Feb 13, 10:48 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Feb 13, 11:03 AM UTC: Graphite rebased this pull request as part of a merge.
  • Feb 13, 11:13 AM UTC: @ssncferreira merged this pull request with Graphite.

@ssncferreira
ssncferreira changed the base branch from ssncferreira/metricsdocgen-ast-scanner-newdesc to graphite-base/21462 February 13, 2026 10:49
@ssncferreira
ssncferreira changed the base branch from graphite-base/21462 to main February 13, 2026 11:01
@ssncferreira
ssncferreira force-pushed the ssncferreira/metricsdocgen-ast-scanner-opts branch from 9832bc8 to 777de5c Compare February 13, 2026 11:02
@ssncferreira
ssncferreira merged commit bcb437d into main Feb 13, 2026
33 checks passed
@ssncferreira
ssncferreira deleted the ssncferreira/metricsdocgen-ast-scanner-opts branch February 13, 2026 11:13
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants