pub struct TokioBatchUdpMetricSink { /* private fields */ }
Expand description

Metric sink that allows clients to enqueue metrics without blocking, and sending them asynchronously via UDP using Tokio runtime.

It also accumulates individual metrics for a configured maximum amount of time before submitting them as a single batch.

Exceeding the configured queue capacity results in an error, which the client may handle as appropriate.

§Important!

The client is responsible for polling the asynchronous processing future, which is created along with the sink, in a manner appropriate for the application (e.g., spawning it in a Tokio task pool).

The client should also wait for this future to complete after dropping the metric sink.

§Example

use cadence::prelude::*;
use cadence::{StatsdClient, DEFAULT_PORT};
use tokio_cadence::TokioBatchUdpMetricSink;
use tokio::{spawn, net::UdpSocket};

let host = ("metrics.example.com", DEFAULT_PORT);
let socket = UdpSocket::bind("0.0.0.0:0").await?;
let (sink, process) = TokioBatchUdpMetricSink::from(host, socket)?;

// Spawn the future!
let processing_job = spawn(process);

{
    let client = StatsdClient::from_sink("my.metrics", sink);

    // Emit metrics!
    client.incr("some.counter");
    client.time("some.methodCall", 42);
    client.gauge("some.thing", 7);
    client.meter("some.value", 5);

    // the client drops here, and the sink along with it
}

// Wait for the processing job to complete!
processing_job.await.unwrap();

Implementations§

source§

impl TokioBatchUdpMetricSink

source

pub fn from<T: ToSocketAddrs>( host: T, socket: UdpSocket ) -> MetricResult<(Self, Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>)>

Creates a new metric sink for the given statsd host using a previously bound UDP socket. Other sink parameters are defaulted.

§Errors

Returns an error when unable to resolve the configured host address.

source

pub fn builder<T: ToSocketAddrs>( host: T, socket: UdpSocket ) -> Builder<T, UdpSocket>

Returns a builder for creating a new metric sink for the given statsd host using a previously bound UDP socket. The builder may be used to customize various configuration parameters before creating an instance of this sink.

source

pub fn with_capacity<T: ToSocketAddrs>( host: T, socket: UdpSocket, queue_capacity: usize, buf_size: usize, max_delay: u64 ) -> MetricResult<(Self, Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>)>

👎Deprecated: please use with_options instead

Creates a new metric sink for the given statsd host, using the UDP socket, as well as metric queue capacity, batch buffer size, and maximum delay (in milliseconds) to wait before submitting any accumulated metrics as a batch.

§Errors

Returns an error when unable to resolve the given host address, or when the queue capacity is 0.

Trait Implementations§

source§

impl Clone for TokioBatchUdpMetricSink

source§

fn clone(&self) -> TokioBatchUdpMetricSink

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TokioBatchUdpMetricSink

source§

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

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

impl MetricSink for TokioBatchUdpMetricSink

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§

impl RefUnwindSafe for TokioBatchUdpMetricSink

source§

impl UnwindSafe for TokioBatchUdpMetricSink

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.