Skip to content

Commit

Permalink
Upgrade metrics crate to 0.22 version (#9)
Browse files Browse the repository at this point in the history
- upgrade `metrics-util` crate to 0.16 version
  • Loading branch information
zohnannor authored Dec 25, 2023
1 parent 964cc1c commit ef932a0
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 174 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ All user visible changes to this project will be documented in this file. This p



## [0.6.0] · 2023-??-??
[0.6.0]: /../../tree/v0.6.0

[Diff](/../../compare/v0.5.0...v0.6.0)

### BC Breaks

- Upgraded to 0.22 version of `metrics` crate. ([#9])
- Upgraded to 0.16 version of `metrics-util` crate. ([#9])

[#9]: /../../pull/9




## [0.5.0] · 2023-09-06
[0.5.0]: /../../tree/v0.5.0

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ include = ["/src/", "/CHANGELOG.md", "/LICENSE-APACHE", "/LICENSE-MIT", "/README

[dependencies]
arc-swap = "1.5"
metrics = { version = "0.21", default-features = false }
metrics-util = { version = "0.15", features = ["registry"], default-features = false }
metrics = { version = "0.22", default-features = false }
metrics-util = { version = "0.16", features = ["registry"], default-features = false }
once_cell = "1.16"
prometheus = { version = "0.13", default-features = false }
sealed = "0.5"
Expand All @@ -28,4 +28,4 @@ smallvec = "1.10"
thiserror = "1.0.2"

[dev-dependencies]
metrics-util = { version = "0.15", features = ["layer-filter"], default-features = false }
metrics-util = { version = "0.16", features = ["layer-filter"], default-features = false }
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ To satisfy the [`metrics::Recorder`]'s requirement of allowing changing metrics
let recorder = metrics_prometheus::install();

// Either use `metrics` crate interfaces.
metrics::increment_counter!("count", "whose" => "mine", "kind" => "owned");
metrics::increment_counter!("count", "whose" => "mine", "kind" => "ref");
metrics::increment_counter!("count", "kind" => "owned", "whose" => "dummy");
metrics::counter!("count", "whose" => "mine", "kind" => "owned").increment(1);
metrics::counter!("count", "whose" => "mine", "kind" => "ref").increment(1);
metrics::counter!("count", "kind" => "owned", "whose" => "dummy").increment(1);

// Or construct and provide `prometheus` metrics directly.
recorder.register_metric(prometheus::Gauge::new("value", "help")?);
Expand Down Expand Up @@ -107,7 +107,7 @@ metrics::describe_counter!("count", "Another description.");

// Even before a metric is registered in `prometheus::Registry`.
metrics::describe_counter!("another", "Yet another counter.");
metrics::increment_counter!("another");
metrics::counter!("another").increment(1);

let report = prometheus::TextEncoder::new()
.encode_to_string(&recorder.registry().gather())?;
Expand Down Expand Up @@ -141,39 +141,39 @@ Since [`prometheus`] crate validates the metrics format very strictly, not every
metrics_prometheus::install();
// panics: 'queries.count' is not a valid metric name
metrics::increment_counter!("queries.count");
metrics::counter!("queries.count").increment(1);
```

- The same metric should use always the same set of labels:
```rust,should_panic
metrics_prometheus::install();
metrics::increment_counter!("count");
metrics::counter!("count").increment(1);
// panics: Inconsistent label cardinality, expect 0 label values, but got 1
metrics::increment_counter!("count", "whose" => "mine");
metrics::counter!("count", "whose" => "mine").increment(1);
```
```rust,should_panic
metrics_prometheus::install();
metrics::increment_counter!("count", "kind" => "owned");
metrics::counter!("count", "kind" => "owned").increment(1);
// panics: label name kind missing in label map
metrics::increment_counter!("count", "whose" => "mine");
metrics::counter!("count", "whose" => "mine").increment(1);
```
```rust,should_panic
metrics_prometheus::install();
metrics::increment_counter!("count", "kind" => "owned");
metrics::counter!("count", "kind" => "owned").increment(1);
// panics: Inconsistent label cardinality, expect 1 label values, but got 2
metrics::increment_counter!("count", "kind" => "ref", "whose" => "mine");
metrics::counter!("count", "kind" => "ref", "whose" => "mine").increment(1);
```

- The same name cannot be used for different types of metrics:
```rust,should_panic
metrics_prometheus::install();
metrics::increment_counter!("count");
metrics::counter!("count").increment(1);
// panics: Duplicate metrics collector registration attempted
metrics::increment_gauge!("count", 1.0);
metrics::gauge!("count").increment(1.0);
```

- Any metric registered in a [`prometheus::Registry`] directly, without using [`metrics`] or this crate interfaces, is not usable via [`metrics`] facade and will cause a [`prometheus::Error`].
Expand All @@ -184,7 +184,7 @@ Since [`prometheus`] crate validates the metrics format very strictly, not every
.register(Box::new(prometheus::Gauge::new("value", "help")?))?;
// panics: Duplicate metrics collector registration attempted
metrics::increment_gauge!("value", 4.5);
metrics::gauge!("value").increment(4.5);
# Ok::<_, prometheus::Error>(())
```

Expand All @@ -205,7 +205,7 @@ metrics_prometheus::Recorder::builder()
.build_and_install();

// `prometheus::Error` is ignored inside.
metrics::increment_counter!("invalid.name");
metrics::counter!("invalid.name").increment(1);

let stats = prometheus::default_registry().gather();
assert_eq!(stats.len(), 0);
Expand Down
30 changes: 18 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,38 @@ pub use self::{
};

/// Tries to install a default [`Recorder`] (backed by the
/// [`prometheus::default_registry()`]) as [`metrics::recorder()`].
/// [`prometheus::default_registry()`]) with the
/// [`metrics::set_global_recorder()`].
///
/// # Errors
///
/// If the [`Recorder`] fails to be installed as [`metrics::recorder()`].
pub fn try_install() -> Result<Recorder, metrics::SetRecorderError> {
/// If the [`Recorder`] fails to be installed with the
/// [`metrics::set_global_recorder()`].
pub fn try_install() -> Result<Recorder, metrics::SetRecorderError<Recorder>> {
Recorder::builder().try_build_and_install()
}

/// Tries to install a default [`FreezableRecorder`] (backed by the
/// [`prometheus::default_registry()`]) as [`metrics::recorder()`].
/// [`prometheus::default_registry()`]) with the
/// [`metrics::set_global_recorder()`].
///
/// # Errors
///
/// If the [`FreezableRecorder`] fails to be installed as
/// [`metrics::recorder()`].
/// If the [`FreezableRecorder`] fails to be installed with the
/// [`metrics::set_global_recorder()`].
pub fn try_install_freezable(
) -> Result<FreezableRecorder, metrics::SetRecorderError> {
) -> Result<FreezableRecorder, metrics::SetRecorderError<FreezableRecorder>> {
Recorder::builder().try_build_freezable_and_install()
}

/// Installs a default [`Recorder`] (backed by the
/// [`prometheus::default_registry()`]) as [`metrics::recorder()`].
/// [`prometheus::default_registry()`]) with the
/// [`metrics::set_global_recorder()`].
///
/// # Panics
///
/// If the [`Recorder`] fails to be installed as [`metrics::recorder()`].
/// If the [`Recorder`] fails to be installed with the
/// [`metrics::set_global_recorder()`].
// We do intentionally omit `#[must_use]` here, as we don't want to force
// library users using the returned `Recorder` directly.
#[allow(clippy::must_use_candidate)]
Expand All @@ -226,12 +231,13 @@ pub fn install() -> Recorder {
}

/// Installs a default [`FreezableRecorder`] (backed by the
/// [`prometheus::default_registry()`]) as [`metrics::recorder()`].
/// [`prometheus::default_registry()`]) with the
/// [`metrics::set_global_recorder()`].
///
/// # Panics
///
/// If the [`FreezableRecorder`] fails to be installed as
/// [`metrics::recorder()`].
/// If the [`FreezableRecorder`] fails to be installed with the
/// [`metrics::set_global_recorder()`].
// We do intentionally omit `#[must_use]` here, as we don't want to force
// library users using the returned `FreezableRecorder` directly.
#[allow(clippy::must_use_candidate)]
Expand Down
86 changes: 52 additions & 34 deletions src/recorder/freezable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ use super::Builder;
/// let recorder = metrics_prometheus::install_freezable();
///
/// // Either use `metrics` crate interfaces.
/// metrics::increment_counter!("count", "whose" => "mine", "kind" => "owned");
/// metrics::increment_counter!("count", "whose" => "mine", "kind" => "ref");
/// metrics::increment_counter!("count", "kind" => "owned", "whose" => "dummy");
/// metrics::counter!(
/// "count", "whose" => "mine", "kind" => "owned",
/// ).increment(1);
/// metrics::counter!(
/// "count", "whose" => "mine", "kind" => "ref",
/// ).increment(1);
/// metrics::counter!(
/// "count", "kind" => "owned", "whose" => "dummy",
/// ).increment(1);
///
/// // Or construct and provide `prometheus` metrics directly.
/// recorder.register_metric(prometheus::Gauge::new("value", "help")?);
Expand All @@ -50,7 +56,7 @@ use super::Builder;
///
/// // However, you cannot register new metrics after freezing.
/// // This is just no-op.
/// metrics::increment_gauge!("new", 2.0);
/// metrics::gauge!("new").increment(2.0);
///
/// let report = prometheus::TextEncoder::new()
/// .encode_to_string(&prometheus::default_registry().gather())?;
Expand Down Expand Up @@ -130,12 +136,12 @@ use super::Builder;
/// .with_failure_strategy(strategy::Panic)
/// .build_freezable_and_install();
///
/// metrics::increment_counter!("count", "kind" => "owned");
/// metrics::counter!("count", "kind" => "owned").increment(1);
///
/// recoder.freeze();
///
/// // This panics, as such labeling is not allowed by `prometheus` crate.
/// metrics::increment_counter!("count", "whose" => "mine");
/// metrics::counter!("count", "whose" => "mine").increment(1);
/// ```
///
/// [`AtomicBool`]: std::sync::atomic::AtomicBool
Expand Down Expand Up @@ -202,7 +208,7 @@ impl<S> Recorder<S> {
/// recorder.registry().register(Box::new(counter))?;
///
/// // panics: Duplicate metrics collector registration attempted
/// metrics::increment_counter!("value");
/// metrics::counter!("value").increment(1);
/// # Ok::<_, prometheus::Error>(())
/// ```
///
Expand Down Expand Up @@ -262,15 +268,15 @@ impl<S> Recorder<S> {
/// // No-op, as the `Recorder` has been frozen.
/// recorder.try_register_metric(prometheus::Gauge::new("new", "help")?)?;
///
/// metrics::increment_counter!(
/// metrics::counter!(
/// "value", "whose" => "mine", "kind" => "owned",
/// );
/// metrics::increment_counter!(
/// ).increment(1);
/// metrics::counter!(
/// "value", "whose" => "mine", "kind" => "ref",
/// );
/// metrics::increment_counter!(
/// ).increment(1);
/// metrics::counter!(
/// "value", "kind" => "owned", "whose" => "foreign",
/// );
/// ).increment(1);
///
/// let report = prometheus::TextEncoder::new()
/// .encode_to_string(&recorder.registry().gather())?;
Expand Down Expand Up @@ -356,15 +362,15 @@ impl<S> Recorder<S> {
/// // No-op, as the `Recorder` has been frozen.
/// recorder.register_metric(prometheus::Gauge::new("new", "help")?);
///
/// metrics::increment_gauge!(
/// "value", 2.0, "whose" => "mine", "kind" => "owned",
/// );
/// metrics::decrement_gauge!(
/// "value", 2.0, "whose" => "mine", "kind" => "ref",
/// );
/// metrics::increment_gauge!(
/// "value", 2.0, "kind" => "owned", "whose" => "foreign",
/// );
/// metrics::gauge!(
/// "value", "whose" => "mine", "kind" => "owned",
/// ).increment(2.0);
/// metrics::gauge!(
/// "value","whose" => "mine", "kind" => "ref",
/// ).decrement(2.0);
/// metrics::gauge!(
/// "value", "kind" => "owned", "whose" => "foreign",
/// ).increment(2.0);
///
/// let report = prometheus::TextEncoder::new()
/// .encode_to_string(&recorder.registry().gather())?;
Expand Down Expand Up @@ -424,7 +430,7 @@ impl<S> Recorder<S> {
/// ```rust
/// let recorder = metrics_prometheus::install_freezable();
///
/// metrics::increment_counter!("count");
/// metrics::counter!("count").increment(1);
///
/// let report = prometheus::TextEncoder::new()
/// .encode_to_string(&recorder.registry().gather())?;
Expand All @@ -440,9 +446,9 @@ impl<S> Recorder<S> {
///
/// recorder.freeze();
///
/// metrics::increment_counter!("count");
/// metrics::counter!("count").increment(1);
/// // This is no-op.
/// metrics::increment_counter!("new");
/// metrics::counter!("new").increment(1);
///
/// let report = prometheus::TextEncoder::new()
/// .encode_to_string(&recorder.registry().gather())?;
Expand Down Expand Up @@ -519,24 +525,36 @@ where
}
}

fn register_counter(&self, key: &metrics::Key) -> metrics::Counter {
fn register_counter(
&self,
key: &metrics::Key,
meta: &metrics::Metadata<'_>,
) -> metrics::Counter {
self.frozen.get().map_or_else(
|| self.usual.register_counter(key),
|frozen| frozen.register_counter(key),
|| self.usual.register_counter(key, meta),
|frozen| frozen.register_counter(key, meta),
)
}

fn register_gauge(&self, key: &metrics::Key) -> metrics::Gauge {
fn register_gauge(
&self,
key: &metrics::Key,
meta: &metrics::Metadata<'_>,
) -> metrics::Gauge {
self.frozen.get().map_or_else(
|| self.usual.register_gauge(key),
|frozen| frozen.register_gauge(key),
|| self.usual.register_gauge(key, meta),
|frozen| frozen.register_gauge(key, meta),
)
}

fn register_histogram(&self, key: &metrics::Key) -> metrics::Histogram {
fn register_histogram(
&self,
key: &metrics::Key,
meta: &metrics::Metadata<'_>,
) -> metrics::Histogram {
self.frozen.get().map_or_else(
|| self.usual.register_histogram(key),
|frozen| frozen.register_histogram(key),
|| self.usual.register_histogram(key, meta),
|frozen| frozen.register_histogram(key, meta),
)
}
}
Loading

0 comments on commit ef932a0

Please sign in to comment.