Skip to content

Commit 3f51afc

Browse files
committed
feat: [#1403] expose udp-tracker-core metrics expose in REST API
1 parent 9d09337 commit 3f51afc

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,19 @@ pub async fn get_metrics_handler(
7070
Arc<InMemoryTorrentRepository>,
7171
Arc<RwLock<BanService>>,
7272
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
73+
Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
7374
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
7475
)>,
7576
params: Query<QueryParams>,
7677
) -> Response {
77-
let metrics = get_labeled_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
78+
let metrics = get_labeled_metrics(
79+
state.0.clone(),
80+
state.1.clone(),
81+
state.2.clone(),
82+
state.3.clone(),
83+
state.4.clone(),
84+
)
85+
.await;
7886

7987
match params.0.format {
8088
Some(format) => match format {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
2929
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
3030
http_api_container.ban_service.clone(),
3131
http_api_container.http_stats_repository.clone(),
32+
http_api_container.udp_core_stats_repository.clone(),
3233
http_api_container.udp_server_stats_repository.clone(),
3334
)),
3435
)

packages/rest-tracker-api-core/src/statistics/services.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,36 @@ pub struct TrackerLabeledMetrics {
8484
}
8585

8686
/// It returns all the [`TrackerLabeledMetrics`]
87+
///
88+
/// # Panics
89+
///
90+
/// Will panic if the metrics cannot be merged. This could happen if the
91+
/// packages are producing duplicate metric names, for example.
8792
#[allow(deprecated)]
8893
pub async fn get_labeled_metrics(
8994
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
9095
ban_service: Arc<RwLock<BanService>>,
9196
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
97+
udp_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
9298
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
9399
) -> TrackerLabeledMetrics {
94100
let _torrents_metrics = in_memory_torrent_repository.get_torrents_metrics();
95101
let _udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
96102
let _udp_server_stats = udp_server_stats_repository.get_stats().await;
97103

98104
let http_stats = http_stats_repository.get_stats().await;
99-
100-
TrackerLabeledMetrics {
101-
metrics: http_stats.metric_collection.clone(),
102-
}
105+
let udp_stats_repository = udp_stats_repository.get_stats().await;
106+
107+
// Merge the metrics from the HTTP and UDP metrics
108+
let mut metrics = MetricCollection::default();
109+
metrics
110+
.merge(&http_stats.metric_collection)
111+
.expect("msg: failed to merge HTTP core metrics");
112+
metrics
113+
.merge(&udp_stats_repository.metric_collection)
114+
.expect("failed to merge UDP core metrics");
115+
116+
TrackerLabeledMetrics { metrics }
103117
}
104118

105119
#[cfg(test)]

0 commit comments

Comments
 (0)