Skip to content

Commit

Permalink
linting, remove some comments and old libs
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanda committed Jun 27, 2023
1 parent 4c95e5c commit 7b73d0d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 63 deletions.
11 changes: 11 additions & 0 deletions rust_snuba/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions rust_snuba/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pyo3 = { version = "0.18.1", features = ["chrono", "extension-module"] }
ctrlc = "3.2.5"
sentry = "0.31.0"

# these are unofficial libs. haven't validated them yet
# unofficial lib. haven't validated it yet
dogstatsd = "0.8.0"
cadence = "0.29.0"

lazy_static = "1.4.0"
9 changes: 2 additions & 7 deletions rust_snuba/rust_arroyo/src/utils/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,11 @@ 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()
.unwrap()
.should_sample(Some(1.0)),);
.is_some()
);

increment(
"a",
Expand Down
2 changes: 1 addition & 1 deletion rust_snuba/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod config;
mod consumer;
mod strategies;
mod types;
mod utils;
pub mod utils;

use pyo3::prelude::*;

Expand Down
5 changes: 0 additions & 5 deletions rust_snuba/src/utils/metrics/backends/abstract_backend.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use rust_arroyo::utils::metrics::MetricsClientTrait;

pub trait MetricsBackend: MetricsClientTrait {
// fn increment(&self, name: &str, value: i64, tags: &[&str]);
// fn gauge(&self, name: &str, value: f64, tags: &[&str]);
// fn timing(&self, name: &str, value: i64, tags: &[&str]);
fn events(
&self,
title: &str,
Expand All @@ -12,6 +9,4 @@ pub trait MetricsBackend: MetricsClientTrait {
priority: &str,
tags: &[&str],
);
// fn counter(&self, name: &str, value: f64, tags: &[&str]) -> Result<(), Error>;
// fn histogram(&self, name: &str, value: f64, tags: &[&str]) -> Result<(), Error>;
}
46 changes: 23 additions & 23 deletions rust_snuba/src/utils/metrics/backends/datadog.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
use rust_arroyo::utils::metrics::{gauge, increment, init, time, MetricsClientTrait};
use rust_arroyo::utils::metrics::{MetricsClientTrait};
use dogstatsd;

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

impl MetricsBackend for DatadogMetricsBackend {
fn events(
&self,
title: &str,
text: &str,
alert_type: &str,
priority: &str,
_alert_type: &str,
_priority: &str,
tags: &[&str],
) {
// TODO figure out how to send priority and alert_type
self.client_sd.event(title, text, tags );
self.client_sd.event(title, text, tags ).unwrap()
}
}


impl DatadogMetricsBackend {
pub fn new(host: String, port: u16 , tags:Vec<String>) -> Self {

let host_port: String = format!("{}:{}", host, port);
let built_options = dogstatsd::OptionsBuilder::new()
let host_port: String = format!("{host}:{port}");
let options = dogstatsd::OptionsBuilder::new()
.to_addr(host_port).build();

let client = dogstatsd::Client::new(dogstatsd::Options::default()).unwrap();
let client = dogstatsd::Client::new(options).unwrap();
DatadogMetricsBackend {
client_sd: client,
tags
_tags: tags
}
}
}
Expand All @@ -43,9 +43,9 @@ impl MetricsClientTrait for DatadogMetricsBackend {
key: &str,
value: Option<i64>,
tags: Option<std::collections::HashMap<&str, &str>>,
sample_rate: Option<f64>,
_sample_rate: Option<f64>,
) {
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{}:{}", k, v)).collect();
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{k}:{v}")).collect();

match value {
Some(v) => {
Expand All @@ -70,21 +70,20 @@ impl MetricsClientTrait for DatadogMetricsBackend {
key: &str,
value: u64,
tags: Option<std::collections::HashMap<&str, &str>>,
sample_rate: Option<f64>,
_sample_rate: Option<f64>,
) {
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{}:{}", k, v)).collect();
let res = self.client_sd.gauge(key, value.to_string(), tags_str).unwrap();
println!("ok = {:?}", res)
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();
}

fn time(
&self,
key: &str,
value: u64,
tags: Option<std::collections::HashMap<&str, &str>>,
sample_rate: Option<f64>,
_sample_rate: Option<f64>,
) {
let tags_str: Vec<String> = tags.unwrap().iter().map(|(k, v)| format!("{}:{}", k, v)).collect();
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 All @@ -93,22 +92,19 @@ impl MetricsClientTrait for DatadogMetricsBackend {
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();
// default client sends metrics to statsd daemon on localhost:8125
let client = dogstatsd::Client::new(dogstatsd::Options::default()).unwrap();
let client_tags: Vec<String> = Vec::new();
let testing_backend = DatadogMetricsBackend {
client_sd: client,
tags: client_tag
_tags: client_tags
};

let mut tags: HashMap<&str, &str> = HashMap::new();
Expand All @@ -122,5 +118,9 @@ mod tests {
// check configure_metrics writes to METRICS
configure_metrics(testing_backend);
metrics::time("c", 30, Some(HashMap::from([("tag3", "value3")])), None);

// test constructor
DatadogMetricsBackend::new("0.0.0.0".to_owned(), 8125, Vec::new())
.counter("test_counter2", Some(2), Some(tags.clone()), None);
}
}
45 changes: 22 additions & 23 deletions rust_snuba/src/utils/metrics/backends/testing.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,84 @@
use std::{collections::HashMap, sync::{Arc, Mutex}};
use std::{collections::HashMap, sync::{Mutex}};

use rust_arroyo::utils::metrics::MetricsClientTrait;

use super::abstract_backend::MetricsBackend;
use lazy_static::lazy_static;

lazy_static! {
// static ref METRICS_CLIENT: RwLock<Option<Arc<dyn MetricsClientTrait>>> = RwLock::new(None);
static ref METRICS: Mutex<HashMap<String, Vec<MetricCall>>> = {
let mut m = HashMap::new();
let m = HashMap::new();
Mutex::new(m)
};
}
// static METRICS: Arc<HashMap<String, Vec<MetricCall>>> = Arc::new(HashMap::new());


pub struct MetricCall {
value: String,
tags: Vec<String>,
_value: String,
_tags: Vec<String>,
}

pub struct TestingMetricsBackend {
}

impl MetricsBackend for TestingMetricsBackend {
fn events(
&self,
title: &str,
text: &str,
alert_type: &str,
priority: &str,
tags: &[&str],
_title: &str,
_text: &str,
_alert_type: &str,
_priority: &str,
_tags: &[&str],
) {
todo!()
}
}

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>) {
let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
tags_vec.push(format!("{}:{}", k, v));
tags_vec.push(format!("{k}:{v}"));
}
}
let mut metrics_map = METRICS.lock().unwrap();
let metric = metrics_map.entry(name.to_string()).or_insert(Vec::new());
metric.push(MetricCall {
value: value.unwrap().to_string(),
tags: tags_vec,
_value: value.unwrap().to_string(),
_tags: tags_vec,
});
}

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>) {
let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
tags_vec.push(format!("{}:{}", k, v));
tags_vec.push(format!("{k}:{v}"));
}
}
let mut metrics_map = METRICS.lock().unwrap();
let metric = metrics_map.entry(name.to_string()).or_insert(Vec::new());

metric.push(MetricCall {
value: value.to_string(),
tags: tags_vec,
_value: value.to_string(),
_tags: tags_vec,
});
}

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>) {
let mut tags_vec = Vec::new();
if let Some(tags) = tags {
for (k, v) in tags {
tags_vec.push(format!("{}:{}", k, v));
tags_vec.push(format!("{k}:{v}"));
}
}
let mut metrics_map = METRICS.lock().unwrap();
let metric = metrics_map.entry(name.to_string()).or_insert(Vec::new());

metric.push(MetricCall {
value: value.to_string(),
tags: tags_vec,
_value: value.to_string(),
_tags: tags_vec,
});
}

Expand Down
2 changes: 0 additions & 2 deletions rust_snuba/src/utils/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod metrics;
pub mod backends;
pub mod metrics_wrapper;

0 comments on commit 7b73d0d

Please sign in to comment.