Skip to content

Commit

Permalink
Adjust how librato source is calculated (#48)
Browse files Browse the repository at this point in the history
This modification removes the regex crate and simplifies the way
we pull out the librato source. We add tests and correct an error
if source is at the found at the end of the tag list.

Signed-off-by: Brian L. Troutwine <blt@postmates.com>
  • Loading branch information
blt authored Jul 21, 2016
1 parent 8043ac4 commit bd7d1e7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mime = "0.2.0"
rustc-serialize = "0.3.19"
chrono = "0.2"
url = "1.1.1"
regex = "0.1"
lalrpop-util = "0.11.0"
string_cache = "0.2.21"
clap = "2.9.2"
Expand Down
31 changes: 27 additions & 4 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use metric::Metric;

use config::Args;

use regex::Regex;
use std::sync::Arc;

use std::sync::mpsc::{Receiver, Sender, channel};
Expand Down Expand Up @@ -34,6 +33,19 @@ pub trait Backend {
}
}

pub fn librato_extract_source(tags: &str) -> &str {
for tag in tags.split(",") {
let mut tag_pieces = tag.split("=");
let name = tag_pieces.next();
let val = tag_pieces.next();

if name == Some("source") {
return val.unwrap()
}
}
"cernan"
}

/// Creates the collection of backends based on the paraemeters
///
pub fn factory(args: Args) -> Vec<Sender<Arc<server::Event>>> {
Expand Down Expand Up @@ -69,9 +81,7 @@ pub fn factory(args: Args) -> Vec<Sender<Arc<server::Event>>> {
// librato does not support arbitrary tags, only a 'source' tag. We have
// to parse the source tag--if it exists--out and ship only that.
thread::spawn(move || {
let re = Regex::new(r"(?x)(source=(?P<source>.*),+)?").unwrap();
let metric_source =
re.captures(&cp_args.tags).unwrap().name("source").unwrap_or("cernan");
let metric_source = librato_extract_source(&args.tags);
librato::Librato::new(&cp_args.librato_username.unwrap(),
&cp_args.librato_token.unwrap(),
metric_source,
Expand All @@ -82,3 +92,16 @@ pub fn factory(args: Args) -> Vec<Sender<Arc<server::Event>>> {
}
backends
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_librato_source_extraction() {
assert_eq!("cernan", librato_extract_source("flkjsdf"));
assert_eq!("testsrc", librato_extract_source("host=hs,source=testsrc"));
assert_eq!("testsrc", librato_extract_source("source=testsrc,host=hs"));
assert_eq!("testsrc", librato_extract_source("source=testsrc"));
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate mime;
extern crate rustc_serialize;
extern crate chrono;
extern crate url;
extern crate regex;
extern crate string_cache;
extern crate fern;
#[macro_use]
Expand Down

0 comments on commit bd7d1e7

Please sign in to comment.