Skip to content

Conversation

@josecelano
Copy link
Member

@josecelano josecelano commented May 9, 2025

Emit events from the torrent-repository package and collect metrics. In this PR, I'm only planning to include four metrics. The ones we have in the current stats endpoint:

{
  "torrents": 387044,
  "seeders": 183887,
  "completed": 1260584,
  "leechers": 249136,
}

However, I will rename them to:

{
  "torrent_repository_per_session_all_instances_all_torrents_total": 387044,
  "torrent_repository_per_session_all_instances_all_seeders_total": 183887,
  "torrent_repository_per_session_all_instances_all_torrents_downloads_total": 1260584,
  "torrent_repository_per_session_all_instances_all_leechers_total": 249136,
}

UPDATE: I will not use those long names but "labels". It's only to explain what they represent.

  • per session: since the main tracker process started.
  • all instances: all tracker servers (UDP or HTTP) running on different ports.
  • all torrents: all swarms.

Becuase the current names are too ambiguous. See #1502 (comment).

How to test

  1. Run the tracker.
  2. Use the tracker client to make an announce request.
cargo run -p torrust-tracker-client --bin udp_tracker_client announce udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
  1. Use the REST API to get the metric value.
curl -s "http://localhost:1212/api/v1/metrics?token=MyAccessToken&format=prometheus" | grep torrent_repository_peers_total

Subtasks

  • Add basic event scaffolding in the torrent-respository package.
  • Run the event listener for the torrent-repository package when the tracker starts.
    • Add the TorrentRepositoryContainer and run the listener when the tracker starts.
    • In production code: use the Swarms service provided by the TorrentRepositoryContainer in the TrackerCoreContainer (otherwise they will be unrelated).
    • In testing code: use the Swarms service provided by the TorrentRepositoryContainer in the TrackerCoreContainer (otherwise they will be unrelated).
  • Expose the new metrics via the metrics API endpoint (even if it will have no value yet).
  • Inject the event sender in Swarms and Swarm type to be able to send events from them.
    • In Swarms type.
    • In Swarm type.
  • Allow incrementing/decrementing gauge metrics. #1516
  • Star triggering events and process them to update the metrics in the metrics collection. One event and the corresponding affected metrics at the time.
    • TorrentAdded -> Increase torrents counter (all instances, all torrents)
    • Torrentremoved -> Decrease torrents counter (all instances, all torrents)
    • PeerAdded -> Increase peers counter (total, seeders, leechers)
    • PeerRemoved -> Decrease peers counter (total, seeders, leechers)
    • PeerUdpated -> Decrease peers counter (total, seeders, leechers)
    • PeerDownloadCompleted -> Increase torrent downloads counter (all instances, all torrents)
  • Add tests to check that events have been sent.
  • Add tests to check that metrics are updated after receiving the event.

@josecelano josecelano requested a review from da2ce7 May 9, 2025 15:48
@josecelano josecelano self-assigned this May 9, 2025
@josecelano josecelano added Enhancement / Feature Request Something New - User - Enjoyable to Use our Software - Admin - Enjoyable to Install and Setup our Software labels May 9, 2025
@codecov
Copy link

codecov bot commented May 9, 2025

Codecov Report

Attention: Patch coverage is 93.00380% with 92 lines in your changes missing coverage. Please review.

Project coverage is 84.35%. Comparing base (93fdf31) to head (d154b2a).
Report is 27 commits behind head on develop.

Files with missing lines Patch % Lines
packages/primitives/src/peer.rs 32.72% 37 Missing ⚠️
...orrent-repository/src/statistics/event/listener.rs 52.00% 11 Missing and 1 partial ⚠️
...kages/torrent-repository/src/statistics/metrics.rs 72.72% 9 Missing ⚠️
...torrent-repository/src/statistics/event/handler.rs 94.80% 0 Missing and 8 partials ⚠️
packages/events/src/bus.rs 68.42% 6 Missing ⚠️
...es/torrent-repository/src/statistics/repository.rs 88.46% 6 Missing ⚠️
...s/rest-tracker-api-core/src/statistics/services.rs 28.57% 5 Missing ⚠️
packages/udp-tracker-server/src/environment.rs 62.50% 3 Missing ⚠️
...racker-api-server/src/v1/context/stats/handlers.rs 0.00% 2 Missing ⚠️
src/bootstrap/jobs/torrent_repository.rs 77.77% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1513      +/-   ##
===========================================
+ Coverage    83.75%   84.35%   +0.59%     
===========================================
  Files          257      265       +8     
  Lines        18049    18933     +884     
  Branches     18049    18933     +884     
