mirror of https://github.com/fralalonde/dipstick
template from vec
This commit is contained in:
parent
40bef5115c
commit
0b61c01550
|
@ -5,16 +5,31 @@ extern crate dipstick;
|
||||||
|
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
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!{
|
metrics!{
|
||||||
COUNTER: Counter = "counter_a";
|
COUNTER: Counter = "counter_a";
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
struct MyFormat;
|
||||||
Proxy::set_default_target(
|
|
||||||
Stream::stderr().formatting(SimpleFormat::default()).input());
|
|
||||||
|
|
||||||
|
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");
|
AppLabel::set("abc", "xyz");
|
||||||
loop {
|
loop {
|
||||||
// report some metric values from our "application" loop
|
// report some metric values from our "application" loop
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub use core::clock::{mock_clock_advance, mock_clock_reset};
|
||||||
pub use core::proxy::Proxy;
|
pub use core::proxy::Proxy;
|
||||||
|
|
||||||
mod output;
|
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::stream::{Stream, TextScope};
|
||||||
pub use output::graphite::{Graphite, GraphiteScope, GraphiteMetric};
|
pub use output::graphite::{Graphite, GraphiteScope, GraphiteMetric};
|
||||||
pub use output::statsd::{Statsd, StatsdScope, StatsdMetric};
|
pub use output::statsd::{Statsd, StatsdScope, StatsdMetric};
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use core::name::Name;
|
use core::name::Name;
|
||||||
use core::input::Kind;
|
use core::input::Kind;
|
||||||
use core::Value;
|
use core::Value;
|
||||||
use self::LineToken::*;
|
use self::LineOp::*;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// Print commands are steps in the execution of output templates.
|
/// Print commands are steps in the execution of output templates.
|
||||||
pub enum LineToken {
|
pub enum LineOp {
|
||||||
/// Print a string.
|
/// Print a string.
|
||||||
Literal(String),
|
Literal(String),
|
||||||
/// Lookup and print label value for key, if it exists.
|
/// Lookup and print label value for key, if it exists.
|
||||||
LabelExists(String, Vec<LabelToken>),
|
LabelExists(String, Vec<LabelOp>),
|
||||||
/// Print metric value as text.
|
/// Print metric value as text.
|
||||||
ValueAsText,
|
ValueAsText,
|
||||||
/// Print metric value, divided by the given scale, as text.
|
/// 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.
|
/// Print commands are steps in the execution of output templates.
|
||||||
pub enum LabelToken {
|
pub enum LabelOp {
|
||||||
/// Print a string.
|
/// Print a string.
|
||||||
Literal(String),
|
Literal(String),
|
||||||
/// Print the label key.
|
/// Print the label key.
|
||||||
|
@ -32,7 +32,13 @@ pub enum LabelToken {
|
||||||
|
|
||||||
/// An sequence of print commands, embodying an output strategy for a single metric.
|
/// An sequence of print commands, embodying an output strategy for a single metric.
|
||||||
pub struct LineTemplate {
|
pub struct LineTemplate {
|
||||||
commands: Vec<LineToken>
|
ops: Vec<LineOp>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<LineOp>> for LineTemplate {
|
||||||
|
fn from(ops: Vec<LineOp>) -> Self {
|
||||||
|
LineTemplate { ops }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LineTemplate {
|
impl LineTemplate {
|
||||||
|
@ -40,7 +46,7 @@ impl LineTemplate {
|
||||||
pub fn print<L>(&self, output: &mut io::Write, value: Value, lookup: L) -> Result<(), io::Error>
|
pub fn print<L>(&self, output: &mut io::Write, value: Value, lookup: L) -> Result<(), io::Error>
|
||||||
where L: Fn(&str) -> Option<Arc<String>>
|
where L: Fn(&str) -> Option<Arc<String>>
|
||||||
{
|
{
|
||||||
for cmd in &self.commands {
|
for cmd in &self.ops {
|
||||||
match cmd {
|
match cmd {
|
||||||
Literal(src) => output.write_all(src.as_ref())?,
|
Literal(src) => output.write_all(src.as_ref())?,
|
||||||
ValueAsText => output.write_all(format!("{}", value).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()) {
|
if let Some(label_value) = lookup(label_key.as_ref()) {
|
||||||
for label_cmd in print_label {
|
for label_cmd in print_label {
|
||||||
match label_cmd {
|
match label_cmd {
|
||||||
LabelToken::LabelValue =>
|
LabelOp::LabelValue =>
|
||||||
output.write_all(label_value.as_bytes())?,
|
output.write_all(label_value.as_bytes())?,
|
||||||
LabelToken::LabelKey =>
|
LabelOp::LabelKey =>
|
||||||
output.write_all(label_key.as_bytes())?,
|
output.write_all(label_key.as_bytes())?,
|
||||||
LabelToken::Literal(src) =>
|
LabelOp::Literal(src) =>
|
||||||
output.write_all(src.as_ref())?,
|
output.write_all(src.as_ref())?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +100,7 @@ impl LineFormat for SimpleFormat {
|
||||||
let mut header = name.join(".");
|
let mut header = name.join(".");
|
||||||
header.push(' ');
|
header.push(' ');
|
||||||
LineTemplate {
|
LineTemplate {
|
||||||
commands: vec![
|
ops: vec![
|
||||||
Literal(header),
|
Literal(header),
|
||||||
ValueAsText,
|
ValueAsText,
|
||||||
NewLine,
|
NewLine,
|
||||||
|
@ -103,6 +109,22 @@ impl LineFormat for SimpleFormat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//enum Parsed {
|
||||||
|
// Literal(String),
|
||||||
|
// Name()
|
||||||
|
// Value(Value),
|
||||||
|
// StaticLabel(String),
|
||||||
|
// DynamicLabel(String),
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//struct TemplateFormat {
|
||||||
|
// tokens: Vec<LineToken>
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//fn parse(template: &str) -> TemplateFormat {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod test {
|
pub mod test {
|
||||||
|
@ -118,16 +140,16 @@ pub mod test {
|
||||||
header.push_str(&name.join("."));
|
header.push_str(&name.join("."));
|
||||||
header.push(' ');
|
header.push(' ');
|
||||||
LineTemplate {
|
LineTemplate {
|
||||||
commands: vec![
|
ops: vec![
|
||||||
Literal(header),
|
Literal(header),
|
||||||
ValueAsText,
|
ValueAsText,
|
||||||
Literal(" ".into()),
|
Literal(" ".into()),
|
||||||
ScaledValueAsText(1000),
|
ScaledValueAsText(1000),
|
||||||
Literal(" ".into()),
|
Literal(" ".into()),
|
||||||
LabelExists("test_key".into(), vec![
|
LabelExists("test_key".into(), vec![
|
||||||
LabelToken::LabelKey,
|
LabelOp::LabelKey,
|
||||||
LabelToken::Literal("=".into()),
|
LabelOp::Literal("=".into()),
|
||||||
LabelToken::LabelValue]),
|
LabelOp::LabelValue]),
|
||||||
NewLine,
|
NewLine,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue