Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanda committed May 23, 2023
1 parent 74b6ca8 commit b166d5d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
52 changes: 41 additions & 11 deletions rust_snuba/src/utils/metrics/backends/datadog.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// use crate::utils::metrics::backends::MetricsBackend;

use rust_arroyo::utils::metrics::{gauge, increment, init, time, MetricsClientTrait};
use dogstatsd;
use cadence::StatsdClient;

use super::abstract_backend::MetricsBackend;
pub struct DatadogMetricsBackend {
client_sd: dogstatsd::Client,
host: String,
port: u16,
tags: Vec<String>,
}

Expand Down Expand Up @@ -37,8 +32,6 @@ impl DatadogMetricsBackend {
let client = dogstatsd::Client::new(dogstatsd::Options::default()).unwrap();
DatadogMetricsBackend {
client_sd: client,
host,
port,
tags
}
}
Expand All @@ -56,7 +49,7 @@ impl MetricsClientTrait for DatadogMetricsBackend {

match value {
Some(v) => {
for i in 0..v.abs() {
for _ in 0..v.abs() {
if v < 0 {
self.client_sd.decr(key, &tags_str).unwrap();
} else {
Expand All @@ -65,7 +58,7 @@ impl MetricsClientTrait for DatadogMetricsBackend {
}
}
None => {
self.client_sd.incr(key, tags_str);
self.client_sd.incr(key, tags_str).unwrap();
}
}

Expand All @@ -80,7 +73,8 @@ impl MetricsClientTrait for DatadogMetricsBackend {
sample_rate: Option<f64>,
) {
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{}:{}", k, v)).collect();
self.client_sd.gauge(key, value.to_string(), tags_str).unwrap();
let res = self.client_sd.gauge(key, value.to_string(), tags_str).unwrap();
println!("ok = {:?}", res)
}

fn time(
Expand All @@ -91,6 +85,42 @@ impl MetricsClientTrait for DatadogMetricsBackend {
sample_rate: Option<f64>,
) {
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{}:{}", k, v)).collect();
self.client_sd.timing(key, value.try_into().unwrap(), tags_str);
self.client_sd.timing(key, value.try_into().unwrap(), tags_str).unwrap();
}
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use dogstatsd::Options;
use rust_arroyo::utils::metrics::{configure_metrics, MetricsClientTrait, self};

use crate::utils::metrics::backends::{datadog::DatadogMetricsBackend};


#[test]
fn test_testing_backend() {

let custom_options = Options::new("0.0.0.0:9000", "0.0.0.0:8125", "analytics", vec!(String::new()));
// let client = dogstatsd::Client::new(dogstatsd::Options::default()).unwrap();
let client = dogstatsd::Client::new(custom_options).unwrap();
let client_tags: Vec<String> = Vec::new();
let testing_backend = DatadogMetricsBackend {
client_sd: client,
tags: client_tag
};

let mut tags: HashMap<&str, &str> = HashMap::new();
tags.insert("tag1", "value1");
tags.insert("tag2", "value2");

testing_backend.counter("test_counter", Some(1), Some(tags.clone()), None);
testing_backend.gauge("test_gauge", 1, Some(tags.clone()), None);
testing_backend.time("test_time", 1, Some(tags.clone()), None);

// check configure_metrics writes to METRICS
configure_metrics(testing_backend);
metrics::time("c", 30, Some(HashMap::from([("tag3", "value3")])), None);
}
}
10 changes: 9 additions & 1 deletion rust_snuba/src/utils/metrics/backends/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl MetricsClientTrait for TestingMetricsBackend{
mod tests {
use std::collections::HashMap;

use rust_arroyo::utils::metrics::{configure_metrics, MetricsClientTrait};
use rust_arroyo::utils::metrics::{configure_metrics, MetricsClientTrait, self};

use crate::utils::metrics::backends::testing::METRICS;


#[test]
Expand All @@ -103,7 +105,13 @@ mod tests {
testing_backend.counter("test_counter", Some(1), Some(tags.clone()), None);
testing_backend.gauge("test_gauge", 1, Some(tags.clone()), None);
testing_backend.time("test_time", 1, Some(tags.clone()), None);
assert!(METRICS.lock().unwrap().contains_key("test_counter"));
assert!(METRICS.lock().unwrap().contains_key("test_gauge"));
assert!(METRICS.lock().unwrap().contains_key("test_time"));

// check configure_metrics writes to METRICS
configure_metrics(testing_backend);
metrics::time("c", 30, Some(HashMap::from([("tag3", "value3")])), None);
assert!(METRICS.lock().unwrap().contains_key("c"));
}
}

0 comments on commit b166d5d

Please sign in to comment.