Skip to content

Commit 496e98a

Browse files
authored
Update metrics and fix Prometheus v3 breakage (#89)
* Update `metrics` to latest version * Explicitly define Content-Type in `/metrics` response to fix Prometheus v3 breakage
1 parent 9c2d88d commit 496e98a

File tree

3 files changed

+54
-97
lines changed

3 files changed

+54
-97
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1818
twilight-http-ratelimiting = { version = "0.16.0" }
1919

2020
# Only used by the `expose-metrics` feature.
21-
metrics = { version = "0.21", optional = true }
22-
metrics-exporter-prometheus = { version = "0.12", default-features = false, optional = true }
23-
metrics-util = { version = "0.15", optional = true }
21+
metrics = { version = "0.24", optional = true }
22+
metrics-exporter-prometheus = { version = "0.16", default-features = false, optional = true }
23+
metrics-util = { version = "0.19", optional = true }
2424
lazy_static = { version = "1.4", optional = true }
2525

2626
[dev-dependencies]

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ use tokio::signal::unix::{signal, SignalKind};
3535
#[cfg(feature = "expose-metrics")]
3636
use std::time::Instant;
3737

38+
#[cfg(feature = "expose-metrics")]
39+
use http::header::CONTENT_TYPE;
3840
#[cfg(feature = "expose-metrics")]
3941
use lazy_static::lazy_static;
4042
#[cfg(feature = "expose-metrics")]
@@ -98,7 +100,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
98100
)
99101
.build_recorder();
100102
handle = Arc::new(recorder.handle());
101-
metrics::set_boxed_recorder(Box::new(recorder))
103+
metrics::set_global_recorder(Box::new(recorder))
102104
.expect("Failed to create metrics receiver!");
103105
}
104106

@@ -401,7 +403,8 @@ async fn handle_request(
401403
.and_then(|header| header.to_str().ok())
402404
.unwrap_or("")
403405
.to_string();
404-
histogram!(METRIC_KEY.as_str(), end - start, "method"=>m.to_string(), "route"=>p, "status"=>status.to_string(), "scope" => scope);
406+
histogram!(METRIC_KEY.as_str(), "method"=>m.to_string(), "route"=>p, "status"=>status.to_string(), "scope" => scope)
407+
.record(end - start);
405408
}
406409

407410
debug!("{} {} ({}): {}", m, p, request_path, status);
@@ -412,6 +415,10 @@ async fn handle_request(
412415
#[cfg(feature = "expose-metrics")]
413416
fn handle_metrics(handle: Arc<PrometheusHandle>) -> Response<Body> {
414417
Response::builder()
418+
.header(
419+
CONTENT_TYPE,
420+
HeaderValue::from_static("text/plain; version=0.0.4"),
421+
)
415422
.body(Body::from(handle.render()))
416423
.unwrap()
417424
}

0 commit comments

Comments
 (0)