Skip to content

Commit

Permalink
Bump to metrics 0.22
Browse files Browse the repository at this point in the history
metrics-rs/metrics@metrics-v0.21.1...metrics-v0.22.0

There are some breaking changes:

- metrics-rs/metrics#414 Added a generic recorder to
`SetRecorderError` and removed `set_boxed_recorder`
- metrics-rs/metrics#380 Added metadata to the metrics
  • Loading branch information
ijc committed Nov 13, 2024
1 parent eed983f commit b7a1066
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ categories = ["development-tools::debugging"]
keywords = ["metrics", "telemetry", "statsd"]

[dependencies]
metrics = "0.21"
metrics-util = { version = "0.15", default-features = false, features = ["recency", "registry", "summary"] }
metrics = "0.22"
metrics-util = { version = "0.16", default-features = false, features = ["recency", "registry", "summary"] }
thiserror = { version = "1", default-features = false }
quanta = { version = "0.11.0", default-features = false }
quanta = { version = "0.12.0", default-features = false }
indexmap = { version = "1", default-features = false }

tokio = { version = "1", features = ["rt", "net", "time"] }
Expand Down
56 changes: 29 additions & 27 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl StatsdBuilder {
recorder
};

metrics::set_boxed_recorder(Box::new(recorder))?;
metrics::set_global_recorder(recorder)?;

