feat(metrics): add requests_per_prefix_total counter with handler-level test coverage#557
Closed
Copilot wants to merge 3 commits into
Closed
feat(metrics): add requests_per_prefix_total counter with handler-level test coverage#557Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Adds a configurable per-prefix counter on /render. Each request
increments requests_per_prefix_total{prefix=...} once per configured
prefix that is directly referenced by one of its target metric
patterns (metric == prefix, or metric starts with '<prefix>.').
Configured via the new requestsPerPrefixList yaml key on the api
side. Empty/glob entries are dropped at startup. Field is nil by
default — feature stays silent until configured.
…est coverage Address review comment #discussion_r3427005713: extract the per-prefix prefix-matching loop from renderHandler into a new testable helper collectMatchedPrefixes, and add TestCollectMatchedPrefixes with 10 cases covering the full pipeline (target string → parser → Metrics() → prefix matching), including function expressions, unparseable targets, dot-boundary enforcement, exact matches, and deduplication.
Copilot
AI
changed the title
[WIP] Fix code for review comment in PR #556
feat(metrics): add requests_per_prefix_total counter with handler-level test coverage
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a configurable per-prefix Prometheus counter on
/render. Each request incrementsrequests_per_prefix_total{prefix=...}once per configured prefix directly referenced by any target metric pattern (metric== prefix, or starts with<prefix>.).What issue is this change attempting to solve?
The per-prefix counting logic in
renderHandlerwas untested — existing tests only coveredmetricRefsPrefixandinitRequestPrefixesin isolation, leaving the full target-parsing → prefix-matching → counter-increment pipeline vulnerable to silent regressions.How does this change solve the problem? Why is this the best approach?
requestsPerPrefixList []stringon the API config; empty/glob entries dropped at startup viainitRequestPrefixes.requests_per_prefix_total(CounterVecwithprefixlabel) registered inPrometheusMetrics.collectMatchedPrefixes(targets, configuredPrefixes)— extracted fromrenderHandler's inline loop into a standalone function that parses each target string, walksexp.Metrics(), and returns the matched prefix set. This makes the full pipeline unit-testable without a live HTTP handler.renderHandlercallscollectMatchedPrefixesbefore its main loop, then increments the counter for each matched prefix after all targets are processed.Extraction into
collectMatchedPrefixesis the minimal change that makes the logic independently testable while keepingrenderHandlerreadable.How can we be sure this works as expected?
TestCollectMatchedPrefixescovers the full pipeline (target string → parser.ParseExpr → Metrics() → metricRefsPrefix) with 10 cases:sum(a.b.c.*))a.bc.ddoes not match prefixa.b)a.bmatches prefixa.b)