production_sink/
production-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 configure the Cadence client for maximum
11// isolation and performance. The buffered UDP sink accumulates multiple metrics
12// in a buffer before writing to the network. The queuing sink runs the wrapped
13// sink in a separate thread ensuring it doesn't interfere with your application.
14
15use cadence::prelude::*;
16use cadence::{BufferedUdpMetricSink, QueuingMetricSink, StatsdClient, DEFAULT_PORT};
17use std::net::UdpSocket;
18use std::time::Duration;
19
20fn main() {
21    let sock = UdpSocket::bind("0.0.0.0:0").unwrap();
22    let buffered = BufferedUdpMetricSink::from(("localhost", DEFAULT_PORT), sock).unwrap();
23    let queued = QueuingMetricSink::from(buffered);
24    let client = StatsdClient::from_sink("example.prefix", queued);
25
26    client.count("example.counter", 1).unwrap();
27    client.gauge("example.gauge", 5).unwrap();
28    client.gauge("example.gauge", 5.0).unwrap();
29    client.time("example.timer", 32).unwrap();
30    client.time("example.timer", Duration::from_millis(32)).unwrap();
31    client.histogram("example.histogram", 22).unwrap();
32    client.histogram("example.histogram", Duration::from_nanos(22)).unwrap();
33    client.histogram("example.histogram", 22.0).unwrap();
34    client.distribution("example.distribution", 33).unwrap();
35    client.distribution("example.distribution", 33.0).unwrap();
36    client.meter("example.meter", 8).unwrap();
37    client.set("example.set", 44).unwrap();
38}