spy_sink/
spy-sink.rs

1// Cadence - An extensible Statsd client for Rust!
2//
3// To the extent possible under law, the author(s) have dedicated all copyright and
4// related and neighboring rights to this file to the public domain worldwide.
5// This software is distributed without any warranty.
6//
7// You should have received a copy of the CC0 Public Domain Dedication along with this
8// software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
9
10// This example shows how you might make use of the "Spy" sinks in Cadence
11// which are meant for integration testing your application. They provide callers
12// with the `Receiver` half of a Crossbeam channel that metrics can be read from
13// to verify that Cadence wrote what you thought it was going to write.
14
15use cadence::prelude::*;
16use cadence::{BufferedSpyMetricSink, StatsdClient};
17use std::time::Duration;
18
19fn main() {
20    // Ensure that the sink is dropped, forcing a flush of all buffered metrics.
21    let rx = {
22        // Use a buffer size larger than any metrics here so we can demonstrate that
23        // each metric ends up with a newline (\n) after it.
24        let (rx, sink) = BufferedSpyMetricSink::with_capacity(None, Some(64));
25        let client = StatsdClient::from_sink("example.prefix", sink);
26
27        client.count("example.counter", 1).unwrap();
28        client.gauge("example.gauge", 5).unwrap();
29        client.gauge("example.gauge", 5.0).unwrap();
30        client.time("example.timer", 32).unwrap();
31        client.time("example.timer", Duration::from_millis(32)).unwrap();
32        client.histogram("example.histogram", 22).unwrap();
33        client.histogram("example.histogram", Duration::from_nanos(22)).unwrap();
34        client.histogram("example.histogram", 22.0).unwrap();
35        client.distribution("example.distribution", 33).unwrap();
36        client.distribution("example.distribution", 33.0).unwrap();
37        client.meter("example.meter", 8).unwrap();
38        client.set("example.set", 44).unwrap();
39
40        rx
41    };
42
43    let mut buffer = Vec::new();
44    while let Ok(v) = rx.try_recv() {
45        buffer.extend(v);
46    }
47
48    println!("Contents of wrapped buffer:\n{}", String::from_utf8(buffer).unwrap());
49}