Skip to content

Commit

Permalink
add sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanda committed May 23, 2023
1 parent 506ca85 commit 2ff48c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
19 changes: 13 additions & 6 deletions rust_snuba/rust_arroyo/src/utils/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ use std::net::{ToSocketAddrs, UdpSocket};
use std::sync::Arc;

pub trait MetricsClientTrait: Send + Sync {

fn should_sample(&self, sample_rate: Option<f64>) -> bool {
rand::thread_rng().gen_range(0.0..=1.0) < sample_rate.unwrap_or(1.0)
}

fn counter(
&self,
key: &str,
Expand All @@ -34,7 +39,6 @@ pub trait MetricsClientTrait: Send + Sync {
);
}

// #[derive( Clone)]
pub struct MetricsClient {
statsd_client: StatsdClient,
prefix: String,
Expand Down Expand Up @@ -120,9 +124,6 @@ impl MetricsClientTrait for MetricsClient {
}

impl MetricsClient {
fn should_sample(&self, sample_rate: Option<f64>) -> bool {
rand::thread_rng().gen_range(0.0..=1.0) < sample_rate.unwrap_or(1.0)
}

fn send_with_tags<'t, T: cadence::Metric + From<String>>(
&self,
Expand Down Expand Up @@ -210,11 +211,17 @@ mod tests {
fn test_metrics() {
init("my_host", "0.0.0.0:8125");

assert!(!METRICS_CLIENT
.read()
.clone()
.unwrap()
.should_sample(Some(0.0)),);

assert!(METRICS_CLIENT
.read()
.clone()
.is_some()
);
.unwrap()
.should_sample(Some(1.0)),);

increment(
"a",
Expand Down
16 changes: 13 additions & 3 deletions rust_snuba/src/utils/metrics/backends/datadog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ impl MetricsClientTrait for DatadogMetricsBackend {
key: &str,
value: Option<i64>,
tags: Option<std::collections::HashMap<&str, &str>>,
_sample_rate: Option<f64>,
sample_rate: Option<f64>,
) {
if !self.should_sample(sample_rate) {
return;
}

let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{k}:{v}")).collect();

match value {
Expand All @@ -70,8 +74,11 @@ impl MetricsClientTrait for DatadogMetricsBackend {
key: &str,
value: u64,
tags: Option<std::collections::HashMap<&str, &str>>,
_sample_rate: Option<f64>,
sample_rate: Option<f64>,
) {
if !self.should_sample(sample_rate) {
return;
}
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();
}
Expand All @@ -81,8 +88,11 @@ impl MetricsClientTrait for DatadogMetricsBackend {
key: &str,
value: u64,
tags: Option<std::collections::HashMap<&str, &str>>,
_sample_rate: Option<f64>,
sample_rate: Option<f64>,
) {
if !self.should_sample(sample_rate) {
return;
}
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).unwrap();
}
Expand Down
17 changes: 14 additions & 3 deletions rust_snuba/src/utils/metrics/backends/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ impl MetricsBackend for TestingMetricsBackend {
}

impl MetricsClientTrait for TestingMetricsBackend{
fn counter(&self, name: &str, value: Option<i64>, tags: Option<HashMap<&str, &str>>, _sample_rate: Option<f64>) {
fn counter(&self, name: &str, value: Option<i64>, tags: Option<HashMap<&str, &str>>, sample_rate: Option<f64>) {
if !self.should_sample(sample_rate) {
return;
}

let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
Expand All @@ -50,7 +54,10 @@ impl MetricsClientTrait for TestingMetricsBackend{
});
}

fn gauge(&self, name: &str, value: u64, tags: Option<HashMap<&str, &str>>, _sample_rate: Option<f64>) {
fn gauge(&self, name: &str, value: u64, tags: Option<HashMap<&str, &str>>, sample_rate: Option<f64>) {
if !self.should_sample(sample_rate) {
return;
}
let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
Expand All @@ -66,7 +73,11 @@ impl MetricsClientTrait for TestingMetricsBackend{
});
}

fn time(&self, name: &str, value: u64, tags: Option<HashMap<&str, &str>>, _sample_rate: Option<f64>) {
fn time(&self, name: &str, value: u64, tags: Option<HashMap<&str, &str>>, sample_rate: Option<f64>) {
if !self.should_sample(sample_rate) {
return;
}

let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
Expand Down

0 comments on commit 2ff48c4

Please sign in to comment.