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
- metrics-rs/metrics#394 Reworked the macros
  • Loading branch information
ijc committed Jan 8, 2024
1 parent 87b3d93 commit ea4c25a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ categories = ["observability", "operations"]

[dependencies]
cadence = "0.29"
metrics = "0.21"
metrics = "0.22"
thiserror = "1.0"
41 changes: 22 additions & 19 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum StatsdError {
#[error("Could not register the metrics recorder")]
RecorderError {
#[from]
source: SetRecorderError,
source: SetRecorderError<StatsdRecorder>,
},
}

Expand Down Expand Up @@ -136,8 +136,8 @@ impl StatsdBuilder {
/// .build(Some("prefix"))
/// .expect("Could not create StatsdRecorder");
///
/// metrics::set_boxed_recorder(Box::new(recorder));
/// metrics::counter!("counter.name",10);
/// metrics::set_global_recorder(recorder);
/// metrics::counter!("counter.name").increment(10);
/// ```
/// will emit a counter metric name as `prefix.counter.name`
pub fn build(self, prefix: Option<&str>) -> Result<StatsdRecorder, StatsdError> {
Expand Down Expand Up @@ -279,6 +279,9 @@ mod tests {
}
}

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

#[test]
#[should_panic]
fn bad_host_name() {
Expand All @@ -299,7 +302,7 @@ mod tests {
fn counter() {
let env = Environ::new(None);
let key = Key::from_name("counter.name");
let counter = env.recorder.register_counter(&key);
let counter = env.recorder.register_counter(&key, &METADATA);
counter.increment(1);
assert_eq!("counter.name:1|c", env.receive_on_server());
}
Expand All @@ -310,7 +313,7 @@ mod tests {
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("counter.name", tags));

let coutner = env.recorder.register_counter(&key);
let coutner = env.recorder.register_counter(&key, &METADATA);
coutner.increment(10);
assert_eq!("counter.name:10|c|#t1:v1,t2:v2", env.receive_on_server());
}
Expand All @@ -319,7 +322,7 @@ mod tests {
fn gauge() {
let env = Environ::new(None);
let key = Key::from_name("gauge.name");
let gauge = env.recorder.register_gauge(&key);
let gauge = env.recorder.register_gauge(&key, &METADATA);
gauge.set(50.25);
assert_eq!("gauge.name:50.25|g", env.receive_on_server());
}
Expand All @@ -329,7 +332,7 @@ mod tests {
let env = Environ::new(None);
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("gauge.name", tags));
let gauge = env.recorder.register_gauge(&key);
let gauge = env.recorder.register_gauge(&key, &METADATA);
gauge.set(50.25);
assert_eq!("gauge.name:50.25|g|#t1:v1,t2:v2", env.receive_on_server());
}
Expand All @@ -338,7 +341,7 @@ mod tests {
fn histogram() {
let env = Environ::new(None);
let key = Key::from_name("histogram.name");
let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!("histogram.name:100|h", env.receive_on_server());
}
Expand All @@ -347,7 +350,7 @@ mod tests {
fn histogram_with_decimals() {
let env = Environ::new(None);
let key = Key::from_name("histogram.name");
let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.52);
assert_eq!("histogram.name:100.52|h", env.receive_on_server());
}
Expand All @@ -357,7 +360,7 @@ mod tests {
let env = Environ::new_histogram_is_distribution();
let key = Key::from_name("distribution.name");

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.52);
assert_eq!("distribution.name:100.52|d", env.receive_on_server());
}
Expand All @@ -368,7 +371,7 @@ mod tests {
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("histogram.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!("histogram.name:100|h|#t1:v1,t2:v2", env.receive_on_server());
}
Expand All @@ -383,7 +386,7 @@ mod tests {
];
let key = Key::from(("distribution.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!(
"distribution.name:100|d|#t1:v1,t2:v2",
Expand All @@ -401,7 +404,7 @@ mod tests {
];
let key = Key::from(("distribution.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!(
"blackbird.distribution.name:100|d|#t1:v1,t2:v2",
Expand All @@ -415,7 +418,7 @@ mod tests {
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("histogram.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!(
"blackbird.histogram.name:100|h|#t1:v1,t2:v2",
Expand All @@ -433,7 +436,7 @@ mod tests {
];
let key = Key::from(("histogram.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
// metrics-rs reports the unit as seconds and we convert it to ms
assert_eq!(
Expand All @@ -448,7 +451,7 @@ mod tests {
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("histogram.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
assert_eq!("histogram.name:100|d|#t1:v1,t2:v2", env.receive_on_server());
}
Expand All @@ -459,7 +462,7 @@ mod tests {
let tags = vec![Label::new("t1", "v1"), Label::new("t2", "v2")];
let key = Key::from(("histogram.name", tags));

let histogram = env.recorder.register_histogram(&key);
let histogram = env.recorder.register_histogram(&key, &METADATA);
histogram.record(100.00);
// metrics-rs reports the unit as seconds and we convert it to ms
assert_eq!(
Expand All @@ -472,7 +475,7 @@ mod tests {
fn prefix() {
let env = Environ::new(Some("koelbird"));
let key = Key::from_name("counter.name");
let counter = env.recorder.register_counter(&key);
let counter = env.recorder.register_counter(&key, &METADATA);
counter.increment(1);
assert_eq!("koelbird.counter.name:1|c", env.receive_on_server());
}
Expand All @@ -491,7 +494,7 @@ mod tests {
};

let key = Key::from_name("counter.name");
let counter = env.recorder.register_counter(&key);
let counter = env.recorder.register_counter(&key, &METADATA);

counter.increment(1);
assert_eq!(
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
//! .build(Some("prefix"))
//! .expect("Could not create StatsdRecorder");
//!
//! metrics::set_boxed_recorder(Box::new(recorder));
//! metrics::set_global_recorder(recorder);
//! ```
//!
//! You can then continue to use [`metrics`] as usual:
//!
//! ```
//! metrics::increment_counter!("counter.name");
//! metrics::counter!("counter.name").increment(1);
//! ```
//!
//! Labels are translated to datadog style tags:
//!
//!```
//! metrics::gauge!("gauge.name", 100.0 , "tag" => "value");
//! metrics::gauge!("gauge.name", "tag" => "value").set(100.0);
//!```
//! will translate to `gauge.name:50.25|g|#tag:value` and should render appropriately in systems
//! like Datadog.
Expand Down Expand Up @@ -56,7 +56,7 @@
//! like to override it for a given metric you can still do so:
//!
//! ```
//! metrics::histogram!("metric.name", 100.0, "histogram"=> "histogram","tag"=>"value")
//! metrics::histogram!("metric.name", "histogram"=> "histogram","tag"=>"value").record(100.0)
//! ```
//! This will emit the usual histogram metric this `metric.name:100|h|#tag:value`.
//!
Expand All @@ -73,7 +73,7 @@
//!
//! **Reporting distributions:**
//! ```
//! metrics::histogram!("metric.name", 100.0, "histogram"=>"distribution", "tag"=>"value")
//! metrics::histogram!("metric.name", "histogram"=>"distribution", "tag"=>"value").record(100.0)
//! ```
//! This will emit a metric like this: `metric.name:100|d|#tag:value`, note the metric type has
//! changed from `h` to `d`.
Expand All @@ -84,7 +84,7 @@
//!
//! **Reporting timers:**
//! ```
//! metrics::histogram!("metric.name", 100.0, "histogram"=>"timer", "tag"=>"value")
//! metrics::histogram!("metric.name", "histogram"=>"timer", "tag"=>"value").record(100.0)
//! ```
//! This will emit a metric like this: `metric.name:100|ms|#tag:value`, note the metric type has
//! changed from `h` to `ms`.
Expand All @@ -104,14 +104,14 @@
//! .build(Some("prefix"))
//! .expect("Could not create StatsdRecorder");
//!
//! metrics::set_boxed_recorder(Box::new(recorder));
//! metrics::set_global_recorder(recorder);
//!```
//!
//! Once the exporter is marked this way then all the histograms will be reported as distributions
//! by default unless labeled differently. For example following statement:
//!
//! ```
//! metrics::histogram!("metric.name", 100.0, "tag"=>"value")
//! metrics::histogram!("metric.name", "tag"=>"value").record(100.0)
//! ```
//! This will emit a metric like this: `metric.name:100|d|#tag:value`, note the metric type has
//! emitted here is `d` and not `h`.
Expand Down
8 changes: 4 additions & 4 deletions src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cadence::{Counted, Distributed, Gauged, Histogrammed, MetricBuilder, StatsdC
use metrics::{Counter, CounterFn, SharedString};
use metrics::{Gauge, GaugeFn};
use metrics::{Histogram, HistogramFn};
use metrics::{Key, KeyName, Label, Recorder, Unit};
use metrics::{Key, KeyName, Label, Metadata, Recorder, Unit};

use crate::types::HistogramType;

Expand Down Expand Up @@ -42,23 +42,23 @@ impl Recorder for StatsdRecorder {
unimplemented!("statsd recording does not support descriptions.")
}

fn register_counter(&self, key: &Key) -> Counter {
fn register_counter(&self, key: &Key, _metadata: &Metadata<'_>) -> Counter {
Counter::from_arc(Arc::new(Handle::new(
key.clone(),
self.statsd.clone(),
self.default_histogram,
)))
}

fn register_gauge(&self, key: &Key) -> Gauge {
fn register_gauge(&self, key: &Key, _metadata: &Metadata<'_>) -> Gauge {
Gauge::from_arc(Arc::new(Handle::new(
key.clone(),
self.statsd.clone(),
self.default_histogram,
)))
}

fn register_histogram(&self, key: &Key) -> Histogram {
fn register_histogram(&self, key: &Key, _metadata: &Metadata<'_>) -> Histogram {
Histogram::from_arc(Arc::new(Handle::new(
key.clone(),
self.statsd.clone(),
Expand Down

0 comments on commit ea4c25a

Please sign in to comment.