===========================================
+ Hits         15117    15970     +853     
- Misses        2654     2682      +28     
- Partials       278      281       +3     

☔ 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 force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from addef82 to 4483e76 Compare May 12, 2025 07:32
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from d1c50d5 to 8b6776d Compare May 12, 2025 10:32
josecelano added a commit that referenced this pull request May 13, 2025
243c254 feat: allow incrementing/decrementing gauge metrics (Jose Celano)

Pull request description:

  It allows incrementing/decrementing gauge metrics.

  This feature was planned to be added, and it's needed in this [PR](#1513).

  Other [crates](https://docs.rs/metrics/latest/metrics/struct.Gauge.html) also implement it.

ACKs for top commit:
  josecelano:
    ACK 243c254

Tree-SHA512: 4055825ec04229d65d6ce124ea90c88624ad76ee221b0d6ca5f6edb0b1f4d1e98e81d49b55d7e085cc18f308a2d9f2deb3a2444e2ff30c1b3f00e4cbd83b8a2a
josecelano added 10 commits May 13, 2025 11:26
…ry pkg

TODO:

- Run the event listener for the torrent-repository package when the tracker starts.
- Inject enven sender in `Swarms` and `Swarm` type to send events.
- Trigger events and process them to update the metrics.
- Expose the metrics via the `metrics` API endpoint.
- ...
…ker starts

This creates independent services that are not used yet in
the tracker-core, meaning the `Swarms` object created in the `TorrentRepositoryContainer` will not store any torrent yet. The tracker core is still creating its own fresh instance.
…production code

todo: do the same for testing code.
…ST API

These are the new metrics in JSON format:

http://localhost:1212/api/v1/metrics?token=MyAccessToken

```json
{
  "metrics": [
    {
      "kind": "counter",
      "name": "torrent_repository_persistent_torrents_downloads_total",
      "samples": []
    },
    {
      "kind": "counter",
      "name": "torrent_repository_runtime_torrents_downloads_total",
      "samples": []
    }
  ]
}
```
It required to use `tokio::sync::Mutex` for the `SwarmHandle` (`Arc<Mutex<Swarm>>`). Otherwise it's not safe to pass the Swarm lock between threads.
… pkg

This package dones not have persistence. Persistence is only handle in
the `tracker-core` pacakge. The metric will be included there.
Decrease torrent cleanup interval and peer timeout to do manual tests
faster.
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from 0cfa4f2 to d47483f Compare May 13, 2025 12:53
There are two concepts:

- Unique peers: phisical client with different socket address.
- Peer connections: a client (peer) can particiapte in multiple swarms.

Current metrics count the second, meaning the peer would be counted
doubled if it particiaptes in two swarms.
To fix broken tests.

This implementation will kept for now. I think it's only used for
testing and I'm planning to remvoe all integration tests becuase now web
have unit tests covering the same functionality.
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from f5c478a to 0e38707 Compare May 14, 2025 14:47
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from f04fda5 to f71211f Compare May 14, 2025 17:26
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from 1405ff5 to 31b8715 Compare May 15, 2025 16:51
@josecelano josecelano force-pushed the 1358-overhaul-stats-emit-events-from-the-torrent-repository-package-and-collect-metrics branch from 31b8715 to b13797e Compare May 15, 2025 17:06
@josecelano josecelano marked this pull request as ready for review May 16, 2025 08:31
@josecelano
Copy link
Member Author

ACK d154b2a

@josecelano josecelano merged commit cb9196e into torrust:develop May 16, 2025
21 checks passed
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 - User - Enjoyable to Use our Software Enhancement / Feature Request Something New

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overhaul stats: Emit events from the torrent-repository package and collect metrics

1 participant