Skip to content

Commit

Permalink
Merge pull request #2307 from joske/fix/metrics-api-changes
Browse files Browse the repository at this point in the history
Fix/metrics api changes
  • Loading branch information
howardwu authored Jan 20, 2024
2 parents da68deb + e2a2182 commit 9656a7e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
45 changes: 12 additions & 33 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ license = "Apache-2.0"
edition = "2021"

[dependencies.metrics]
version = "0.21"
version = "0.22"

[dependencies.metrics-exporter-prometheus]
version = "0.12"
version = "0.13"
24 changes: 15 additions & 9 deletions metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,70 @@ pub fn register_metrics() {

/// Registers a counter with the given name.
pub fn register_counter(name: &'static str) {
::metrics::register_counter!(name);
let _counter = ::metrics::counter!(name);
}

/// Updates a counter with the given name to the given value.
///
/// Counters represent a single monotonic value, which means the value can only be incremented,
/// not decremented, and always starts out with an initial value of zero.
pub fn counter<V: Into<u64>>(name: &'static str, value: V) {
::metrics::counter!(name, value.into());
let counter = ::metrics::counter!(name);
counter.absolute(value.into());
}

/// Increments a counter with the given name by one.
///
/// Counters represent a single monotonic value, which means the value can only be incremented,
/// not decremented, and always starts out with an initial value of zero.
pub fn increment_counter(name: &'static str) {
::metrics::increment_counter!(name);
let counter = ::metrics::counter!(name);
counter.increment(1);
}

/******** Gauge ********/

/// Registers a gauge with the given name.
pub fn register_gauge(name: &'static str) {
::metrics::register_gauge!(name);
let _gauge = ::metrics::gauge!(name);
}

/// Updates a gauge with the given name to the given value.
///
/// Gauges represent a single value that can go up or down over time,
/// and always starts out with an initial value of zero.
pub fn gauge<V: Into<f64>>(name: &'static str, value: V) {
::metrics::gauge!(name, value.into());
let gauge = ::metrics::gauge!(name);
gauge.set(value.into());
}

/// Increments a gauge with the given name by the given value.
///
/// Gauges represent a single value that can go up or down over time,
/// and always starts out with an initial value of zero.
pub fn increment_gauge<V: Into<f64>>(name: &'static str, value: V) {
::metrics::increment_gauge!(name, value.into());
let gauge = ::metrics::gauge!(name);
gauge.increment(value.into());
}

/// Decrements a gauge with the given name by the given value.
///
/// Gauges represent a single value that can go up or down over time,
/// and always starts out with an initial value of zero.
pub fn decrement_gauge<V: Into<f64>>(name: &'static str, value: V) {
::metrics::decrement_gauge!(name, value.into());
let gauge = ::metrics::gauge!(name);
gauge.decrement(value.into());
}

/******** Histogram ********/

/// Registers a histogram with the given name.
pub fn register_histogram(name: &'static str) {
::metrics::register_histogram!(name);
let _histogram = ::metrics::histogram!(name);
}

/// Updates a histogram with the given name to the given value.
pub fn histogram<V: Into<f64>>(name: &'static str, value: V) {
::metrics::histogram!(name, value.into());
let histogram = ::metrics::histogram!(name);
histogram.record(value.into());
}

0 comments on commit 9656a7e

Please sign in to comment.