Repository navigation
fix(axum-rest-api-server): [#2719] print numeric values and a final newline in the legacy stats text - #2727
Conversation
…a final newline in the legacy stats text The legacy stats text printed completed_persisted_enabled as true or false and had no trailing line feed, so Prometheus rejected it. The tests now describe the intended text and fail against the current implementation. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…final newline in the legacy stats text completed_persisted_enabled is now printed as 1 or 0 and the body ends with a line feed, so a Prometheus server scrapes all the lines instead of rejecting the target at the boolean. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…heus text of the stats endpoint Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…for-like recheck Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… require HELP and TYPE Record that /stats is not deprecated and serves the aggregated statistics, that /metrics serves the extendable labelled metrics, and that every Prometheus text the tracker exports must carry # HELP and # TYPE. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…tats and metrics endpoints Point the stats module and handler docs and the issue spec at the ADR, so the endpoint status and the missing HELP and TYPE lines are not rediscovered. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the fix that makes legacy stats text parseable by Prometheus.
No open issues 🎉
What changed in this PR
Fixes #2719 by making the REST API’s legacy stats text parseable by Prometheus, while leaving the JSON response unchanged.
Changes:
- Renders
completed_persisted_enabledas1or0and adds a final newline. - Updates regression tests for both persistence modes and documents the text format.
- Records red/green test results and manual verification showing all 32 samples are scraped.
| File | Description |
|---|---|
packages/axum-rest-api-server/src/v1/context/stats/responses.rs |
Fixes numeric output and newline termination; updates regression tests. |
packages/axum-rest-api-server/src/v1/context/stats/mod.rs |
Documents the legacy Prometheus text and points to the richer metrics endpoint. |
docs/issues/open/2719-stats-prometheus-text-boolean-line/manual-verification-evidence.md |
Records regression results and post-fix parser and scrape checks. |
docs/issues/open/2719-stats-prometheus-text-boolean-line/ISSUE.md |
Updates implementation progress and acceptance evidence. |
Review effort: Balanced
|
ACK 40480c6 |
…ssues/closed 025a0ad chore(issues): archive closed issue #2719 spec to docs/issues/closed (josecelano-bot) Pull request description: ## Summary Archives the spec for issue #2719 (closed on GitHub by #2727) from `docs/issues/open/` to `docs/issues/closed/`. - Verified #2719 is `CLOSED` and #2727 is `MERGED` - Updated frontmatter (`status: done`, `spec-path`, `related-pr: 2727`, `last-updated-utc`); added minimal frontmatter to the evidence file - Ticked the workflow checkpoints and recorded that no retrospective is needed - Repaired the ADR links to the moved spec No code changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ACKs for top commit: josecelano: ACK 025a0ad Tree-SHA512: 5a0cdc1653702e8f9fd2a8f378bce32a7d907166a7ac2a0b4565adc083692ec96c0f180b031871982d143a1ec03e6519a11c3ad57fefc2f1aef7fe8205e5a2e4
da2ce7
left a comment
There was a problem hiding this comment.
This is a post-merge review of the change from #2727 as it stands at a94c3ced8 on the default branch.
What still runs: #2741 (commit 5af51fc73) rewrote metrics_response as a loop over STATS_DESCRIPTIONS and Stats::values(). None of the production lines #2727 wrote remain, but the behaviour it introduced does. Stats::values() at packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:57 converts every field with u64::from, so completed_persisted_enabled prints as 1 or 0. The writeln! calls at packages/axum-rest-api-server/src/v1/context/stats/responses.rs:42-44 end every line of the /stats text with a line feed, the last line included. Still in place from #2727 are the persistence-off test (responses.rs:213-223), the endpoint-purpose doc comments (packages/axum-rest-api-server/src/v1/context/stats/handlers.rs:28-30 and :44-46), the module docs (packages/axum-rest-api-server/src/v1/context/stats/mod.rs:3-8 and the Prometheus text section from mod.rs:57, since extended by #2741) and the ADR. The description's file list leaves out the ADR, its index entry and the handlers.rs doc comments, all added in the last two commits. Its other claims match the diff: numeric 1/0, the final line feed, the JSON body unchanged, and the test committed before the fix.
[Minor] GET /api/v1/metrics?format=prometheus still ends without a final line feed whenever the last family it renders has a sample. That is the conformance defect #2727 removed from /stats. labeled_metrics_response at packages/axum-rest-api-server/src/v1/context/stats/responses.rs:18-20 returns the torrust-metrics text unchanged. The test at packages/axum-rest-api-server/src/v1/context/stats/responses.rs:248 pins the body for one counter with one sample, and that body ends in requests_total 1 with no line feed after it. The #2719 evidence records that the strict checker that ships with Prometheus rejects a body ending that way, with unexpected end of input stream; the Prometheus server itself tolerates it. The same evidence file (docs/issues/closed/2719-stats-prometheus-text-boolean-line/manual-verification-evidence.md:56) says that /metrics ends with a newline. That holds only while the last family has no sample, as on the freshly started tracker it measured. The gap predates #2727, but since #2727 the two Prometheus renderers in one file disagree on the final line feed. Smallest fix: in labeled_metrics_response, append a line feed when the rendered text does not already end with one, or terminate the text in torrust-metrics. Then re-pin the test at responses.rs:248 with the final line feed.
[Nit] docs/adrs/20261105132341_define_the_stats_and_metrics_endpoints_and_require_help_and_type_in_prometheus_output.md:47-48 says #2719 fixed "the parts that broke scrapers (a boolean value and the missing final line feed)". The #2719 analysis (docs/issues/closed/2719-stats-prometheus-text-boolean-line/ISSUE.md:52 and :57) and its evidence say otherwise. The Prometheus 3.5.0 server and the parser of the Python client library both accepted the missing line feed, and only the strict checker rejected it. The boolean broke the scrape; the missing line feed was a conformance defect. Smallest fix: "(the boolean value, which broke scrapers, and the missing final line feed, which strict parsers reject)".
Checked and found sound:
STATS_DESCRIPTIONSandvalues()come from one macro list and share the lengthSTATS_FIELD_COUNT, so thezipatresponses.rs:38cannot drop a metric.- A future field whose type has no lossless conversion to
u64fails to compile instead of printing a non-numeric sample. - The
# HELPtexts contain no backslash or line feed that would need escaping. - The full-text pin and the line-feed assertion at
responses.rs:197cover the enabled flag, andresponses.rs:222covers the disabled one. - The JSON body still prints the boolean as
true(pinned atresponses.rs:171). - The doc comments match the code.
- The ADR's history citations (#1414, #1581) match the commits, its related-artifact paths exist, and its index entry matches it.
Summary
Fixes the legacy
GET /api/v1/stats?format=prometheustext, which printedcompleted_persisted_enabled true(orfalse) and had no trailing line feed. A Prometheus 3.5.0 server scraping it marked the target down at that line and ingested only 5 of the 32 samples. The boolean is now printed as1or0and the body ends with a line feed, so the same scrape ingests all 32 samples. The JSON/statsresponse is unchanged.Closes #2719
Files touched
packages/axum-rest-api-server/src/v1/context/stats/responses.rs: the fix inmetrics_response, its doc comment, and the unit tests (the text pinned for Overhaul Packages Testing: Add tests foraxum-rest-api-serverpackage #2637 now expects1and the final newline; a new test covers persistence off).packages/axum-rest-api-server/src/v1/context/stats/mod.rs: documents the legacy text in the module docs.docs/issues/open/2719-stats-prometheus-text-boolean-line/: spec progress andmanual-verification-evidence.md(reproduction, red/green, like-for-like recheck).Validation
torrust-tracker-axum-rest-api-serverpackage passes.linter all, the pre-commit and the pre-push checks pass.upin Prometheus 3.5.0 with 32 samples scraped (before:down, 5);promtool check metrics3.5.0 only reportsno help textlint warnings; Pythonprometheus_client0.26.0 parses 32 families. Commands and output are in the evidence file.Compatibility: a client that matched the literal
true/falsein this legacy text would need to read1/0. No consumer is known in this repository.🤖 Generated with Claude Code