Ok(())
}
Expand Down Expand Up @@ -495,11 +495,13 @@ async fn send_all(
#[cfg(test)]
mod tests {
use super::{split_in_packets, Matcher, StatsdBuilder};
use metrics::{Key, KeyName, Label, Recorder};
use metrics::{Key, KeyName, Label, Level, Metadata, Recorder};
use metrics_util::MetricKindMask;
use quanta::Clock;
use std::time::Duration;

static METADATA: Metadata = Metadata::new(module_path!(), Level::INFO, Some(module_path!()));

#[test]
fn test_split_packet() {
let data = "123456789\n12345\n678\n";
Expand Down Expand Up @@ -600,7 +602,7 @@ mod tests {
.build_recorder();

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let handle = recorder.handle();
Expand All @@ -610,15 +612,15 @@ mod tests {

let labels = vec![Label::new("wutang", "forever")];
let key = Key::from_parts("basic.gauge", labels);
let gauge1 = recorder.register_gauge(&key);
let gauge1 = recorder.register_gauge(&key, &METADATA);
gauge1.set(-3.44);
let rendered = handle.render();
// each render call will reset the value of the counter
let expected_gauge = "basic.gauge:-3.44|g|#wutang:forever\n\n";
assert_eq!(rendered, expected_gauge);

let key = Key::from_name("basic.histogram");
let histogram1 = recorder.register_histogram(&key);
let histogram1 = recorder.register_histogram(&key, &METADATA);
histogram1.record(12.0);
let rendered = handle.render();

Expand Down Expand Up @@ -659,19 +661,19 @@ mod tests {
.build_recorder();

let full_key = Key::from_name("metrics.testing_foo");
let full_key_histo = recorder.register_histogram(&full_key);
let full_key_histo = recorder.register_histogram(&full_key, &METADATA);
full_key_histo.record(FULL_VALUES[0]);

let prefix_key = Key::from_name("metrics.testing_bar");
let prefix_key_histo = recorder.register_histogram(&prefix_key);
let prefix_key_histo = recorder.register_histogram(&prefix_key, &METADATA);
prefix_key_histo.record(PREFIX_VALUES[1]);

let suffix_key = Key::from_name("metrics.testin_foo");
let suffix_key_histo = recorder.register_histogram(&suffix_key);
let suffix_key_histo = recorder.register_histogram(&suffix_key, &METADATA);
suffix_key_histo.record(SUFFIX_VALUES[2]);

let default_key = Key::from_name("metrics.wee");
let default_key_histo = recorder.register_histogram(&default_key);
let default_key_histo = recorder.register_histogram(&default_key, &METADATA);
default_key_histo.record(DEFAULT_VALUES[2] + 1.0);

let full_data = concat!(
Expand Down Expand Up @@ -735,15 +737,15 @@ mod tests {
.build_with_clock(clock);

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let key = Key::from_name("basic.gauge");
let gauge1 = recorder.register_gauge(&key);
let gauge1 = recorder.register_gauge(&key, &METADATA);
gauge1.set(-3.44);

let key = Key::from_name("basic.histogram");
let histo1 = recorder.register_histogram(&key);
let histo1 = recorder.register_histogram(&key, &METADATA);
histo1.record(1.0);

let handle = recorder.handle();
Expand Down Expand Up @@ -792,15 +794,15 @@ mod tests {
.build_with_clock(clock);

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let key = Key::from_name("basic.gauge");
let gauge1 = recorder.register_gauge(&key);
let gauge1 = recorder.register_gauge(&key, &METADATA);
gauge1.set(-3.44);

let key = Key::from_name("basic.histogram");
let histo1 = recorder.register_histogram(&key);
let histo1 = recorder.register_histogram(&key, &METADATA);
histo1.record(1.0);

let handle = recorder.handle();
Expand Down Expand Up @@ -840,15 +842,15 @@ mod tests {
.build_with_clock(clock);

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let key = Key::from_name("basic.gauge");
let gauge1 = recorder.register_gauge(&key);
let gauge1 = recorder.register_gauge(&key, &METADATA);
gauge1.set(-3.44);

let key = Key::from_name("basic.histogram");
let histo1 = recorder.register_histogram(&key);
let histo1 = recorder.register_histogram(&key, &METADATA);
histo1.record(1.0);

let handle = recorder.handle();
Expand All @@ -870,7 +872,7 @@ mod tests {
assert_eq!(rendered, expected);

let key = Key::from_parts("basic.histogram", vec![Label::new("type", "special")]);
let histo2 = recorder.register_histogram(&key);
let histo2 = recorder.register_histogram(&key, &METADATA);
histo2.record(2.0);

let expected_second = concat!(
Expand Down Expand Up @@ -913,11 +915,11 @@ mod tests {
.build_with_clock(clock);

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let key = Key::from_name("basic.gauge");
let gauge1 = recorder.register_gauge(&key);
let gauge1 = recorder.register_gauge(&key, &METADATA);
gauge1.set(-3.44);

let handle = recorder.handle();
Expand Down Expand Up @@ -953,7 +955,7 @@ mod tests {
.build_with_clock(clock);

let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

// First render, which starts tracking the counter in the recency state.
Expand Down Expand Up @@ -992,7 +994,7 @@ mod tests {
.add_global_tag("foo", "bar")
.build_recorder();
let key = Key::from_name("basic.counter");
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(42);

let handle = recorder.handle();
Expand All @@ -1010,7 +1012,7 @@ mod tests {

let key =
Key::from_name("overridden").with_extra_labels(vec![Label::new("foo", "overridden")]);
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(1);

let handle = recorder.handle();
Expand All @@ -1029,7 +1031,7 @@ mod tests {
let key_name = KeyName::from("yee_haw:lets go");
let key =
Key::from_name(key_name).with_extra_labels(vec![Label::new("øhno", "\"yeet\nies\\\"")]);
let counter1 = recorder.register_counter(&key);
let counter1 = recorder.register_counter(&key, &METADATA);
counter1.increment(1);

let handle = recorder.handle();
Expand All @@ -1045,7 +1047,7 @@ mod tests {

let key_name = KeyName::from("distn");
let key = Key::from_name(key_name);
let hist = recorder.register_histogram(&key);
let hist = recorder.register_histogram(&key, &METADATA);

hist.record(12.0);

Expand All @@ -1064,7 +1066,7 @@ mod tests {

let key_name = KeyName::from("data-dist");
let key = Key::from_name(key_name);
let hist = recorder.register_histogram(&key);
let hist = recorder.register_histogram(&key, &METADATA);

hist.record(12.0);

Expand Down
3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use crate::distribution::Distribution;

use crate::formatting::sanitize_metric_name;
use crate::StatsdRecorder;
use indexmap::IndexMap;
use metrics::SetRecorderError;
use thiserror::Error;
Expand Down Expand Up @@ -54,7 +55,7 @@ pub enum BuildError {

/// Installing the recorder did not succeed.
#[error("failed to install exporter as global recorder: {0}")]
FailedToSetGlobalRecorder(#[from] SetRecorderError),
FailedToSetGlobalRecorder(#[from] SetRecorderError<StatsdRecorder>),

/// The given address could not be parsed successfully as an IP address/subnet.
#[error("failed to parse address as a valid IP address/subnet: {0}")]
Expand Down
8 changes: 4 additions & 4 deletions src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::distribution::{Distribution, DistributionBuilder};
use crate::formatting::{key_to_parts, write_metric_line};
use crate::registry::GenerationalAtomicStorage;

use metrics::{Counter, Gauge, Histogram, Key, KeyName, Recorder, SharedString, Unit};
use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit};
use metrics_util::registry::{Recency, Registry};

use indexmap::IndexMap;
Expand Down Expand Up @@ -319,19 +319,19 @@ impl Recorder for StatsdRecorder {
fn describe_gauge(&self, _k: KeyName, _u: Option<Unit>, _d: SharedString) {}
fn describe_histogram(&self, _k: KeyName, _u: Option<Unit>, _d: SharedString) {}

fn register_counter(&self, key: &Key) -> Counter {
fn register_counter(&self, key: &Key, _metadata: &Metadata<'_>) -> Counter {
self.inner
.registry
.get_or_create_counter(key, |c| c.clone().into())
}

fn register_gauge(&self, key: &Key) -> Gauge {
fn register_gauge(&self, key: &Key, _metadata: &Metadata<'_>) -> Gauge {
self.inner
.registry
.get_or_create_gauge(key, |c| c.clone().into())
}

fn register_histogram(&self, key: &Key) -> Histogram {
fn register_histogram(&self, key: &Key, _metadata: &Metadata<'_>) -> Histogram {
self.inner
.registry
.get_or_create_histogram(key, |c| c.clone().into())
Expand Down

0 comments on commit b7a1066

Please sign in to comment.