Skip to content

feat(rest-api): [#2734] add HELP and TYPE to the legacy stats Prometheus text - #2741

Merged
josecelano merged 5 commits into
torrust:developfrom
josecelano-bot:issue-2734-stats-help-type
Nov 5, 2026
Merged

josecelano merged 5 commits into
torrust:developfrom
josecelano-bot:issue-2734-stats-help-type

Conversation

@josecelano-bot

Copy link
Copy Markdown
Contributor

Summary

Prints # HELP and # TYPE before each of the 32 metrics of GET /api/v1/stats?format=prometheus, as the ADR requires. Metric names, order and values are unchanged; the JSON response is untouched.

  • One table, STATS_DESCRIPTIONS (name, type, help), lives next to Stats in rest-api-protocol; metrics_response renders from it. A test checks that names and order match the JSON fields and that Stats::values() lines up with the table. This mirrors /metrics, where each metric is declared once with its type and description.
  • Types as confirmed by the maintainer: 8 gauges (torrents, seeders, leechers, completed_persisted_enabled, udp_banned_ips_total, udp_avg_*_processing_time_ns), 24 counters.
  • Module docs updated, "known gap" wording removed.

Verification

Evidence in docs/issues/open/2734-stats-prometheus-text-help-and-type/manual-verification-evidence.md: real tracker + curl (persistence on and off), promtool check metrics 3.5.0, Prometheus 3.5.0 scraping the tracker (target up, 32 samples, 24 counter / 8 gauge in the metadata). linter all and the pre-push checks pass.

Needs your attention: AC4

promtool check metrics exits 3, not 0. There are no more "no help text" warnings; the 28 remaining ones are naming lint on frozen API v1 names (24 counters without _total, 3 _ns units, the gauge udp_banned_ips_total). Renaming is a breaking change, so per the maintainer's decision AC4 was amended and the rename is noted for the next API version (spec section "Next API version" and a doc comment on the table).

Closes #2734

🤖 Generated with Claude Code

josecelano-bot and others added 4 commits November 5, 2026 17:41
…e legacy stats Prometheus text

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…Prometheus text

The descriptions and types of the 32 metrics live in one table in the protocol
package that the renderer reads. Names and values are unchanged.

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.

🟡 Changes recommended

Stats field documentation and HELP descriptions are independently duplicated and already differ for udp4_responses, leaving the required shared source of descriptions unestablished.

1 open issue
What changed in this PR

Adds # HELP and # TYPE metadata to the legacy stats endpoint’s Prometheus output, implementing #2734 while preserving metric names, order, values, and JSON behavior.

Changes:

  • Adds metadata for all 32 statistics: 24 counters and 8 gauges.
  • Updates rendering and tests for metadata, field ordering, and value alignment.
  • Updates documentation and records verification, including the accepted naming-lint warnings for frozen API v1 names.
File Description
packages/​rest-api-protocol/​src/​v1/​context/​stats/​resources/​stats.rs Adds metric descriptions, types, ordered values, and alignment tests.
packages/​axum-rest-api-server/​src/​v1/​context/​stats/​responses.rs Renders HELP/TYPE lines and tests the resulting text.
packages/​axum-rest-api-server/​src/​v1/​context/​stats/​mod.rs Updates endpoint documentation and output examples.
packages/​axum-rest-api-server/​src/​v1/​context/​stats/​handlers.rs Updates the Prometheus response expectation.
docs/​issues/​open/​2734-stats-prometheus-text-help-and-type/​manual-verification-evidence.md Records tracker, promtool, and Prometheus verification results.
docs/​issues/​open/​2734-stats-prometheus-text-help-and-type/​ISSUE.md Records progress and amends acceptance criteria for naming warnings.

Review effort: Balanced

…cs and the HELP table from one list

The Stats field documentation and the Prometheus HELP text were two copies that
had already drifted. A macro now declares both from one description.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@josecelano

Copy link
Copy Markdown
Member

ACK 6846ccf

@josecelano
josecelano merged commit 08e2375 into torrust:develop Nov 5, 2026
23 checks passed
josecelano pushed a commit that referenced this pull request Nov 6, 2026
The legacy /stats Prometheus text carries HELP and TYPE since #2741, so the ADR
no longer lists it as a known exception.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
josecelano added a commit that referenced this pull request Nov 6, 2026
…e the ADR

5a5aadc chore(issues): [#2734] archive the closed spec and update the ADR (josecelano-bot)

Pull request description:

  ## Summary

  Close-out of #2734 (implemented in #2741):

  - Moves the spec to `docs/issues/closed/`, sets `status: done` and `related-pr: 2741`, ticks the checkboxes and records the retrospective.
  - Updates the ADR "Define the stats and metrics endpoints and require HELP and TYPE in Prometheus output" (rule 5, background, consequences, index row; marked "amended 2026-11-06"): the legacy `/stats?format=prometheus` is no longer a known exception.

  Docs only. `linter all` and the pre-push checks pass.

  🤖 Generated with [Claude Code](https://claude.com/claude-code)

ACKs for top commit:
  josecelano:
    ACK 5a5aadc

Tree-SHA512: fc91a569ca401845e26bf2712ea8cf04a7624c56abcdea912f5c7576e13ce6d6ecf5a95df4871e7cf509fa54912cc0b33421f8e5ef7b24aabf28f786eb64bcb5

@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 #2741 as it stands at a94c3ced8 on the default branch.

What still runs: the stats! macro in packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs declares the Stats fields, STATS_DESCRIPTIONS and Stats::values() from one list. metrics_response (packages/axum-rest-api-server/src/v1/context/stats/responses.rs:35) writes a # HELP line, a # TYPE line and a sample line for each of the 32 statistics. Both files are byte-identical to the pull request's head 6846ccf93. The only later change in this context is the curl examples in the module docs (90634942d). The description says that one table lives next to Stats; in the merged head a single macro declares both the struct and the table, so they cannot drift. The manual evidence was recorded at bcd7982d0, before that macro commit. The exact-text test in responses.rs, which 6846ccf93 did not touch, pins the same rendered bytes.

[Minor] The help text of tcp4_connections_handled and tcp6_connections_handled (packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:113 and :116) says "connections". The value is actually the sum of the announce and scrape requests (packages/rest-api-runtime-adapter/src/v1/adapters/stats.rs:99 and :104). For example, a client that sends one announce and one scrape over one keep-alive connection to an IPv4 HTTP tracker is reported as tcp4_connections_handled 2, under a HELP line that reads "Total number of TCP (HTTP tracker) connections from IPv4 peers." Before this change the sentence was only a Rust field comment. It is now the description that operators see next to the series, and rule 4 of the ADR "Define the stats and metrics endpoints and require HELP and TYPE in Prometheus output" asks that the help describe the value. Smallest fix: keep the frozen names and change the two help strings to "Total number of TCP (HTTP tracker) announce and scrape requests from IPv4 peers." and its IPv6 twin. The exact-text test in responses.rs changes with them.

[Nit] udp4_connections_handled and udp6_connections_handled both count accepted connect requests (packages/rest-api-runtime-adapter/src/v1/adapters/stats.rs:119 and :127), but their help texts differ: "connections from IPv4 peers" (packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:131) and "connection requests from IPv6 peers" (packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:138). One wording for both, such as "Total number of UDP (UDP tracker) connect requests accepted from IPv4 peers.", would describe the value and keep the pair symmetric.

[Nit] The field documentation of completed gives the same instruction twice. The macro puts the help sentence first (packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:25), and that sentence already ends with "Use completed_in_session and completed_persisted instead.". The extra doc lines at packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs:99-100 then add "Use [Self::completed_in_session] and [Self::completed_persisted] instead." Dropping the extra lines removes the repetition; keeping only the two links, without the sentence, would also work.

[Nit] The closed issue spec marks T5 as DONE but still gives "exit 0" for the metrics lint check as its expected output (docs/issues/closed/2734-stats-prometheus-text-help-and-type/ISSUE.md:105). AC4, M2 and manual-verification-evidence.md record exit 3, with naming-convention warnings only, after the amendment. The row should say the same.

Checked and found sound:

  • The 32 names and their order match the JSON fields and the previous text, and the values are unchanged (the boolean is still 1 or 0).
  • The JSON resource keeps its derives and its three #[serde(default)] fields.
  • The 8 gauge and 24 counter types match how each value is produced. The request and download totals only rise within one process, and the stored download totals are restored at startup. udp_banned_ips_total can fall when bans are reset. The averages and the swarm sizes move both ways.
  • Every family has exactly one HELP line and one TYPE line before its sample. No help text contains a backslash or a line feed that would need escaping, and the body ends with a line feed.
  • The renderer has no fallible or panicking path, and the format is still chosen only by a lowercase json or prometheus query value.
  • The stats routes, their authentication and the labelled /metrics text are untouched.

This branch was previously deployed

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

Add HELP and TYPE lines to the legacy stats Prometheus text

4 participants