Skip to content

fix(axum-rest-api-server): [#2719] print numeric values and a final newline in the legacy stats text - #2727

Merged
josecelano merged 6 commits into
torrust:developfrom
josecelano-bot:2719-stats-prometheus-text-boolean-line
Nov 5, 2026
Merged

josecelano merged 6 commits into
torrust:developfrom
josecelano-bot:2719-stats-prometheus-text-boolean-line

Conversation

@josecelano-bot

Copy link
Copy Markdown
Contributor

Summary

Fixes the legacy GET /api/v1/stats?format=prometheus text, which printed completed_persisted_enabled true (or false) 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 as 1 or 0 and the body ends with a line feed, so the same scrape ingests all 32 samples. The JSON /stats response is unchanged.

Closes #2719

Files touched

  • packages/axum-rest-api-server/src/v1/context/stats/responses.rs: the fix in metrics_response, its doc comment, and the unit tests (the text pinned for Overhaul Packages Testing: Add tests for axum-rest-api-server package #2637 now expects 1 and 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 and manual-verification-evidence.md (reproduction, red/green, like-for-like recheck).

Validation

  • The regression tests fail on the old output (commit order: test, then fix) and pass after the fix; the whole torrust-tracker-axum-rest-api-server package passes.
  • linter all, the pre-commit and the pre-push checks pass.
  • Real artifacts, after the fix: the tracker target is up in Prometheus 3.5.0 with 32 samples scraped (before: down, 5); promtool check metrics 3.5.0 only reports no help text lint warnings; Python prometheus_client 0.26.0 parses 32 families. Commands and output are in the evidence file.

Compatibility: a client that matched the literal true/false in this legacy text would need to read 1/0. No consumer is known in this repository.

🤖 Generated with Claude Code

josecelano-bot and others added 4 commits November 5, 2026 07:09
…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]>
josecelano-bot and others added 2 commits November 5, 2026 13:25
… 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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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_enabled as 1 or 0 and 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

@josecelano

Copy link
Copy Markdown
Member

ACK 40480c6

@josecelano
josecelano merged commit 75fabfc into torrust:develop Nov 5, 2026
22 checks passed
josecelano added a commit that referenced this pull request Nov 5, 2026
…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 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_DESCRIPTIONS and values() come from one macro list and share the length STATS_FIELD_COUNT, so the zip at responses.rs:38 cannot drop a metric.
  • A future field whose type has no lossless conversion to u64 fails to compile instead of printing a non-numeric sample.
  • The # HELP texts contain no backslash or line feed that would need escaping.
  • The full-text pin and the line-feed assertion at responses.rs:197 cover the enabled flag, and responses.rs:222 covers the disabled one.
  • The JSON body still prints the boolean as true (pinned at responses.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.

This branch was previously deployed

1 inactive deployment
coverage — 40480c66 Deployed Nov 5, 2026 by josecelano-bot via Generate Coverage Report #2930
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.

Stats Prometheus text emits a boolean value and breaks Prometheus scrapes

4 participants