From b166d5d925ff93a63aa240a959954a6b220c38f9 Mon Sep 17 00:00:00 2001 From: Dalitso Banda Date: Fri, 28 Apr 2023 13:21:17 -0700 Subject: [PATCH] add unit tests --- .../src/utils/metrics/backends/datadog.rs | 52 +++++++++++++++---- .../src/utils/metrics/backends/testing.rs | 10 +++- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/rust_snuba/src/utils/metrics/backends/datadog.rs b/rust_snuba/src/utils/metrics/backends/datadog.rs index 1b2c364d48..8a9b701321 100644 --- a/rust_snuba/src/utils/metrics/backends/datadog.rs +++ b/rust_snuba/src/utils/metrics/backends/datadog.rs @@ -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, } @@ -37,8 +32,6 @@ impl DatadogMetricsBackend { let client = dogstatsd::Client::new(dogstatsd::Options::default()).unwrap(); DatadogMetricsBackend { client_sd: client, - host, - port, tags } } @@ -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 { @@ -65,7 +58,7 @@ impl MetricsClientTrait for DatadogMetricsBackend { } } None => { - self.client_sd.incr(key, tags_str); + self.client_sd.incr(key, tags_str).unwrap(); } } @@ -80,7 +73,8 @@ impl MetricsClientTrait for DatadogMetricsBackend { sample_rate: Option, ) { let tags_str: Vec = 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( @@ -91,6 +85,42 @@ impl MetricsClientTrait for DatadogMetricsBackend { sample_rate: Option, ) { let tags_str: Vec = 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 = 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); } } diff --git a/rust_snuba/src/utils/metrics/backends/testing.rs b/rust_snuba/src/utils/metrics/backends/testing.rs index 5e6abdea0c..c24ef60d8d 100644 --- a/rust_snuba/src/utils/metrics/backends/testing.rs +++ b/rust_snuba/src/utils/metrics/backends/testing.rs @@ -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] @@ -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")); } }