Skip to content

Commit

Permalink
Merge pull request #52 from Ptrskay3/change-default-prometheus-recorder
Browse files Browse the repository at this point in the history
Change default initialization of PrometheusHandle
  • Loading branch information
Ptrskay3 authored Jul 20, 2024
2 parents aaf0a29 + affb87a commit 0bcc34f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ All notable changes to this project will be documented in this file.
### Changed

- `MakeDefaultHandle::make_default_handle` now takes `self` as argument. This allows custom implementor structs to hold non-static data. [\#49]
- Change the default initialization of `PrometheusHandle` to prevent unbounded memory growth of histograms. [\#52]
- Bump `metrics` to `0.23`, `metrics-exporter-prometheus` to `0.15`. [\#52]
- Document MSRV as 1.70 currently. [\#52]

### Added

Expand Down
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ repository = "https://github.com/Ptrskay3/axum-prometheus"

[dependencies]
axum = "0.7.1"
futures = "0.3.23"
http = "1.0.0"
http-body = "1.0.0"
metrics = "0.22.0"
metrics-exporter-prometheus = { version = "0.14.0", optional = true, default-features = false, features = ["http-listener"] }
metrics = "0.23.0"
metrics-exporter-prometheus = { version = "0.15.0", optional = true, default-features = false, features = ["http-listener"] }
pin-project = "1.0.12"
tower = "0.4.13"
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"] }
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ AXUM_HTTP_RESPONSE_BODY_SIZE = "my_app_response_body_size"
| `0.6` | `0.2`, `0.3`, `0.4` |
| `0.7` | `0.5`, `0.6` |

#### MSRV

This crate's current MSRV is 1.70.

## Usage

For more elaborate use-cases, see the [`builder example`](examples/builder-example/).
Expand Down
2 changes: 1 addition & 1 deletion examples/exporter-statsd-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ axum = "0.7.1"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
metrics-exporter-statsd = "0.7.0"
metrics-exporter-statsd = "0.8.0"
axum-prometheus = { path = "../../", default-features = false }

34 changes: 19 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

mod builder;
Expand Down Expand Up @@ -736,21 +737,24 @@ pub struct Handle(pub PrometheusHandle);
#[cfg(feature = "prometheus")]
impl Default for Handle {
fn default() -> Self {
Self(
PrometheusBuilder::new()
.set_buckets_for_metric(
Matcher::Full(
PREFIXED_HTTP_REQUESTS_DURATION_SECONDS
.get()
.map_or(AXUM_HTTP_REQUESTS_DURATION_SECONDS, |s| s.as_str())
.to_string(),
),
utils::SECONDS_DURATION_BUCKETS,
)
.unwrap()
.install_recorder()
.unwrap(),
)
// TODO: Handle the push gateway feature somehow.
let (recorder, _) = PrometheusBuilder::new()
.upkeep_timeout(Duration::from_secs(5))
.set_buckets_for_metric(
Matcher::Full(
PREFIXED_HTTP_REQUESTS_DURATION_SECONDS
.get()
.map_or(AXUM_HTTP_REQUESTS_DURATION_SECONDS, |s| s.as_str())
.to_string(),
),
utils::SECONDS_DURATION_BUCKETS,
)
.unwrap()
.build()
.expect("Failed to build metrics recorder");
let handle = recorder.handle();
metrics::set_global_recorder(recorder).expect("Failed to set global recorder");
Self(handle)
}
}

Expand Down

0 comments on commit 0bcc34f

Please sign in to comment.