Skip to content
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

omit namespace from service checks & events #18

Merged
merged 2 commits into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl Client {
M: Metric,
S: AsRef<str>,
{
let formatted_metric = format_for_send(&metric.metric_type_format(), &self.namespace, tags);
let formatted_metric = format_for_send(metric, &self.namespace, tags);
self.socket.send_to(formatted_metric.as_slice(), &self.to_addr)?;
Ok(())
}
Expand Down
43 changes: 35 additions & 8 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use chrono::{DateTime, Utc};

pub fn format_for_send<I, S>(metric: &str, namespace: &str, tags: I) -> Vec<u8>
where I: IntoIterator<Item=S>,
pub fn format_for_send<M, I, S>(in_metric: &M, in_namespace: &str, tags: I) -> Vec<u8>
where M: Metric,
I: IntoIterator<Item=S>,
S: AsRef<str>,
{
let metric = in_metric.metric_type_format();
let namespace = if in_metric.uses_namespace() {
in_namespace
} else {
""
};
let mut buf = Vec::with_capacity(metric.len() + namespace.len());

if !namespace.is_empty() {
Expand Down Expand Up @@ -35,6 +42,10 @@ pub fn format_for_send<I, S>(metric: &str, namespace: &str, tags: I) -> Vec<u8>

pub trait Metric {
fn metric_type_format(&self) -> String;

fn uses_namespace(&self) -> bool {
true
}
}

pub enum CountMetric<'a> {
Expand Down Expand Up @@ -259,6 +270,10 @@ pub struct ServiceCheck<'a> {
}

impl<'a> Metric for ServiceCheck<'a> {
fn uses_namespace(&self) -> bool {
false
}

// _sc|my_service.can_connect|1
fn metric_type_format(&self) -> String {
let mut buf = String::with_capacity(6 + self.stat.len() + self.options.len());
Expand Down Expand Up @@ -302,6 +317,10 @@ pub struct Event<'a> {
}

impl<'a> Metric for Event<'a> {
fn uses_namespace(&self) -> bool {
false
}

fn metric_type_format(&self) -> String {
let title_len = self.title.len().to_string();
let text_len = self.text.len().to_string();
Expand Down Expand Up @@ -335,24 +354,32 @@ mod tests {
#[test]
fn test_format_for_send_no_tags() {
assert_eq!(
&b"namespace.metric:val|v"[..],
&format_for_send("metric:val|v", "namespace", &[] as &[String])[..]
&b"namespace.foo:1|c"[..],
&format_for_send(&CountMetric::Incr("foo"), "namespace", &[] as &[String])[..]
)
}

#[test]
fn test_format_for_send_no_namespace() {
assert_eq!(
&b"metric:val|v|#tag:1,tag:2"[..],
&format_for_send("metric:val|v", "", &["tag:1", "tag:2"])[..]
&b"foo:1|c|#tag:1,tag:2"[..],
&format_for_send(&CountMetric::Incr("foo"), "", &["tag:1", "tag:2"])[..]
)
}

#[test]
fn test_format_for_send_everything() {
assert_eq!(
&b"namespace.metric:val|v|#tag:1,tag:2"[..],
&format_for_send("metric:val|v", "namespace", &["tag:1", "tag:2"])[..]
&b"namespace.foo:1|c|#tag:1,tag:2"[..],
&format_for_send(&CountMetric::Incr("foo"), "namespace", &["tag:1", "tag:2"])[..]
)
}

#[test]
fn test_format_for_send_everything_omit_namespace() {
assert_eq!(
&b"_e{5,4}:title|text|#tag:1,tag:2"[..],
&format_for_send(&Event::new("title".into(), "text".into()), "namespace", &["tag:1", "tag:2"])[..]
)
}

Expand Down