Skip to content

Commit d717818

Browse files
committed
feat: [#1403] add extendable-labeled metrics to http-tracker-core 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 ```
1 parent 730de9f commit d717818

17 files changed

Lines changed: 432 additions & 46 deletions

File tree

Cargo.lock

Lines changed: 169 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-rest-tracker-api-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ torrust-rest-tracker-api-core = { version = "3.0.0-develop", path = "../rest-tra
3737
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
3838
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
3939
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
40+
torrust-tracker-metrics = { version = "3.0.0-develop", path = "../metrics" }
4041
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
4142
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker-server" }
4243
tower = { version = "0", features = ["timeout"] }

packages/axum-rest-tracker-api-server/src/v1/context/stats/handlers.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
99
use bittorrent_udp_tracker_core::services::banning::BanService;
1010
use serde::Deserialize;
1111
use tokio::sync::RwLock;
12-
use torrust_rest_tracker_api_core::statistics::services::get_metrics;
12+
use torrust_rest_tracker_api_core::statistics::services::{get_labeled_metrics, get_metrics};
1313

14-
use super::responses::{metrics_response, stats_response};
14+
use super::responses::{labeled_metrics_response, labeled_stats_response, metrics_response, stats_response};
1515

1616
#[derive(Deserialize, Debug, Default)]
1717
#[serde(rename_all = "lowercase")]
@@ -28,7 +28,7 @@ pub struct QueryParams {
2828
pub format: Option<Format>,
2929
}
3030

31-
/// It handles the request to get the tracker statistics.
31+
/// It handles the request to get the tracker global metrics.
3232
///
3333
/// By default it returns a `200` response with the stats in JSON format.
3434
///
@@ -57,3 +57,30 @@ pub async fn get_stats_handler(
5757
None => stats_response(metrics),
5858
}
5959
}
60+
61+
/// It handles the request to get the tracker extendable metrics.
62+
///
63+
/// By default it returns a `200` response with the stats in JSON format.
64+
///
65+
/// You can add the GET parameter `format=prometheus` to get the stats in
66+
/// Prometheus Text Exposition Format.
67+
#[allow(clippy::type_complexity)]
68+
pub async fn get_metrics_handler(
69+
State(state): State<(
70+
Arc<InMemoryTorrentRepository>,
71+
Arc<RwLock<BanService>>,
72+
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
73+
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
74+
)>,
75+
params: Query<QueryParams>,
76+
) -> Response {
77+
let metrics = get_labeled_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
78+
79+
match params.0.format {
80+
Some(format) => match format {
81+
Format::Json => labeled_stats_response(metrics),
82+
Format::Prometheus => labeled_metrics_response(&metrics),
83+
},
84+
None => labeled_stats_response(metrics),
85+
}
86+
}

packages/axum-rest-tracker-api-server/src/v1/context/stats/resources.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! API resources for the [`stats`](crate::v1::context::stats)
22
//! API context.
33
use serde::{Deserialize, Serialize};
4-
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
4+
use torrust_rest_tracker_api_core::statistics::services::{TrackerLabeledMetrics, TrackerMetrics};
5+
use torrust_tracker_metrics::metric_collection::MetricCollection;
56

67
/// It contains all the statistics generated by the tracker.
78
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
@@ -116,6 +117,21 @@ impl From<TrackerMetrics> for Stats {
116117
}
117118
}
118119

120+
/// It contains all the statistics generated by the tracker.
121+
#[derive(Serialize, Debug, PartialEq)]
122+
pub struct LabeledStats {
123+
metrics: MetricCollection,
124+
}
125+
126+
impl From<TrackerLabeledMetrics> for LabeledStats {
127+
#[allow(deprecated)]
128+
fn from(metrics: TrackerLabeledMetrics) -> Self {
129+
Self {
130+
metrics: metrics.metrics,
131+
}
132+
}
133+
}
134+
119135
#[cfg(test)]
120136
mod tests {
121137
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;

0 commit comments

Comments
 (0)