Skip to content

Commit

Permalink
Merge pull request #41 from jjcomer/branch-transform
Browse files Browse the repository at this point in the history
Branch transform support
  • Loading branch information
jjcomer authored Feb 26, 2020
2 parents c834f2f + b7ecb0b commit 5dd85fa
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 192 deletions.
87 changes: 44 additions & 43 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ doc = false

[package]
name = 'hogan'
version = '0.7.2'
version = '0.7.3'
authors = ['Jonathan Morley <[email protected]>']
edition = '2018'

Expand All @@ -31,6 +31,7 @@ actix-web = '2'
actix-rt = '1'
actix-service = '1'
futures = '0.3'
parking_lot = '0.10'

[dependencies.regex]
version = '1'
Expand Down
79 changes: 41 additions & 38 deletions src/app/datadogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,78 @@ use dogstatsd::{Client, Options};
use std::env;

pub struct DdMetrics {
default_tags: [String; 2],
default_tags: Vec<String>,
client: Client,
}
impl Default for DdMetrics {
fn default() -> Self {
let dd_options = Options::default();
DdMetrics {
default_tags: [String::from("service:hogan"), "env:unknown".to_string()],
client: Client::new(dd_options).unwrap(),
}
DdMetrics::new()
}
}
impl DdMetrics {
pub fn new() -> Self {
let dd_options = Options::default();
let mut env_tag = String::from("env: ");
let key = "ENV";
match env::var(key) {
Ok(val) => {
info!("{}: {}", key, val);
env_tag.push_str(&val);
}
Err(e) => {
info!("couldn't interpret {}: {}", key, e);
env_tag.push_str("unknown");
}
}
let env_name = env::var(key).unwrap_or_else(|_| "unknown".to_string());

let dd_tags = [String::from("service:hogan"), env_tag];
info!("Setting up datadog with environment {}", env_name);

let dd_tags = vec![String::from("service:hogan"), format!("env:{}", env_name)];
DdMetrics {
default_tags: dd_tags,
client: Client::new(dd_options).unwrap(),
}
}
pub fn incr(&self, name: &str, url: &str) {
pub fn incr(&self, name: &str, additional_tags: Option<Vec<String>>) {
self.client
.incr(name, self.append_url_tag(url).iter())
.incr(
name,
additional_tags
.unwrap_or_default()
.iter()
.chain(self.default_tags.iter()),
)
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

pub fn decr(&self, name: &str, url: &str) {
pub fn decr(&self, name: &str, additional_tags: Option<Vec<String>>) {
self.client
.decr(name, self.append_url_tag(url).iter())
.decr(
name,
additional_tags
.unwrap_or_default()
.iter()
.chain(self.default_tags.iter()),
)
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

pub fn gauge(&self, name: &str, url: &str, value: &str) {
pub fn gauge(&self, name: &str, additional_tags: Option<Vec<String>>, value: &str) {
self.client
.gauge(name, value, self.append_url_tag(url).iter())
.gauge(
name,
value,
additional_tags
.unwrap_or_default()
.iter()
.chain(self.default_tags.iter()),
)
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

pub fn time(&self, name: &str, url: &str, value: i64) {
pub fn time(&self, name: &str, additional_tags: Option<Vec<String>>, value: i64) {
self.client
.timing(name, value, self.append_url_tag(url).iter())
.timing(
name,
value,
additional_tags
.unwrap_or_default()
.iter()
.chain(self.default_tags.iter()),
)
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

fn append_url_tag(&self, url: &str) -> Vec<String> {
let mut dd_tags = Vec::new();
dd_tags.extend_from_slice(&self.default_tags);

let mut url_tag = String::from("request_url: ");
url_tag.push_str(url);

dd_tags.push(url_tag);
dd_tags
}

fn error_msg(&self, name: &str, err: &str) {
warn!("{} dd metrics failed with error {}", name, err)
}
Expand All @@ -87,7 +90,7 @@ impl CustomMetrics {
match self {
CustomMetrics::CacheMiss => "hogan.cache_miss.counter",
CustomMetrics::CacheHit => "hogan.cache_hit.counter",
CustomMetrics::RequestTime => "hogan.request_time.gauge",
CustomMetrics::RequestTime => "hogan.requests",
}
}
}
Loading

0 comments on commit 5dd85fa

Please sign in to comment.