1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Standard stateless metric outputs.
// TODO parameterize templates
use core::*;
use std::sync::RwLock;

/// Write metric values to stdout using `println!`.
pub fn to_stdout() -> Chain<String> {
    Chain::new(
        |_kind, name, _rate| String::from(name),
        |buffered| {
            if !buffered {
                ControlScopeFn::new(|cmd| {
                    if let ScopeCmd::Write(m, v) = cmd {
                        println!("{}: {}", m, v)
                    }
                })
            } else {
                let buf = RwLock::new(String::new());
                ControlScopeFn::new(move |cmd| {
                    let mut buf = buf.write().expect("Locking stdout buffer");
                    match cmd {
                        ScopeCmd::Write(metric, value) => buf.push_str(format!("{}: {}\n", metric, value).as_ref()),
                        ScopeCmd::Flush => {
                            println!("{}", buf);
                            buf.clear();
                        }
                    }
                })
            }
        },
    )
}

/// Write metric values to the standard log using `info!`.
// TODO parameterize log level
pub fn to_log() -> Chain<String> {
    Chain::new(
        |_kind, name, _rate| String::from(name),
        |buffered| {
            if !buffered {
                ControlScopeFn::new(|cmd| {
                    if let ScopeCmd::Write(m, v) = cmd {
                        info!("{}: {}", m, v)
                    }
                })
            } else {
                let buf = RwLock::new(String::new());
                ControlScopeFn::new(move |cmd| {
                    let mut buf = buf.write().expect("Locking string buffer");
                    match cmd {
                        ScopeCmd::Write(metric, value) => buf.push_str(format!("{}: {}\n", metric, value).as_ref()),
                        ScopeCmd::Flush => {
                            info!("{}", buf);
                            buf.clear();
                        }
                    }
                })
            }
        },
    )
}

/// Discard all metric values sent to it.
pub fn to_void() -> Chain<String> {
    Chain::new(
        move |_kind, name, _rate| String::from(name),
        |_buffered| ControlScopeFn::new(|_cmd| {}),
    )
}

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

    #[test]
    fn sink_print() {
        let c = super::to_stdout();
        let m = c.define_metric(Kind::Marker, "test", 1.0);
        c.open_scope(true).write(&m, 33);
    }

    #[test]
    fn test_to_log() {
        let c = super::to_log();
        let m = c.define_metric(Kind::Marker, "test", 1.0);
        c.open_scope(true).write(&m, 33);
    }

    #[test]
    fn test_to_void() {
        let c = super::to_void();
        let m = c.define_metric(Kind::Marker, "test", 1.0);
        c.open_scope(true).write(&m, 33);
    }

}