Skip to content

Allow incrementing/decrementing gauge metrics.#1516

Merged
josecelano merged 1 commit intotorrust:developfrom
josecelano:increase-decrease-gauges-in-metrics
May 13, 2025
Merged

Allow incrementing/decrementing gauge metrics.#1516
josecelano merged 1 commit intotorrust:developfrom
josecelano:increase-decrease-gauges-in-metrics

Conversation

@josecelano
Copy link
Copy Markdown
Member

It allows incrementing/decrementing gauge metrics.

This feature was planned to be added, and it's needed in this PR.

Other crates also implement it.

@josecelano josecelano force-pushed the increase-decrease-gauges-in-metrics branch from 24ea27b to 243c254 Compare May 13, 2025 09:38
@josecelano
Copy link
Copy Markdown
Member Author

ACK 24ea27b

@josecelano josecelano changed the title It allows incrementing/decrementing gauge metrics. Allow incrementing/decrementing gauge metrics. May 13, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented May 13, 2025

Codecov Report

Attention: Patch coverage is 69.23077% with 36 lines in your changes missing coverage. Please review.

Project coverage is 83.76%. Comparing base (b0bf307) to head (243c254).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
packages/metrics/src/metric_collection.rs 0.00% 28 Missing ⚠️
packages/metrics/src/metric/mod.rs 0.00% 6 Missing ⚠️
packages/metrics/src/sample_collection.rs 95.23% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1516      +/-   ##
===========================================
- Coverage    83.86%   83.76%   -0.11%     
===========================================
  Files          257      257              
  Lines        17945    18049     +104     
  Branches     17945    18049     +104     
===========================================
+ Hits         15049    15118      +69     
- Misses        2620     2654      +34     
- Partials       276      277       +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
Copy link
Copy Markdown
Member Author

ACK 243c254

@josecelano josecelano merged commit 93fdf31 into torrust:develop May 13, 2025
20 of 21 checks passed
josecelano added a commit that referenced this pull request May 16, 2025
…` package and collect metrics

d154b2a refactor: [#1358] clean Swarm type (Jose Celano)
b3b0b71 refactor: [#1358] Swarm, cleaning upsert_peer method (Jose Celano)
47d1eab refactor: [#1358] Swarm tests to use new mock helpers (Jose Celano)
b13797e test: [#1358] add tests for events in torrent-repository pkg (Jose Celano)
f71211f test: [#1358] add tests to torrust_tracker_torrent_repository::swarms::Swarms (Jose Celano)
3d7e6ff test: [#1358] add tests to torrust_tracker_torrent_repository::swarm::Swarm (Jose Celano)
0e38707 fix: [#1358] revert Hash impl for Swarm (Jose Celano)
c9a893c refactor: [#1358] rename metrics for clarity (Jose Celano)
8ee258e refactor: [#1358] use the new field info-hash as ID for the Swarm (Hash,PartialEq) (Jose Celano)
dfba00c feat: [#1358] allow disabling the event sender in the torrent-repository pkg (Jose Celano)
60c00e8 feat: [#1358] add info-hash to all torrent-repository events (Jose Celano)
c706a1b refactor: [#1358] move logs (Jose Celano)
daba8a0 feat: [#1358] new metric in torrent-repository: total number of downloads (Jose Celano)
01a9970 feat: [#1358] new metric in torrent-repository: total number of peers (Jose Celano)
269d273 refactor: [#1358] rename metric (Jose Celano)
ba2033b fix: [#1358] trigger PeerRemoved event when peer is removed due to inactivity (Jose Celano)
d47483f feat: [#1358] new metric in torrent-repository: total number of torrents (Jose Celano)
29a2dfd dev: change default config (Jose Celano)
1eb545c feat: [#1358] remove persistent metric from torrent-repository pkg (Jose Celano)
6d95d1a refactor: [#1358] inject event sender in Swarm type (Jose Celano)
2c479a1 refactor: [#1358] inject event sender in Swarms type (Jose Celano)
68b930d feat: [#1495] expose new torrent-repositoru metrics via the REST API (Jose Celano)
41f4022 feat: [#1358] inject Swarms into InMemoryTorrentRepository in testing code (Jose Celano)
95766bb feat: [#1358] inject Swarms into InMemoryTorrentRepository in production code (Jose Celano)
f986bda feat: [#1358] add the  and run the event listener when the tracker starts (Jose Celano)
2522ad4 feat: [#1358] basic scaffolding for events in torrent-repository pkg (Jose Celano)

Pull request description:

  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:

  ```json
  {
    "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
    ```
  3. 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

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

ACKs for top commit:
  josecelano:
    ACK d154b2a

Tree-SHA512: d4946847dae38b51bc6027b4de25d7e2ad5b1042294913008f503c590c81e586ed083ab980613d647a0f867601f7bd482f4c004628c01cdfffc9d8b810bb670e
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.

1 participant