Skip to content

Commit 227d96c

Browse files
committed
feat: enable prometheus format in new http tracker core metrics endpoint
1 parent 7465b4a commit 227d96c

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::Deserialize;
1111
use tokio::sync::RwLock;
1212
use torrust_rest_tracker_api_core::statistics::services::{get_labeled_metrics, get_metrics};
1313

14-
use super::responses::{labeled_stats_response, 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
///
@@ -58,6 +58,12 @@ pub async fn get_stats_handler(
5858
}
5959
}
6060

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.
6167
#[allow(clippy::type_complexity)]
6268
pub async fn get_metrics_handler(
6369
State(state): State<(
@@ -73,7 +79,7 @@ pub async fn get_metrics_handler(
7379
match params.0.format {
7480
Some(format) => match format {
7581
Format::Json => labeled_stats_response(metrics),
76-
Format::Prometheus => todo!(),
82+
Format::Prometheus => labeled_metrics_response(&metrics),
7783
},
7884
None => labeled_stats_response(metrics),
7985
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
//! API context.
33
use axum::response::{IntoResponse, Json, Response};
44
use torrust_rest_tracker_api_core::statistics::services::{TrackerLabeledMetrics, TrackerMetrics};
5+
use torrust_tracker_metrics::prometheus::PrometheusSerializable;
56

67
use super::resources::{LabeledStats, Stats};
78

8-
/// `200` response that contains the [`Stats`] resource as json.
9-
#[must_use]
10-
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
11-
Json(Stats::from(tracker_metrics)).into_response()
12-
}
13-
149
/// `200` response that contains the [`LabeledStats`] resource as json.
1510
#[must_use]
1611
pub fn labeled_stats_response(tracker_metrics: TrackerLabeledMetrics) -> Response {
1712
Json(LabeledStats::from(tracker_metrics)).into_response()
1813
}
1914

15+
#[must_use]
16+
pub fn labeled_metrics_response(tracker_metrics: &TrackerLabeledMetrics) -> Response {
17+
tracker_metrics.metrics.to_prometheus().into_response()
18+
}
19+
20+
/// `200` response that contains the [`Stats`] resource as json.
21+
#[must_use]
22+
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
23+
Json(Stats::from(tracker_metrics)).into_response()
24+
}
25+
2026
/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format .
2127
#[allow(deprecated)]
2228
#[must_use]

0 commit comments

Comments
 (0)