Skip to content

Commit

Permalink
Merge pull request #24 from nastevens/add-arbitrary-count-method
Browse files Browse the repository at this point in the history
Add arbitrary count method
  • Loading branch information
mcasper authored Feb 24, 2019
2 parents 3f33555 + ee02c88 commit 53e1b19
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
41 changes: 40 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ impl Client {
}
}

/// Make an arbitrary change to a StatsD counter
///
/// # Examples
///
/// ```
/// use dogstatsd::{Client, Options};
///
/// let client = Client::new(Options::default()).unwrap();
/// client.count("counter", 42, &["tag:counter"])
/// .unwrap_or_else(|e| println!("Encountered error: {}", e));
/// ```
pub fn count<'a, I, S, T>(&self, stat: S, count: i64, tags: I) -> DogstatsdResult
where I: IntoIterator<Item=T>,
S: Into<Cow<'a, str>>,
T: AsRef<str>,
{
match stat.into() {
Cow::Borrowed(stat) => self.send(&CountMetric::Arbitrary(stat, count), tags),
Cow::Owned(stat) => self.send(&CountMetric::Arbitrary(&stat, count), tags)
}
}

/// Time how long it takes for a block of code to execute.
///
/// # Examples
Expand Down Expand Up @@ -511,6 +533,18 @@ mod bench {
})
}

#[bench]
fn bench_count(b: &mut Bencher) {
let options = Options::default();
let client = Client::new(options).unwrap();
let tags = &["name1:value1"];
let mut i = 0;
b.iter(|| {
client.count("bench.count", i, tags).unwrap();
i += 1;
})
}

#[bench]
fn bench_timing(b: &mut Bencher) {
let options = Options::default();
Expand Down Expand Up @@ -576,8 +610,13 @@ mod bench {
let options = Options::default();
let client = Client::new(options).unwrap();
let tags = vec!["name1:value1"];
let all_options = ServiceCheckOptions {
hostname: Some("my-host.localhost"),
timestamp: Some(1510326433),
message: Some("Message about the check or service")
};
b.iter(|| {
client.service_check("bench.service_check", ServiceStatus::Critical, &tags).unwrap();
client.service_check("bench.service_check", ServiceStatus::Critical, &tags, Some(all_options)).unwrap();
})
}

Expand Down
34 changes: 31 additions & 3 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub trait Metric {
pub enum CountMetric<'a> {
Incr(&'a str),
Decr(&'a str),
Arbitrary(&'a str, i64),
}

impl<'a> Metric for CountMetric<'a> {
Expand All @@ -65,11 +66,19 @@ impl<'a> Metric for CountMetric<'a> {
buf
},
CountMetric::Decr(stat) => {
let mut buf = String::with_capacity(3 + stat.len() + 4);
let mut buf = String::with_capacity(3 + stat.len() + 5);
buf.push_str(stat);
buf.push_str(":-1|c");
buf
},
CountMetric::Arbitrary(stat, amount) => {
let mut buf = String::with_capacity(3 + stat.len() + 23);
buf.push_str(stat);
buf.push_str(":");
buf.push_str(&amount.to_string());
buf.push_str("|c");
buf
}
}
}
}
Expand Down Expand Up @@ -423,6 +432,16 @@ mod tests {
assert_eq!("decr:-1|c", metric.metric_type_format())
}

#[test]
fn test_count_metric() {
let metric = CountMetric::Arbitrary("arb".into(), 54321);
assert_eq!("arb:54321|c", metric.metric_type_format());
let metric = CountMetric::Arbitrary("arb".into(), -12345);
assert_eq!("arb:-12345|c", metric.metric_type_format());
let metric = CountMetric::Arbitrary("arb".into(), 0);
assert_eq!("arb:0|c", metric.metric_type_format());
}

#[test]
fn test_time_metric() {
let start_time = Utc.ymd(2016, 4, 24).and_hms_milli(0, 0, 0, 0);
Expand Down Expand Up @@ -540,14 +559,23 @@ mod bench {
use self::test::Bencher;
use super::*;

struct NullMetric;

impl Metric for NullMetric {
fn metric_type_format(&self) -> String {
String::new()
}
}

#[bench]
fn bench_format_for_send(b: &mut Bencher) {
let metric = NullMetric;

b.iter(|| {
format_for_send("metric", "foo", &["bar", "baz"]);
format_for_send(&metric, "foo", &["bar", "baz"]);
})
}


#[bench]
fn bench_set_metric(b: &mut Bencher) {
let metric = SetMetric {
Expand Down

0 comments on commit 53e1b19

Please sign in to comment.