From 0b61c01550b1427db0f192c839121d345fc87a05 Mon Sep 17 00:00:00 2001 From: Francis Lalonde Date: Thu, 18 Oct 2018 09:31:53 -0400 Subject: [PATCH] template from vec --- examples/text_format_label.rs | 23 +++++++++++++--- src/lib.rs | 2 +- src/output/format.rs | 50 +++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/examples/text_format_label.rs b/examples/text_format_label.rs index 6f48648..a7f6ab8 100644 --- a/examples/text_format_label.rs +++ b/examples/text_format_label.rs @@ -5,16 +5,31 @@ extern crate dipstick; use std::thread::sleep; use std::time::Duration; -use dipstick::{Proxy, Stream, Counter, InputScope, Input, SimpleFormat, Formatting, AppLabel}; +use dipstick::{Proxy, Stream, Counter, InputScope, Input, Formatting, AppLabel, + Name, Kind, LineTemplate, LineFormat, LineOp, LabelOp}; metrics!{ COUNTER: Counter = "counter_a"; } -fn main() { - Proxy::set_default_target( - Stream::stderr().formatting(SimpleFormat::default()).input()); +struct MyFormat; +impl LineFormat for MyFormat { + fn template(&self, name: &Name, _kind: Kind) -> LineTemplate { + vec![ + LineOp::Literal(format!("{} ", name.join("."))), + LineOp::ValueAsText, + LineOp::Literal(" ".into()), + LineOp::LabelExists("abc".into(), + vec![LabelOp::LabelKey, LabelOp::Literal(":".into()), LabelOp::LabelValue], + ), + LineOp::NewLine, + ].into() + } +} + +fn main() { + Proxy::set_default_target(Stream::stderr().formatting(MyFormat).input()); AppLabel::set("abc", "xyz"); loop { // report some metric values from our "application" loop diff --git a/src/lib.rs b/src/lib.rs index cea052e..f32aee3 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ pub use core::clock::{mock_clock_advance, mock_clock_reset}; pub use core::proxy::Proxy; mod output; -pub use output::format::{LineFormat, SimpleFormat, LineToken, LineTemplate, Formatting}; +pub use output::format::{LineFormat, SimpleFormat, LineOp, LabelOp, LineTemplate, Formatting}; pub use output::stream::{Stream, TextScope}; pub use output::graphite::{Graphite, GraphiteScope, GraphiteMetric}; pub use output::statsd::{Statsd, StatsdScope, StatsdMetric}; diff --git a/src/output/format.rs b/src/output/format.rs index d59e529..67ef8d7 100644 --- a/src/output/format.rs +++ b/src/output/format.rs @@ -1,17 +1,17 @@ use core::name::Name; use core::input::Kind; use core::Value; -use self::LineToken::*; +use self::LineOp::*; use std::io; use std::sync::Arc; /// Print commands are steps in the execution of output templates. -pub enum LineToken { +pub enum LineOp { /// Print a string. Literal(String), /// Lookup and print label value for key, if it exists. - LabelExists(String, Vec), + LabelExists(String, Vec), /// Print metric value as text. ValueAsText, /// Print metric value, divided by the given scale, as text. @@ -21,7 +21,7 @@ pub enum LineToken { } /// Print commands are steps in the execution of output templates. -pub enum LabelToken { +pub enum LabelOp { /// Print a string. Literal(String), /// Print the label key. @@ -32,7 +32,13 @@ pub enum LabelToken { /// An sequence of print commands, embodying an output strategy for a single metric. pub struct LineTemplate { - commands: Vec + ops: Vec +} + +impl From> for LineTemplate { + fn from(ops: Vec) -> Self { + LineTemplate { ops } + } } impl LineTemplate { @@ -40,7 +46,7 @@ impl LineTemplate { pub fn print(&self, output: &mut io::Write, value: Value, lookup: L) -> Result<(), io::Error> where L: Fn(&str) -> Option> { - for cmd in &self.commands { + for cmd in &self.ops { match cmd { Literal(src) => output.write_all(src.as_ref())?, ValueAsText => output.write_all(format!("{}", value).as_ref())?, @@ -53,11 +59,11 @@ impl LineTemplate { if let Some(label_value) = lookup(label_key.as_ref()) { for label_cmd in print_label { match label_cmd { - LabelToken::LabelValue => + LabelOp::LabelValue => output.write_all(label_value.as_bytes())?, - LabelToken::LabelKey => + LabelOp::LabelKey => output.write_all(label_key.as_bytes())?, - LabelToken::Literal(src) => + LabelOp::Literal(src) => output.write_all(src.as_ref())?, } } @@ -94,7 +100,7 @@ impl LineFormat for SimpleFormat { let mut header = name.join("."); header.push(' '); LineTemplate { - commands: vec![ + ops: vec![ Literal(header), ValueAsText, NewLine, @@ -103,6 +109,22 @@ impl LineFormat for SimpleFormat { } } +//enum Parsed { +// Literal(String), +// Name() +// Value(Value), +// StaticLabel(String), +// DynamicLabel(String), +//} +// +//struct TemplateFormat { +// tokens: Vec +//} +// +//fn parse(template: &str) -> TemplateFormat { +// +//} + #[cfg(test)] pub mod test { @@ -118,16 +140,16 @@ pub mod test { header.push_str(&name.join(".")); header.push(' '); LineTemplate { - commands: vec![ + ops: vec![ Literal(header), ValueAsText, Literal(" ".into()), ScaledValueAsText(1000), Literal(" ".into()), LabelExists("test_key".into(), vec![ - LabelToken::LabelKey, - LabelToken::Literal("=".into()), - LabelToken::LabelValue]), + LabelOp::LabelKey, + LabelOp::Literal("=".into()), + LabelOp::LabelValue]), NewLine, ] }