Skip to content

Commit

Permalink
Renamed metrics::Recorder & metrics-runtime::Sink methods (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-airoldie authored and tobz committed Aug 14, 2019
1 parent c30f256 commit 2f3e344
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions metrics-runtime/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ impl Generator {
0
};

let _ = self.stats.record_counter("ok", 1);
let _ = self.stats.increment_counter("ok", 1);
let _ = self.stats.record_timing("ok", t0, t1);
let _ = self.stats.record_gauge("total", self.gauge);
let _ = self.stats.update_gauge("total", self.gauge);

if start != 0 {
let delta = self.stats.now() - start;
Expand Down
20 changes: 10 additions & 10 deletions metrics-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
//!
//! // We can update a counter. Counters are monotonic, unsigned integers that start at 0 and
//! // increase over time.
//! sink.record_counter("widgets", 5);
//! sink.increment_counter("widgets", 5);
//!
//! // We can update a gauge. Gauges are signed, and hold on to the last value they were updated
//! // to, so you need to track the overall value on your own.
//! sink.record_gauge("red_balloons", 99);
//! sink.update_gauge("red_balloons", 99);
//!
//! // We can update a timing histogram. For timing, we're using the built-in `Sink::now` method
//! // which utilizes a high-speed internal clock. This method returns the time in nanoseconds, so
Expand Down Expand Up @@ -90,29 +90,29 @@
//! # let receiver = Receiver::builder().build().expect("failed to create receiver");
//! // This sink has no scope aka the root scope. The metric will just end up as "widgets".
//! let mut root_sink = receiver.get_sink();
//! root_sink.record_counter("widgets", 42);
//! root_sink.increment_counter("widgets", 42);
//!
//! // This sink is under the "secret" scope. Since we derived ourselves from the root scope,
//! // we're not nested under anything, but our metric name will end up being "secret.widgets".
//! let mut scoped_sink = root_sink.scoped("secret");
//! scoped_sink.record_counter("widgets", 42);
//! scoped_sink.increment_counter("widgets", 42);
//!
//! // This sink is under the "supersecret" scope, but we're also nested! The metric name for this
//! // sample will end up being "secret.supersecret.widget".
//! let mut scoped_sink_two = scoped_sink.scoped("supersecret");
//! scoped_sink_two.record_counter("widgets", 42);
//! scoped_sink_two.increment_counter("widgets", 42);
//!
//! // Sinks retain their scope even when cloned, so the metric name will be the same as above.
//! let mut cloned_sink = scoped_sink_two.clone();
//! cloned_sink.record_counter("widgets", 42);
//! cloned_sink.increment_counter("widgets", 42);
//!
//! // This sink will be nested two levels deeper than its parent by using a slightly different
//! // input scope: scope can be a single string, or multiple strings, which is interpreted as
//! // nesting N levels deep.
//! //
//! // This metric name will end up being "super.secret.ultra.special.widgets".
//! let mut scoped_sink_three = scoped_sink.scoped(&["super", "secret", "ultra", "special"]);
//! scoped_sink_two.record_counter("widgets", 42);
//! scoped_sink_two.increment_counter("widgets", 42);
//! ```
//!
//! # Labels
Expand Down Expand Up @@ -181,7 +181,7 @@
//! egg_count.record(12);
//!
//! // This updates the same metric as above! We have so many eggs now!
//! sink.record_counter("eggs", 12);
//! sink.increment_counter("eggs", 12);
//!
//! // Gauges and histograms don't have any extra helper methods, just `record`:
//! let gauge = sink.gauge("population");
Expand Down Expand Up @@ -264,8 +264,8 @@
//! // We can update a counter. Counters are monotonic, unsigned integers that start at 0 and
//! // increase over time.
//! // Take some measurements, similar to what we had in other examples:
//! sink.record_counter("widgets", 5);
//! sink.record_gauge("red_balloons", 99);
//! sink.increment_counter("widgets", 5);
//! sink.update_gauge("red_balloons", 99);
//!
//! let start = sink.now();
//! thread::sleep(Duration::from_millis(10));
Expand Down
8 changes: 4 additions & 4 deletions metrics-runtime/src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ impl Receiver {
}

impl Recorder for Receiver {
fn record_counter(&self, key: Key, value: u64) {
fn increment_counter(&self, key: Key, value: u64) {
SINK.with(move |sink| {
let mut sink = sink.borrow_mut();
if sink.is_none() {
let new_sink = self.get_sink();
*sink = Some(new_sink);
}

sink.as_mut().unwrap().record_counter(key, value);
sink.as_mut().unwrap().increment_counter(key, value);
});
}

fn record_gauge(&self, key: Key, value: i64) {
fn update_gauge(&self, key: Key, value: i64) {
SINK.with(move |sink| {
let mut sink = sink.borrow_mut();
if sink.is_none() {
let new_sink = self.get_sink();
*sink = Some(new_sink);
}

sink.as_mut().unwrap().record_gauge(key, value);
sink.as_mut().unwrap().update_gauge(key, value);
});
}

Expand Down
24 changes: 12 additions & 12 deletions metrics-runtime/src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Sink {
self.clock.now()
}

/// Records a value for a counter identified by the given name.
/// Increment a value for a counter identified by the given name.
///
/// # Examples
///
Expand All @@ -132,10 +132,10 @@ impl Sink {
/// # fn main() {
/// let receiver = Receiver::builder().build().expect("failed to create receiver");
/// let mut sink = receiver.get_sink();
/// sink.record_counter("messages_processed", 1);
/// sink.increment_counter("messages_processed", 1);
/// # }
/// ```
pub fn record_counter<N>(&mut self, name: N, value: u64)
pub fn increment_counter<N>(&mut self, name: N, value: u64)
where
N: Into<Key>,
{
Expand All @@ -145,7 +145,7 @@ impl Sink {
value_handle.update_counter(value);
}

/// Records a value for a counter identified by the given name and labels.
/// Increment a value for a counter identified by the given name and labels.
///
/// # Examples
///
Expand All @@ -155,10 +155,10 @@ impl Sink {
/// # fn main() {
/// let receiver = Receiver::builder().build().expect("failed to create receiver");
/// let mut sink = receiver.get_sink();
/// sink.record_counter_with_labels("messages_processed", 1, &[("message_type", "mgmt")]);
/// sink.increment_counter_with_labels("messages_processed", 1, &[("message_type", "mgmt")]);
/// # }
/// ```
pub fn record_counter_with_labels<N, L>(&mut self, name: N, value: u64, labels: L)
pub fn increment_counter_with_labels<N, L>(&mut self, name: N, value: u64, labels: L)
where
N: Into<ScopedString>,
L: IntoLabels,
Expand All @@ -169,7 +169,7 @@ impl Sink {
value_handle.update_counter(value);
}

/// Records a value for a gauge identified by the given name.
/// Update a value for a gauge identified by the given name.
///
/// # Examples
///
Expand All @@ -179,10 +179,10 @@ impl Sink {
/// # fn main() {
/// let receiver = Receiver::builder().build().expect("failed to create receiver");
/// let mut sink = receiver.get_sink();
/// sink.record_gauge("current_offset", -131);
/// sink.update_gauge("current_offset", -131);
/// # }
/// ```
pub fn record_gauge<N>(&mut self, name: N, value: i64)
pub fn update_gauge<N>(&mut self, name: N, value: i64)
where
N: Into<Key>,
{
Expand All @@ -192,7 +192,7 @@ impl Sink {
value_handle.update_gauge(value);
}

/// Records a value for a gauge identified by the given name and labels.
/// Update a value for a gauge identified by the given name and labels.
///
/// # Examples
///
Expand All @@ -202,10 +202,10 @@ impl Sink {
/// # fn main() {
/// let receiver = Receiver::builder().build().expect("failed to create receiver");
/// let mut sink = receiver.get_sink();
/// sink.record_gauge_with_labels("current_offset", -131, &[("source", "stratum-1")]);
/// sink.update_gauge_with_labels("current_offset", -131, &[("source", "stratum-1")]);
/// # }
/// ```
pub fn record_gauge_with_labels<N, L>(&mut self, name: N, value: i64, labels: L)
pub fn update_gauge_with_labels<N, L>(&mut self, name: N, value: i64, labels: L)
where
N: Into<ScopedString>,
L: IntoLabels,
Expand Down
4 changes: 2 additions & 2 deletions metrics/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ static RECORDER: PrintRecorder = PrintRecorder;
struct PrintRecorder;

impl Recorder for PrintRecorder {
fn record_counter(&self, key: Key, value: u64) {
fn increment_counter(&self, key: Key, value: u64) {
println!("metrics -> counter(name={}, value={})", key, value);
}

fn record_gauge(&self, key: Key, value: i64) {
fn update_gauge(&self, key: Key, value: i64) {
println!("metrics -> gauge(name={}, value={})", key, value);
}

Expand Down
28 changes: 14 additions & 14 deletions metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@
//! struct LogRecorder;
//!
//! impl Recorder for LogRecorder {
//! fn record_counter(&self, key: Key, value: u64) {
//! fn increment_counter(&self, key: Key, value: u64) {
//! info!("counter '{}' -> {}", key, value);
//! }
//!
//! fn record_gauge(&self, key: Key, value: i64) {
//! fn update_gauge(&self, key: Key, value: i64) {
//! info!("gauge '{}' -> {}", key, value);
//! }
//!
Expand All @@ -96,8 +96,8 @@
//! # use metrics_core::Key;
//! # struct LogRecorder;
//! # impl Recorder for LogRecorder {
//! # fn record_counter(&self, _key: Key, _value: u64) {}
//! # fn record_gauge(&self, _key: Key, _value: i64) {}
//! # fn increment_counter(&self, _key: Key, _value: u64) {}
//! # fn update_gauge(&self, _key: Key, _value: i64) {}
//! # fn record_histogram(&self, _key: Key, _value: u64) {}
//! # }
//! use metrics::SetRecorderError;
Expand All @@ -122,8 +122,8 @@
//! # use metrics_core::Key;
//! # struct LogRecorder;
//! # impl Recorder for LogRecorder {
//! # fn record_counter(&self, _key: Key, _value: u64) {}
//! # fn record_gauge(&self, _key: Key, _value: i64) {}
//! # fn increment_counter(&self, _key: Key, _value: u64) {}
//! # fn update_gauge(&self, _key: Key, _value: i64) {}
//! # fn record_histogram(&self, _key: Key, _value: u64) {}
//! # }
//! use metrics::SetRecorderError;
Expand Down Expand Up @@ -168,7 +168,7 @@ pub trait Recorder {
/// counters and gauges usually have slightly different modes of operation.
///
/// For the sake of flexibility on the exporter side, both are provided.
fn record_counter(&self, key: Key, value: u64);
fn increment_counter(&self, key: Key, value: u64);

/// Records a gauge.
///
Expand All @@ -177,7 +177,7 @@ pub trait Recorder {
/// counters and gauges usually have slightly different modes of operation.
///
/// For the sake of flexibility on the exporter side, both are provided.
fn record_gauge(&self, key: Key, value: i64);
fn update_gauge(&self, key: Key, value: i64);

/// Records a histogram.
///
Expand All @@ -191,8 +191,8 @@ pub trait Recorder {
struct NoopRecorder;

impl Recorder for NoopRecorder {
fn record_counter(&self, _key: Key, _value: u64) {}
fn record_gauge(&self, _key: Key, _value: i64) {}
fn increment_counter(&self, _key: Key, _value: u64) {}
fn update_gauge(&self, _key: Key, _value: i64) {}
fn record_histogram(&self, _key: Key, _value: u64) {}
}

Expand Down Expand Up @@ -314,13 +314,13 @@ pub fn recorder() -> &'static dyn Recorder {
}

#[doc(hidden)]
pub fn __private_api_record_count(key: Key, value: u64) {
recorder().record_counter(key, value);
pub fn __private_api_increment_counter(key: Key, value: u64) {
recorder().increment_counter(key, value);
}

#[doc(hidden)]
pub fn __private_api_record_gauge<K: Into<Key>>(key: K, value: i64) {
recorder().record_gauge(key.into(), value);
pub fn __private_api_update_gauge<K: Into<Key>>(key: K, value: i64) {
recorder().update_gauge(key.into(), value);
}

#[doc(hidden)]
Expand Down
12 changes: 6 additions & 6 deletions metrics/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// exist, then increment it by the given value. Optionally, a set of labels,
/// of the form `key => value`, can be passed to further describe the counter.
///
/// Functionally equivalent to calling [`Recorder::record_counter`].
/// Functionally equivalent to calling [`Recorder::increment_counter`].
///
/// ### Examples
///
Expand Down Expand Up @@ -33,13 +33,13 @@
#[macro_export]
macro_rules! counter {
($name:expr, $value:expr) => {
$crate::__private_api_record_count($crate::Key::from_name($name), $value);
$crate::__private_api_increment_counter($crate::Key::from_name($name), $value);
};

($name:expr, $value:expr, $($labels:tt)*) => {
let labels = $crate::labels!( $($labels)* );
let key = $crate::Key::from_name_and_labels($name, labels);
$crate::__private_api_record_count(key, $value);
$crate::__private_api_increment_counter(key, $value);
};
}

Expand All @@ -50,7 +50,7 @@ macro_rules! counter {
/// a set of labels, of the form `key => value`, can be passed to further
/// describe the gauge.
///
/// Functionally equivalent to calling [`Recorder::record_gauge`].
/// Functionally equivalent to calling [`Recorder::update_gauge`].
///
/// ### Examples
///
Expand Down Expand Up @@ -78,13 +78,13 @@ macro_rules! counter {
#[macro_export]
macro_rules! gauge {
($name:expr, $value:expr) => {
$crate::__private_api_record_gauge($crate::Key::from_name($name), $value);
$crate::__private_api_update_gauge($crate::Key::from_name($name), $value);
};

($name:expr, $value:expr, $($labels:tt)*) => {
let labels = $crate::labels!( $($labels)* );
let key = $crate::Key::from_name_and_labels($name, labels);
$crate::__private_api_record_gauge(key, $value);
$crate::__private_api_update_gauge(key, $value);
};
}

Expand Down

0 comments on commit 2f3e344

Please sign in to comment.