Skip to content

Commit

Permalink
Dd monitor custom metrics (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolfly86 authored and jonathanmorley committed Oct 1, 2019
1 parent e568dcc commit 5266c4e
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 21 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ deploy:
provider: releases
skip_cleanup: true

cache: cargo
before_cache:
# Travis can't cache files that are not readable by "others"
- chmod -R a+r $HOME/.cargo
# we are on nightly and the cache isn’t really helping us.
# we will re-add this once we are back on stable
# cache: cargo
# before_cache:
# # Travis can't cache files that are not readable by "others"
# - chmod -R a+r $HOME/.cargo

branches:
only:
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ zip = "0.5"
rocket = { version = "0.4", default-features = false }
rocket_contrib = "0.4"
rocket_lamb = "0.6"
dogstatsd = "0.6"

[dev-dependencies]
assert_cmd = "0.11"
Expand Down
87 changes: 87 additions & 0 deletions src/datadogstatsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use dogstatsd::{Client, Options};
use std::env;

pub struct DdMetrics {
default_tags: [String; 2],
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(),
}
}
}
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 dd_tags = [String::from("service:hogan"), env_tag];
DdMetrics {
default_tags: dd_tags,
client: Client::new(dd_options).unwrap(),
}
}
pub fn incr(&self, name: &str, url: &str) {
self.client
.incr(name, self.append_url_tag(url).iter())
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

pub fn decr(&self, name: &str, url: &str) {
self.client
.incr(name, self.append_url_tag(url).iter())
.unwrap_or_else(|err| self.error_msg(name, &err.to_string()));
}

pub fn gauge(&self, name: &str, url: &str, value: &str) {
self.client
.gauge(name, value, self.append_url_tag(url).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) {
info!("{} dd metrics failed with error {}", name, err)
}
}

pub enum CustomMetrics {
CacheMiss,
CacheHit,
RequestTime,
}

impl CustomMetrics {
pub fn metrics_name(self) -> &'static str {
match self {
CustomMetrics::CacheMiss => "hogan.cache_miss.counter",
CustomMetrics::CacheHit => "hogan.cache_hit.counter",
CustomMetrics::RequestTime => "hogan.request_time.gauge",
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod config;
pub mod git;
pub mod template;
pub mod transform;
pub mod datadogstatsd;

use regex::Regex;
use std::path::{Path, PathBuf};
Expand Down
Loading

0 comments on commit 5266c4e

Please sign in to comment.