Skip to content

Commit

Permalink
Update version of metrics to fix integration with other tools
Browse files Browse the repository at this point in the history
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 metrics-rs/metrics#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.
  • Loading branch information
bltavares committed Jan 22, 2024
1 parent 4601af8 commit a687bf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<FailureClass> for Traffic<'a> {
Expand Down Expand Up @@ -396,14 +396,14 @@ impl<'a, FailureClass> Callbacks<FailureClass> 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,
Expand All @@ -426,14 +426,14 @@ impl<'a, FailureClass> Callbacks<FailureClass> 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()),
Expand All @@ -444,12 +444,12 @@ impl<'a, FailureClass> Callbacks<FailureClass> 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);
}
}
}
Expand Down

0 comments on commit a687bf6

Please sign in to comment.