Struct cadence::UdpMetricSink

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

Implementation of a MetricSink that emits metrics over UDP.

This is the most basic version of MetricSink that sends metrics over UDP. It accepts a UDP socket instance over which to write metrics and the address of the Statsd server to send packets to.

Each metric is sent to the Statsd server when the .emit() method is called, in the thread of the caller.

Implementations§

source§

impl UdpMetricSink

source

pub fn from<A>(to_addr: A, socket: UdpSocket) -> MetricResult<UdpMetricSink>
where A: ToSocketAddrs,

Construct a new UdpMetricSink instance.

The address should be the address of the remote metric server to emit metrics to over UDP. The socket should already be bound to a local address with any desired configuration applied (blocking vs non-blocking, timeouts, etc.).

§Example
use std::net::UdpSocket;
use cadence::{UdpMetricSink, DEFAULT_PORT};

let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
let host = ("metrics.example.com", DEFAULT_PORT);
let sink = UdpMetricSink::from(host, socket);

To send metrics over a non-blocking socket, simply put the socket in non-blocking mode before creating the UDP metric sink.

§Non-blocking Example

Note that putting the UDP socket into non-blocking mode is the default when sink and socket are automatically created with the StatsdClient::from_udp_host method.

use std::net::UdpSocket;
use cadence::{UdpMetricSink, DEFAULT_PORT};

let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
socket.set_nonblocking(true).unwrap();
let host = ("metrics.example.com", DEFAULT_PORT);
let sink = UdpMetricSink::from(host, socket);
§Failures

This method may fail if:

  • It is unable to resolve the hostname of the metric server.
  • The host address is otherwise unable to be parsed
Examples found in repository?
examples/simple-sink.rs (line 21)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    let sock = UdpSocket::bind("0.0.0.0:0").unwrap();
    let sink = UdpMetricSink::from(("localhost", DEFAULT_PORT), sock).unwrap();
    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();
}

Trait Implementations§

source§

impl Debug for UdpMetricSink

source§

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

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

impl MetricSink for UdpMetricSink

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 stats(&self) -> SinkStats

Return I/O telemetry like bytes / packets sent or dropped. 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

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.