Struct cadence::BufferedSpyMetricSink

source ·
pub struct BufferedSpyMetricSink { /* private fields */ }
Expand description

MetricSink implementation that buffers metrics and writes them to the Sender half of a channel while callers are given ownership of the Receiver half.

This is not a general purpose sink, rather it’s a sink meant for verifying metrics written during the course of integration tests. By default, the channel used is unbounded. The channel size can be limited using the with_capacity method.

Metrics are line buffered, meaning that a trailing “\n” is added after each metric written to this sink. When the buffer is sufficiently full and a write is attempted, the contents of the buffer are flushed to the underlying writer and then the metric is written to the buffer. The buffer is also flushed when this sink is destroyed.

The default size of the buffer is 512 bytes. This is to be consistent with the default for the BufferedUdpMetricSink. The buffer size can be customized using the with_capacity method to create the sink if desired.

If a metric larger than the buffer is emitted, it will be written directly to the underlying writer, bypassing the buffer.

Note that since metrics are buffered until a certain size is reached, it’s possible that they may sit in the buffer for a while for applications that do not emit metrics frequently or at a high volume. For these low- throughput use cases, it may make more sense to use the SpyMetricSink since it sends metrics immediately with no buffering.

Implementations§

source§

impl BufferedSpyMetricSink

source

pub fn new() -> (Receiver<Vec<u8>>, Self)

source

pub fn with_capacity( queue: Option<usize>, buffer: Option<usize> ) -> (Receiver<Vec<u8>>, Self)

Examples found in repository?
examples/spy-sink.rs (line 24)
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
fn main() {
    // Ensure that the sink is dropped, forcing a flush of all buffered metrics.
    let rx = {
        // Use a buffer size larger than any metrics here so we can demonstrate that
        // each metric ends up with a newline (\n) after it.
        let (rx, sink) = BufferedSpyMetricSink::with_capacity(None, Some(64));
        let client = StatsdClient::from_sink("example.prefix", sink);

        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();

        rx
    };

    let mut buffer = Vec::new();
    while let Ok(v) = rx.try_recv() {
        buffer.extend(v);
    }

    println!("Contents of wrapped buffer:\n{}", String::from_utf8(buffer).unwrap());
}

Trait Implementations§

source§

impl Debug for BufferedSpyMetricSink

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl MetricSink for BufferedSpyMetricSink

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. Read more
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. Read more
source§

fn stats(&self) -> SinkStats

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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.