Skip to content

Overhaul stats: Scaffolding for metrics in the tracker-core package - #1536

Merged
josecelano merged 1 commit into
torrust:developfrom
josecelano:1535-overhaul-stats-persist-labeled-metrics
May 27, 2025
Merged

josecelano merged 1 commit into
torrust:developfrom
josecelano:1535-overhaul-stats-persist-labeled-metrics

Conversation

@josecelano

@josecelano josecelano commented May 26, 2025 •

Copy link
Copy Markdown
Member

Relates to: #1502

It adds a new metric.

The new metric in Prometheus format:

tracker_core_persistent_torrents_downloads_total{} 1

In new PRs, the new metric will be persisted.

@josecelano josecelano self-assigned this May 26, 2025
@josecelano
josecelano requested a review from da2ce7 May 26, 2025 17:44
@josecelano josecelano added Enhancement / Feature Request Something New - Admin - Enjoyable to Install and Setup our Software labels May 26, 2025
@codecov

codecov Bot commented May 26, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 39.71631% with 85 lines in your changes missing coverage. Please review.

Project coverage is 83.52%. Comparing base (f21957d) to head (2175270).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
packages/tracker-core/src/statistics/repository.rs 25.37% 50 Missing ⚠️
packages/tracker-core/src/statistics/metrics.rs 24.24% 25 Missing ⚠️
...s/rest-tracker-api-core/src/statistics/services.rs 0.00% 5 Missing ⚠️
...racker-api-server/src/v1/context/stats/handlers.rs 0.00% 2 Missing ⚠️
...racker-client/src/http/client/requests/announce.rs 0.00% 2 Missing ⚠️
src/bootstrap/jobs/tracker_core.rs 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1536      +/-   ##
===========================================
- Coverage    83.89%   83.52%   -0.37%     
===========================================
  Files          270      273       +3     
  Lines        19181    19317     +136     
  Branches     19181    19317     +136     
===========================================
+ Hits         16091    16134      +43     
- Misses        2817     2909      +92     
- Partials       273      274       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano josecelano changed the title Overhaul stats: Add new persisted metric to the tracker-core (total downloads for all torrents) Overhaul stats: Scaffolding for metrics in the tracker-core package May 26, 2025
@josecelano josecelano linked an issue May 26, 2025 that may be closed by this pull request
New metric added:

```
tracker_core_persistent_torrents_downloads_total{} 1
```

However, it's not persisted yet.

TODO:

- Persist into the database when updated.
- Load from database when the tracker starts.
@josecelano
josecelano force-pushed the 1535-overhaul-stats-persist-labeled-metrics branch from 14a1ffa to 2175270 Compare May 27, 2025 07:12
@josecelano
josecelano marked this pull request as ready for review May 27, 2025 07:39
@josecelano

Copy link
Copy Markdown
Member Author

ACK 2175270

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

What still runs from 21752709a: the tracker-core statistics repository and its metrics wrapper (packages/tracker-core/src/statistics/repository.rs, packages/tracker-core/src/statistics/metrics.rs), the container field that holds the repository (packages/tracker-core/src/container.rs:53, built at :105), and the increment of tracker_core_persistent_torrents_downloads_total on every PeerDownloadCompleted event (packages/tracker-core/src/statistics/event/handler.rs:72). Later changes split the listener into an in-memory listener and a persistence listener (6248ff876) and deprecated that counter in favour of explicit in-session and persisted counters (194676344). As merged, the bootstrap started the listener when only tracker_usage_statistics was on, while the handler wrote each completion to the database unconditionally, so the default configuration persisted downloads with persistent_torrent_completed_stat = false. 4febda494 made those writes conditional on that flag, and each of the two listeners now starts on its own flag (src/bootstrap/jobs/tracker_core.rs:24, :45), so that is history. The description mentions only the new counter. The development configuration timeouts the change also edited went unexplained, and they still hold the values it set.

[Minor] share/default/config/tracker.development.sqlite3.toml:36 sets max_peer_timeout = 60 and :13 runs the cleanup every 120 s. The file has no [core.announce_policy], so the default announce interval and minimum interval of 120 s apply (packages/primitives/src/announce.rs:74-79). This file is the binary's fallback configuration (src/bootstrap/config.rs:24) and the README's starting point for a custom one. A peer that announces on schedule passes the 60 s cutoff (packages/tracker-core/src/torrent/manager.rs:104) halfway through each interval. Exactly one cleanup pass falls in every 120 s window, so about half of the compliant peers are removed before each regular announce and re-added by it. With remove_peerless_torrents = true (:38), a one-peer swarm goes with them. The removals also lose completions, which this file's persistent_torrent_completed_stat = true (:37) is there to exercise. Example: a leecher announces at t=0, a cleanup pass at t=70 removes it, and it finishes and announces event=completed at t=90. The swarm then treats it as a new peer (packages/swarm-coordination-registry/src/swarm/coordinator.rs:162-177), so no PeerDownloadCompleted is sent and neither the in-session nor the persisted totals move. The change doubled both values from 30 and 60 without saying why. The timeout-below-interval relation that drops compliant peers predates it and survives it. Smallest fix: set max_peer_timeout above the announce interval in this file, for example 240. If a short timeout is wanted for manual tests, instead add [core.announce_policy] with interval and interval_min below 60, and a comment saying so.

[Nit] packages/tracker-core/src/statistics/metrics.rs:7 documents the type as metrics collected by the torrent repository, and packages/tracker-core/src/statistics/repository.rs:16 calls it a repository for the torrent repository metrics. Both lines were carried over from the swarm package and name the wrong package. That now shows in the public documentation of the tracker-core type. Smallest fix: say tracker-core metrics in both lines.

Checked and found sound:

  • The write paths release the lock before logging and return the error they log (packages/tracker-core/src/statistics/repository.rs:90-108). The handler discards a result only after it has been logged.
  • The persisted totals are seeded before any listener subscribes (src/app.rs:268-270).
  • Persistent statistics without usage statistics are refused at startup (src/bootstrap/persistence.rs:41, called from src/bootstrap/app.rs:62), so the deprecated counter cannot sit frozen at its seeded value.
  • The persisted counter advances only after the global database increment succeeds (packages/tracker-core/src/statistics/event/handler.rs:113-131).
  • The expect calls on the metrics merge (packages/rest-api-runtime-adapter/src/v1/adapters/stats.rs:139-158) and on the initial zero values (packages/tracker-core/src/statistics/mod.rs:65-75) cannot fire with the current package-prefixed names.
  • The change added no credential, endpoint or new exposure.

This branch was previously deployed

1 inactive deployment
coverage — 21752709 Deployed May 27, 2025 by josecelano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

- Admin - Enjoyable to Install and Setup our Software Enhancement / Feature Request Something New

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overhaul stats: Scaffolding for metrics in tracker-core package

2 participants