-
Notifications
You must be signed in to change notification settings - Fork 49
New API endpoint with extendable metrics #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josecelano
merged 10 commits into
torrust:develop
from
josecelano:1403-overhaul-stats-start-collecting-stats-per-server-instance-per-socket
Apr 10, 2025
Merged
New API endpoint with extendable metrics #1414
josecelano
merged 10 commits into
torrust:develop
from
josecelano:1403-overhaul-stats-start-collecting-stats-per-server-instance-per-socket
Apr 10, 2025
+4,358
−128
Conversation
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
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.
ACK e3b84a4 |
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 (
torrents
andpeers
) require adding metrics to the tracker core package.JSON format
URL: http://0.0.0.0:1212/api/v1/metrics?token=MyAccessToken
Sample response:
Prometheus format
URL: http://0.0.0.0:1212/api/v1/metrics?token=MyAccessToken&format=prometheus
Manual Tests
You can increase the number of
announce
orscrape
requests 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 | jq
cargo run -p torrust-tracker-client --bin http_tracker_client scrape http://127.0.0.1:7070 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
UDP:
cargo run -p torrust-tracker-client --bin udp_tracker_client announce udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
cargo run -p torrust-tracker-client --bin udp_tracker_client scrape udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
TODO
metrics
package.Discarded:
Use a f64 for metric values instead of u64.. Usingu64
for counter andf64
for 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.namespace
label to avoid conflict for packages using the same metric name.Notes:
Future work
MetricCollection
to search for metrics, apply aggregated functions, etc. For example:http://0.0.0.0:1212/api/v1/stats
API 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 gettorrents
andpeers
metrics from. See Overhaul stats: Segregated metrics for each tracker running on a different socket address #1263 (comment)