-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rust): add some metrics backends #4063
Open
dbanda
wants to merge
7
commits into
master
Choose a base branch
from
dbanda/rust/add_metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e09844f
make metrics a trait
dbanda 4d6db85
add some implementations of metrics backends
dbanda a185dcc
add unit tests
dbanda 4c95e5c
linting
dbanda 7b73d0d
linting, remove some comments and old libs
dbanda e04575f
add sampling
dbanda 14d6aa8
use count instead of a series on incr/decr
dbanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ mod config; | |
mod consumer; | ||
mod strategies; | ||
mod types; | ||
pub mod utils; | ||
|
||
use pyo3::prelude::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use rust_arroyo::utils::metrics::MetricsClientTrait; | ||
|
||
pub trait MetricsBackend: MetricsClientTrait { | ||
fn events( | ||
&self, | ||
title: &str, | ||
text: &str, | ||
alert_type: &str, | ||
priority: &str, | ||
tags: &[&str], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use rust_arroyo::utils::metrics::{MetricsClientTrait}; | ||
use dogstatsd; | ||
|
||
use super::abstract_backend::MetricsBackend; | ||
pub struct DatadogMetricsBackend { | ||
client_sd: dogstatsd::Client, | ||
_tags: Vec<String>, | ||
} | ||
|
||
impl MetricsBackend for DatadogMetricsBackend { | ||
fn events( | ||
&self, | ||
title: &str, | ||
text: &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 ).unwrap() | ||
} | ||
} | ||
|
||
|
||
impl DatadogMetricsBackend { | ||
pub fn new(host: String, port: u16 , tags:Vec<String>) -> Self { | ||
|
||
let host_port: String = format!("{host}:{port}"); | ||
let options = dogstatsd::OptionsBuilder::new() | ||
.to_addr(host_port).build(); | ||
|
||
let client = dogstatsd::Client::new(options).unwrap(); | ||
DatadogMetricsBackend { | ||
client_sd: client, | ||
_tags: tags | ||
} | ||
} | ||
} | ||
|
||
impl MetricsClientTrait for DatadogMetricsBackend { | ||
fn counter( | ||
&self, | ||
key: &str, | ||
value: Option<i64>, | ||
tags: Option<std::collections::HashMap<&str, &str>>, | ||
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 { | ||
Some(v) => { | ||
self.client_sd.count(key, v, &tags_str).unwrap(); | ||
} | ||
None => { | ||
self.client_sd.incr(key, tags_str).unwrap(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
fn gauge( | ||
&self, | ||
key: &str, | ||
value: u64, | ||
tags: Option<std::collections::HashMap<&str, &str>>, | ||
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(); | ||
} | ||
|
||
fn time( | ||
&self, | ||
key: &str, | ||
value: u64, | ||
tags: Option<std::collections::HashMap<&str, &str>>, | ||
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(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use rust_arroyo::utils::metrics::{configure_metrics, MetricsClientTrait, self}; | ||
|
||
use crate::utils::metrics::backends::{datadog::DatadogMetricsBackend}; | ||
|
||
|
||
#[test] | ||
fn test_testing_backend() { | ||
// 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_tags | ||
}; | ||
|
||
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); | ||
|
||
// test constructor | ||
DatadogMetricsBackend::new("0.0.0.0".to_owned(), 8125, Vec::new()) | ||
.counter("test_counter2", Some(2), Some(tags.clone()), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod datadog; | ||
pub mod abstract_backend; | ||
pub mod testing; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not idiomatic to name traits as
Trait