Trait cadence::MetricSink

source ·
pub trait MetricSink {
    // Required method
    fn emit(&self, metric: &str) -> Result<usize>;

    // Provided methods
    fn flush(&self) -> Result<()> { ... }
    fn stats(&self) -> SinkStats { ... }
}
Expand description

Trait for various backends that send Statsd metrics somewhere.

The metric string will be in the canonical format to be sent to a Statsd server. The metric string will not include a trailing newline. Examples of each supported metric type are given below.

§Counter

some.counter:123|c

§Timer

some.timer:456|ms

§Gauge

some.gauge:5|g

§Meter

some.meter:8|m

§Histogram

some.histogram:4|h

§Set

some.set:2|s

§Distribution

some.distribution:2|d

See the Statsd spec for more information.

Required Methods§

source

fn emit(&self, metric: &str) -> Result<usize>

Send the Statsd metric using this sink and return the number of bytes written or an I/O error.

Note that implementations may return 0 bytes if the metric is not immediately written (such as when it is buffered). Callers should NOT interpret this as an error.

Provided Methods§

source

fn flush(&self) -> Result<()>

Flush any currently buffered metrics to the underlying backend, returning an I/O error if they could not be written for some reason.

Note that not all sinks buffer metrics and so the default implementation of this method does nothing.

Examples found in repository?
examples/wrapped.rs (line 46)
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
    fn flush(&self) -> io::Result<()> {
        self.wrapped.flush()
    }
}

fn main() {
    let real_sink = NopMetricSink;
    let reference1 = CloneableSink::new(real_sink);
    let reference2 = reference1.clone();
    let client = StatsdClient::from_sink("prefix", reference1);

    let _ = reference2.flush();

    client.count("example.counter", 1).unwrap();
    client.gauge("example.gauge", 5).unwrap();
    client.gauge("example.gauge", 5.0).unwrap();
    client.time("example.timer", 32).unwrap();
    client.time("example.timer", Duration::from_millis(32)).unwrap();
    client.histogram("example.histogram", 22).unwrap();
    client.histogram("example.histogram", Duration::from_nanos(22)).unwrap();
    client.histogram("example.histogram", 22.0).unwrap();
    client.distribution("example.distribution", 33).unwrap();
    client.distribution("example.distribution", 33.0).unwrap();
    client.meter("example.meter", 8).unwrap();
    client.set("example.set", 44).unwrap();

    let _ = reference2.flush();
}
source

fn stats(&self) -> SinkStats

Return I/O telemetry like bytes / packets sent or dropped.

Note that not all sinks implement this method and the default implementation returns zeros.

Implementors§