New API endpoint with extendable metrics#1414
Merged
josecelano merged 10 commits intotorrust:developfrom Apr 10, 2025
Merged
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1414 +/- ##
===========================================
+ Coverage 83.45% 84.56% +1.11%
===========================================
Files 234 254 +20
Lines 17197 19189 +1992
Branches 17197 19189 +1992
===========================================
+ Hits 14351 16227 +1876
- Misses 2590 2690 +100
- Partials 256 272 +16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
b766cc7 to
c159351
Compare
This was referenced Mar 26, 2025
c159351 to
4b5fd1b
Compare
8 tasks
59cd15c to
f0999f0
Compare
5b5ecde to
4fc8fa1
Compare
384adf9 to
6573a02
Compare
6573a02 to
db26b61
Compare
d46044f to
e4b4194
Compare
e4b4194 to
b9c8220
Compare
73f17a9 to
0dc7b73
Compare
This package allow creating collection of metrics that can have labels.
It's similar to the `metrics` crate. There are two types of metrics:
- Counter
- Gauge
For example, you can increase a counter with:
```rust
let time = DurationSinceUnixEpoch::from_secs(1_743_552_000);
let label_set: LabelSet = (LabelName::new("label_name"), LabelValue::new("value")).into();
let mut metric_collection = MetricCollection::new(
// Collection of counter-type metrics
MetricKindCollection::new(vec![
Metric::new(
MetricName::new("test_counter"),
SampleCollection::new(vec![Sample::new(Counter::new(0), time, label_set.clone())]))
]),
// Empty colelction of gauge-type metrics
MetricKindCollection::new(vec![])
);
metric_collection.increase_counter(&MetricName::new("test_counter"), &label_set, time);
```
Metric colelctions are serializable into JSON and exportable to
Prometheus format.
…ore and expose in REST API **URL:** http://0.0.0.0:1212/api/v1/metrics?token=MyAccessToken **Sample response:** ```json { "metrics":[ { "kind":"counter", "name":"http_tracker_core_announce_requests_received_total", "samples":[ { "value":1, "update_at":"2025-04-02T00:00:00+00:00", "labels":[ { "name":"server_binding_ip", "value":"0.0.0.0" }, { "name":"server_binding_port", "value":"7070" }, { "name":"server_binding_protocol", "value":"http" } ] } ] }, { "kind":"gauge", "name":"udp_tracker_server_performance_avg_announce_processing_time_ns", "samples":[ { "value":1.0, "update_at":"2025-04-02T00:00:00+00:00", "labels":[ { "name":"server_binding_ip", "value":"0.0.0.0" }, { "name":"server_binding_port", "value":"7070" }, { "name":"server_binding_protocol", "value":"http" } ] } ] } ] } ``` **URL:** http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken&format=prometheus ``` http_tracker_core_announce_requests_received_total{server_binding_ip="0.0.0.0",server_binding_port="7070",server_binding_protocol="http"} 1 udp_tracker_server_performance_avg_announce_processing_time_ns{server_binding_ip="0.0.0.0",server_binding_port="7070",server_binding_protocol="http"} 1 ```
681b8c0 to
af8dbfa
Compare
After discussing with @da2ce7 we don't think this is necessary.
To remove duplicate data. LabelSet is the HashMap key and it was also included in the HashMap value.
… Sample The new name is more common in the context of metrics and time-series data packages like Prometheus.
Member
Author
|
ACK e3b84a4 |
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to: #1263 (comment)
It adds a new REST API endpoint with a new extendable format for metrics.
In this PR I will only add the metrics for:
See the complete list here.
It requires emitting the new metrics from the:
The remaining metrics (
torrentsandpeers) require adding metrics to the tracker core package.JSON format
URL: http://0.0.0.0:1212/api/v1/metrics?token=MyAccessToken
Sample response:
{ "metrics":[ { "kind":"counter", "name":"http_tracker_core_announce_requests_received_total", "samples":[ { "value":1, "recorded_at":"2025-04-02T00:00:00+00:00", "labels":[ { "name":"server_binding_ip", "value":"0.0.0.0" }, { "name":"server_binding_port", "value":"7070" }, { "name":"server_binding_protocol", "value":"http" } ] } ] }, { "kind":"gauge", "name":"udp_tracker_server_performance_avg_announce_processing_time_ns", "samples":[ { "value":1.0, "recorded_at":"2025-04-02T00:00:00+00:00", "labels":[ { "name":"server_binding_ip", "value":"0.0.0.0" }, { "name":"server_binding_port", "value":"7070" }, { "name":"server_binding_protocol", "value":"http" } ] } ] } ] }Prometheus format
URL: http://0.0.0.0:1212/api/v1/metrics?token=MyAccessToken&format=prometheus
Manual Tests
You can increase the number of
announceorscraperequests by using the console tracker client.HTTP:
cargo run -p torrust-tracker-client --bin http_tracker_client announce http://127.0.0.1:7070 443c7602b4fde83d1154d6d9da48808418b181b6 | jqcargo run -p torrust-tracker-client --bin http_tracker_client scrape http://127.0.0.1:7070 443c7602b4fde83d1154d6d9da48808418b181b6 | jqUDP:
cargo run -p torrust-tracker-client --bin udp_tracker_client announce udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jqcargo run -p torrust-tracker-client --bin udp_tracker_client scrape udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jqTODO
metricspackage.Discarded:
Use a f64 for metric values instead of u64.. Usingu64for counter andf64for gauge.Add a newThe metric would not be the same with different context, it would be a different source even if they might represent the same thing. For example, a counter for announce requests can be added at the server level, HTTP core level and tracker core level. I think it's better to use a different metric for them.namespacelabel to avoid conflict for packages using the same metric name.Notes:
Future work
MetricCollectionto search for metrics, apply aggregated functions, etc. For example:http://0.0.0.0:1212/api/v1/statsAPI endpoint until we have the same information in the new endpointhttp://0.0.0.0:1212/api/v1/metrics. We need to add this type of metrics to the tracker core package too because it's the package we can gettorrentsandpeersmetrics from. See Overhaul stats: Segregated metrics for each tracker running on a different socket address #1263 (comment)