Skip to content

Commit 88560c3

Browse files
authored
Quorum: Add metrics over http urls (#2804)
1 parent cfa52f5 commit 88560c3

File tree

7 files changed

+245
-18
lines changed

7 files changed

+245
-18
lines changed

apps/quorum/Cargo.lock

Lines changed: 120 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/quorum/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "quorum"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[dependencies]
@@ -23,3 +23,4 @@ time = "0.3.41"
2323
serde_json = "1.0.140"
2424
futures = "0.3.31"
2525
serde_wormhole = "0.1.0"
26+
axum-prometheus = "0.8.0"

apps/quorum/src/api.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use axum::{
33
routing::{get, post},
44
Json, Router,
55
};
6+
use axum_prometheus::{EndpointLabel, PrometheusMetricLayerBuilder};
67
use secp256k1::{
78
ecdsa::{RecoverableSignature, RecoveryId},
89
Message, Secp256k1,
@@ -26,16 +27,24 @@ pub type Payload<'a> = &'a RawMessage;
2627
pub async fn run(listen_address: SocketAddr, state: State) -> anyhow::Result<()> {
2728
tracing::info!("Starting server...");
2829

30+
let (prometheus_layer, _) = PrometheusMetricLayerBuilder::new()
31+
.with_metrics_from_fn(|| state.metrics_recorder.clone())
32+
.with_endpoint_label_type(EndpointLabel::MatchedPathWithFallbackFn(|_| {
33+
"unknown".to_string()
34+
}))
35+
.build_pair();
36+
2937
let routes = Router::new()
3038
.route("/live", get(|| async { "OK" }))
3139
.route("/observation", post(post_observation))
3240
.route("/ws", get(ws_route_handler))
41+
.layer(prometheus_layer)
3342
.with_state(state);
3443
let listener = tokio::net::TcpListener::bind(&listen_address).await?;
3544

3645
axum::serve(listener, routes)
3746
.with_graceful_shutdown(async {
38-
let _ = crate::server::EXIT.subscribe().changed().await;
47+
crate::server::wait_for_exit().await;
3948
tracing::info!("Shutting down server...");
4049
})
4150
.await?;

apps/quorum/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::IsTerminal;
44
use crate::server::RunOptions;
55

66
mod api;
7+
mod metrics_server;
78
mod pythnet;
89
mod server;
910
mod ws;

apps/quorum/src/metrics_server.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use axum::{routing::get, Router};
2+
use axum_prometheus::{
3+
metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle},
4+
PrometheusMetricLayerBuilder,
5+
};
6+
7+
use crate::server::{wait_for_exit, RunOptions, State};
8+
9+
pub const DEFAULT_METRICS_BUCKET: &[f64; 20] = &[
10+
0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.25, 1.5, 2.0,
11+
3.0, 5.0, 10.0,
12+
];
13+
14+
pub fn setup_metrics_recorder() -> anyhow::Result<PrometheusHandle> {
15+
PrometheusBuilder::new()
16+
.set_buckets(DEFAULT_METRICS_BUCKET)?
17+
.install_recorder()
18+
.map_err(|err| anyhow::anyhow!("Failed to set up metrics recorder: {:?}", err))
19+
}
20+
21+
pub async fn run(run_options: RunOptions, state: State) -> anyhow::Result<()> {
22+
tracing::info!("Starting Metrics Server...");
23+
24+
let (_, metric_handle) = PrometheusMetricLayerBuilder::new()
25+
.with_metrics_from_fn(|| state.metrics_recorder.clone())
26+
.build_pair();
27+
let app = Router::new();
28+
let app = app.route("/metrics", get(|| async move { metric_handle.render() }));
29+
30+
let listener = tokio::net::TcpListener::bind(&run_options.server.metrics_addr).await?;
31+
axum::serve(listener, app)
32+
.with_graceful_shutdown(async {
33+
let _ = wait_for_exit().await;
34+
tracing::info!("Shutting down metrics server...");
35+
})
36+
.await?;
37+
Ok(())
38+
}

0 commit comments

Comments
 (0)