From a687bf660ea60b7418f0efb30c735d9af2e1b817 Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Fri, 5 Jan 2024 15:33:45 -0300 Subject: [PATCH] Update version of metrics to fix integration with other tools The current version of metrics (0.21.0) uses an older version of macros that point to different global allocators than the current latest version (0.22.0) causing metrics to not be reported by other tools integrating with the `metrics` facade. Since https://github.com/metrics-rs/metrics/pull/414, the `metrics` project now can have different registries, and such, the macros (eg: `counter!`) have been refactored. As this is a pre-1.0 project, including axum-prometheus into a project with `metrics:0.22.0` register 2 different versions of the metrics crate. So metrics reported by `axum-prometheus` will be registered on the `metrics:0.21.0` global collector, while the project's metrics will use the `metrics:0.22.0` global collector. While the current suggested method of exporting the `let (_, handle)` as a route will work, as it's pointing to the `metrics:0.21.0` collectors, other integrations such as the push-based background task will miss out the layer metrics. By upgrading this project to `metrics:0.22.0` we can ensure it's reporting to the same global collector, and thus integrated with other `metrics`-based tools. This commit bumps the metrics version to 0.22.0 as well as fix the macro invocations introduced on the new version. --- Cargo.toml | 4 ++-- src/lib.rs | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d5bc61b..93c9b1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ axum = "0.7.1" futures = "0.3.23" http = "1.0.0" http-body = "1.0.0" -metrics = "0.21.0" -metrics-exporter-prometheus = { version = "0.12.0", optional = true } +metrics = "0.22.0" +metrics-exporter-prometheus = { version = "0.13.0", optional = true } pin-project = "1.0.12" tower = "0.4.13" tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"] } diff --git a/src/lib.rs b/src/lib.rs index 1f9694c..a3aa7d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,7 +198,7 @@ use builder::{LayerOnly, Paired}; use lifecycle::layer::LifeCycleLayer; use lifecycle::OnBodyChunk; use lifecycle::{service::LifeCycle, Callbacks}; -use metrics::{decrement_gauge, histogram, increment_counter, increment_gauge}; +use metrics::{counter, gauge, histogram}; use once_cell::sync::OnceCell; use tower::Layer; use tower_http::classify::{ClassifiedResponse, SharedClassifier, StatusInRangeAsFailures}; @@ -358,7 +358,7 @@ fn body_size_histogram(metrics_data: &MetricsData) { let response_body_size = PREFIXED_HTTP_RESPONSE_BODY_SIZE .get() .map_or(AXUM_HTTP_RESPONSE_BODY_SIZE, |s| s.as_str()); - metrics::histogram!(response_body_size, metrics_data.body_size, labels); + metrics::histogram!(response_body_size, labels).record(metrics_data.body_size); } impl<'a, FailureClass> Callbacks for Traffic<'a> { @@ -396,14 +396,14 @@ impl<'a, FailureClass> Callbacks for Traffic<'a> { let requests_pending = PREFIXED_HTTP_REQUESTS_PENDING .get() .map_or(AXUM_HTTP_REQUESTS_PENDING, |s| s.as_str()); - increment_gauge!( + gauge!( requests_pending, - 1.0, &[ ("method", method.to_owned()), ("endpoint", endpoint.clone()), ] - ); + ) + .increment(1.0); Some(MetricsData { endpoint, @@ -426,14 +426,14 @@ impl<'a, FailureClass> Callbacks for Traffic<'a> { let requests_pending = PREFIXED_HTTP_REQUESTS_PENDING .get() .map_or(AXUM_HTTP_REQUESTS_PENDING, |s| s.as_str()); - decrement_gauge!( + gauge!( requests_pending, - 1.0, &[ ("method", data.method.to_string()), ("endpoint", data.endpoint.to_string()), ] - ); + ) + .decrement(1.0); let labels = [ ("method", data.method.to_string()), @@ -444,12 +444,12 @@ impl<'a, FailureClass> Callbacks for Traffic<'a> { let requests_total = PREFIXED_HTTP_REQUESTS_TOTAL .get() .map_or(AXUM_HTTP_REQUESTS_TOTAL, |s| s.as_str()); - increment_counter!(requests_total, &labels); + counter!(requests_total, &labels).increment(1); let requests_duration = PREFIXED_HTTP_REQUESTS_DURATION_SECONDS .get() .map_or(AXUM_HTTP_REQUESTS_DURATION_SECONDS, |s| s.as_str()); - histogram!(requests_duration, duration_seconds, &labels); + histogram!(requests_duration, &labels).record(duration_seconds); } } }