Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ToCounterValue impls #201

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions cadence/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::u64;
/// Conversion trait for valid values for counters
///
/// This trait must be implemented for any types that are used as counter
/// values (currently only `i64`). This trait is internal to how values are
/// values (currently `i64`, `i32`, `u64`, and `u32`). This trait is internal to how values are
/// formatted as part of metrics but is exposed publicly for documentation
/// purposes.
///
Expand All @@ -36,6 +36,23 @@ impl ToCounterValue for i64 {
Ok(MetricValue::Signed(self))
}
}
impl ToCounterValue for i32 {
fn try_to_value(self) -> MetricResult<MetricValue> {
Ok(MetricValue::Signed(self.into()))
}
}

impl ToCounterValue for u64 {
fn try_to_value(self) -> MetricResult<MetricValue> {
Ok(MetricValue::Unsigned(self))
}
}

impl ToCounterValue for u32 {
fn try_to_value(self) -> MetricResult<MetricValue> {
Ok(MetricValue::Unsigned(self.into()))
}
}

/// Conversion trait for valid values for timers
///
Expand Down Expand Up @@ -487,6 +504,9 @@ where
/// "prefix", NopMetricSink));
///
/// client.count("some.counter", 1).unwrap();
/// client.count("some.counter", 2i32).unwrap();
/// client.count("some.counter", 4u64).unwrap();
/// client.count("some.counter", 8u32).unwrap();
/// client.time("some.timer", 42).unwrap();
/// client.time("some.timer", Duration::from_millis(42)).unwrap();
/// client.time("some.timer", vec![42]).unwrap();
Expand All @@ -503,6 +523,9 @@ where
/// ```
pub trait MetricClient:
Counted<i64>
+ Counted<i32>
+ Counted<u64>
+ Counted<u32>
+ CountedExt
+ Timed<u64>
+ Timed<Duration>
Expand Down Expand Up @@ -1523,12 +1546,33 @@ mod tests {
// we hadn't, this wouldn't compile.

#[test]
fn test_statsd_client_as_counted() {
fn test_statsd_client_as_counted_i64() {
let client: Box<dyn Counted<i64>> = Box::new(StatsdClient::from_sink("prefix", NopMetricSink));

client.count("some.counter", 5).unwrap();
}

#[test]
fn test_statsd_client_as_counted_i32() {
let client: Box<dyn Counted<i32>> = Box::new(StatsdClient::from_sink("prefix", NopMetricSink));

client.count("some.counter", 10i32).unwrap();
}

#[test]
fn test_statsd_client_as_counted_u64() {
let client: Box<dyn Counted<u64>> = Box::new(StatsdClient::from_sink("prefix", NopMetricSink));

client.count("some.counter", 20u64).unwrap();
}

#[test]
fn test_statsd_client_as_counted_u32() {
let client: Box<dyn Counted<u32>> = Box::new(StatsdClient::from_sink("prefix", NopMetricSink));

client.count("some.counter", 40u32).unwrap();
}

#[test]
fn test_statsd_client_as_countedext() {
let client: Box<dyn CountedExt> = Box::new(StatsdClient::from_sink("prefix", NopMetricSink));
Expand Down Expand Up @@ -1664,7 +1708,10 @@ mod tests {
QueuingMetricSink::from(NopMetricSink),
));

client.count("some.counter", 3).unwrap();
client.count("some.counter", 3).unwrap(); // this defaults to i64
client.count("some.counter", 6i32).unwrap();
client.count("some.counter", 12u64).unwrap();
client.count("some.counter", 24u32).unwrap();
client.time("some.timer", 198).unwrap();
client.time("some.timer", Duration::from_millis(198)).unwrap();
client.time("some.timer", vec![198]).unwrap();
Expand Down