Struct cadence::AsyncMetricSink
[−]
[src]
pub struct AsyncMetricSink<T: 'static + MetricSink + Send + Sync> { /* fields omitted */ }
Implementation of a MetricSink
that wraps another implementation
and uses it to emit metrics asynchronously with a thread pool.
The wrapped implementation can by any thread safe (Send + Sync
)
MetricSink
implementation. Results from the wrapped implementation
will be discarded.
Because this MetricSink
implementation uses a thread pool, the sink
itself cannot be shared between threads. Instead, callers may opt to
create a .clone()
for each thread that needs to emit metrics. This
of course requires that the wrapped sink implements the Clone
trait
(all of the sinks that are part of Cadence implement Clone
).
When cloned, the new instance of this sink will have a cloned thread
pool instance that submits jobs to the same worker threads as the
original pool and a reference (Arc
) to the wrapped sink. If you
plan on cloning this sink, the thread pool should be sized
appropriately to be used by all expected sink instances.
Methods
impl<T: 'static + MetricSink + Send + Sync> AsyncMetricSink<T>
[src]
fn from(sink: T) -> AsyncMetricSink<T>
Construct a new AsyncMetricSink
instance wrapping another sink
implementation.
The .emit()
method of the wrapped sink will be executed in a
different thread via a thread pool. The wrapped sink should be
thread safe (Send + Sync
).
The default thread pool size is four threads. Callers can use
more or fewer threads by making use of the with_threadpool
constructor.
UDP Sink Example
In this example we wrap the basic UDP sink to execute it in a different thread.
use std::net::UdpSocket; use cadence::{UdpMetricSink, AsyncMetricSink, DEFAULT_PORT}; let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let host = ("metrics.example.com", DEFAULT_PORT); let udp_sink = UdpMetricSink::from(host, socket).unwrap(); let async_sink = AsyncMetricSink::from(udp_sink);
Buffered UDP Sink Example
This example uses the buffered UDP sink, wrapped in the async metric sink.
use std::net::UdpSocket; use cadence::{BufferedUdpMetricSink, AsyncMetricSink, DEFAULT_PORT}; let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let host = ("metrics.example.com", DEFAULT_PORT); let udp_sink = BufferedUdpMetricSink::from(host, socket).unwrap(); let async_sink = AsyncMetricSink::from(udp_sink);
fn with_threadpool(sink: T, pool: ThreadPool) -> AsyncMetricSink<T>
Construct a new AsyncMetricSink
instance wrapping another sink
implementation that will use the provided thread pool for
asynchronous execution.
The .emit()
method of the wrapped sink will be executed in a
different thread via a thread pool. The wrapped sink should be
thread safe (Send + Sync
).
Buffered UDP Sink With a Single Threaded Pool
use std::net::UdpSocket; use threadpool::ThreadPool; use cadence::{BufferedUdpMetricSink, AsyncMetricSink, DEFAULT_PORT}; let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); let host = ("metrics.example.com", DEFAULT_PORT); let udp_sink = BufferedUdpMetricSink::from(host, socket).unwrap(); let async_sink = AsyncMetricSink::with_threadpool( udp_sink, ThreadPool::new(1));
Trait Implementations
impl<T: Debug + 'static + MetricSink + Send + Sync> Debug for AsyncMetricSink<T>
[src]
impl<T: Clone + 'static + MetricSink + Send + Sync> Clone for AsyncMetricSink<T>
[src]
fn clone(&self) -> AsyncMetricSink<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more