pub struct UnixMetricSink { /* private fields */ }Expand description
Implementation of a MetricSink that emits metrics over a Unix socket.
This is the most basic version of MetricSink that sends metrics over
a Unix socket. It accepts a Unix socket instance over which to write metrics
and the path of the socket for the Statsd server to send metrics to.
Each metric is sent to the Statsd server when the .emit() method is
called, in the thread of the caller.
Note that unlike the UDP sinks, if there is no receiving socket at the path specified or nothing listening at the path, an error will be returned when metrics are emitted.
Implementations§
Source§impl UnixMetricSink
impl UnixMetricSink
Sourcepub fn from<P>(path: P, socket: UnixDatagram) -> UnixMetricSink
pub fn from<P>(path: P, socket: UnixDatagram) -> UnixMetricSink
Construct a new UnixMetricSink instance.
The socket does not need to be bound (i.e. UnixDatagram::unbound() is
fine) but should have any desired configuration already applied
(blocking vs non-blocking, timeouts, etc.).
§Example
use std::os::unix::net::UnixDatagram;
use cadence::UnixMetricSink;
let socket = UnixDatagram::unbound().unwrap();
let sink = UnixMetricSink::from("/run/statsd.sock", socket);To send metrics over a non-blocking socket, simply put the socket in non-blocking mode before creating the Unix metric sink.
§Non-blocking Example
use std::os::unix::net::UnixDatagram;
use cadence::UnixMetricSink;
let socket = UnixDatagram::unbound().unwrap();
socket.set_nonblocking(true).unwrap();
let sink = UnixMetricSink::from("/run/statsd.sock", socket);Examples found in repository?
26fn main() {
27 let harness = UnixServerHarness::new("unix-socket-example");
28 harness.run(
29 |s: String| println!("Got {} bytes from socket: {}", s.len(), s),
30 |path| {
31 let socket = UnixDatagram::unbound().unwrap();
32 let sink = UnixMetricSink::from(path, socket);
33 let client = StatsdClient::from_sink("example.prefix", sink);
34
35 client.count("example.counter", 1).unwrap();
36 client.gauge("example.gauge", 5).unwrap();
37 client.gauge("example.gauge", 5.0).unwrap();
38 client.time("example.timer", 32).unwrap();
39 client.time("example.timer", Duration::from_millis(32)).unwrap();
40 client.histogram("example.histogram", 22).unwrap();
41 client.histogram("example.histogram", Duration::from_nanos(22)).unwrap();
42 client.histogram("example.histogram", 22.0).unwrap();
43 client.distribution("example.distribution", 33).unwrap();
44 client.distribution("example.distribution", 33.0).unwrap();
45 client.meter("example.meter", 8).unwrap();
46 client.set("example.set", 44).unwrap();
47 },
48 );
49}