Repository navigation
Labels
Code Cleanup / RefactoringTidying and Making Neat
Description
Depends on: #1445
The API exposes the "old" metrics (/stats) and the new ones (/metrics)
Internally, we use only one repository to store both:
For example, in packages/http-tracker-core/src/statistics/metrics.rs:
pub struct Metrics {
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
pub tcp4_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
pub tcp4_scrapes_handled: u64,
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
pub tcp6_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
pub tcp6_scrapes_handled: u64,
/// A collection of metrics.
pub metric_collection: MetricCollection,
}
impl Metrics {
pub fn increase_counter(&mut self, metric_name: &MetricName, labels: &LabelSet, now: DurationSinceUnixEpoch) {
self.metric_collection.increase_counter(metric_name, labels, now);
}
pub fn set_gauge(&mut self, metric_name: &MetricName, labels: &LabelSet, value: f64, now: DurationSinceUnixEpoch) {
self.metric_collection.set_gauge(metric_name, labels, value, now);
}
}
I kept global and new metrics because I didn't want to make breaking changes in the API. However, we can remove the old ones internally and calculate them from the new ones only when the API replies to a /stats request.
That would have these benefits:
- Simplify the
bittorrent_http_tracker_core::statistics::event::handler - Improve performance; the tracker does not have to maintain both metrics.
The cons are:
- The old
/statsendpoint will be slower - We must implement new methods in the
MetricColelctiontype to calculate aggregate data. These methods could be helpful for other tasks in the future.
Metadata
Metadata
Assignees
Labels
Code Cleanup / RefactoringTidying and Making Neat
Activity
josecelano commentedon May 16, 2025
I've been considering keeping both endpoints:
stats(simple predefined metrics)metrics(extendable-labelled metrics)The second one can be too complex for consumers with basic needs. Since they use labels, you need to aggregate measurements if you want the total for a given metric.
Besides, I'm planning to implement functions on the MetricCollection type to sum samples for a metric. They can be useful for third-party users consuming Torrust tracker metrics.
If we keep both, we should calculate the
statsfrom the new labeled metrics to avoid duplicate code.josecelano commentedon Jun 11, 2025
Implementation plan
We now have two Grafana dashboards for the demo tracker:
statsendpoint (global stats)metricsendpoint (labeled metrics)statsendpoint. Use metric collection in https://github.com/torrust/torrust-tracker/blob/develop/packages/rest-tracker-api-core/src/statistics/services.rs#L41-L100cc @da2ce7
feat: [torrust#1446] add aggregate function sum to metric collection
feat: [torrust#1446] add aggregate function sum to metric collection
feat: [torrust#1446] add aggregate function sum to metric collection
feat: [torrust#1446] add aggregate function sum to metric collection
refactor: [torrust#1446] rename vars
test: [torrust#1446] add more tests to metrics package
refactor: [torrust#1446] WIP. Calculate global metrics from labeled m…
refactor: [torrust#1446] rename AddressType to IpType
feat: [torrust#1446] add new metric label server_binding_address_ip_type
fix: [torrust#1446] clippy error
refactor: [torrust#1446] Calculate global metrics from labeled metrics
refactor: [torrust#1446] Calculate global metrics from labeled metrics
Merge #1577: Overhaul stats: Refactor metrics, use label metrics to c…