Skip to content

Commit 5455e4e

Browse files
committed
feat: add metrics endpoint
1 parent b0af435 commit 5455e4e

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

Cargo.lock

+132-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ axum = { version = "0", features = ["macros"] }
3636
axum-client-ip = "0"
3737
axum-extra = { version = "0", features = ["query"] }
3838
axum-server = { version = "0", features = ["tls-rustls-no-provider"] }
39+
axum-prometheus = "0.7.0"
3940
bittorrent-http-protocol = { version = "3.0.0-develop", path = "packages/http-protocol" }
4041
bittorrent-primitives = "0.1.0"
4142
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }

src/servers/http/v1/routes.rs

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::response::Response;
99
use axum::routing::get;
1010
use axum::{BoxError, Router};
1111
use axum_client_ip::SecureClientIpSource;
12+
use axum_prometheus::PrometheusMetricLayer;
1213
use hyper::{Request, StatusCode};
1314
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
1415
use tower::timeout::TimeoutLayer;
@@ -30,6 +31,8 @@ use crate::servers::http::HTTP_TRACKER_LOG_TARGET;
3031
#[allow(clippy::needless_pass_by_value)]
3132
#[instrument(skip(tracker, server_socket_addr))]
3233
pub fn router(tracker: Arc<Tracker>, server_socket_addr: SocketAddr) -> Router {
34+
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
35+
3336
Router::new()
3437
// Health check
3538
.route("/health_check", get(health_check::handler))
@@ -39,6 +42,8 @@ pub fn router(tracker: Arc<Tracker>, server_socket_addr: SocketAddr) -> Router {
3942
// Scrape request
4043
.route("/scrape", get(scrape::handle_without_key).with_state(tracker.clone()))
4144
.route("/scrape/:key", get(scrape::handle_with_key).with_state(tracker))
45+
.route("/metrics", get(|| async move { metric_handle.render() }))
46+
.layer(prometheus_layer)
4247
// Add extension to get the client IP from the connection info
4348
.layer(SecureClientIpSource::ConnectInfo.into_extension())
4449
.layer(CompressionLayer::new())

0 commit comments

Comments
 (0)