Struct tokio_cadence::udp::TokioBatchUdpMetricSink
source · 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
impl TokioBatchUdpMetricSink
sourcepub fn from<T: ToSocketAddrs>(
host: T,
socket: UdpSocket
) -> MetricResult<(Self, Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>)>
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.
sourcepub fn builder<T: ToSocketAddrs>(
host: T,
socket: UdpSocket
) -> Builder<T, UdpSocket>
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.
sourcepub 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
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>>)>
with_options
insteadCreates 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
impl Clone for TokioBatchUdpMetricSink
source§fn clone(&self) -> TokioBatchUdpMetricSink
fn clone(&self) -> TokioBatchUdpMetricSink
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more