Crates.io | tokio-cadence |
lib.rs | tokio-cadence |
version | 0.6.0 |
source | src |
created_at | 2020-10-29 02:08:35.377254 |
updated_at | 2024-11-20 02:20:41.87735 |
description | Tokio-based metric sinks for Cadence |
homepage | |
repository | https://github.com/ecliptical/tokio-cadence |
max_upload_size | |
id | 306537 |
size | 51,556 |
A collection of Cadence Metric Sink implementations that process metrics asynchronously using Tokio.
The Statsd client provided by Cadence does not support asynchronous operation -- submitting a metric may in fact block the caller!
This is undesirable in asynchronous contexts.
The Metric Sinks implemented in this crate alleviate this issue by allowing the application code to enqueue metrics without blocking, and offloading their actual sending to a separate asynchronous task.
Add cadence
, tokio
, and tokio-cadence
to your Cargo.toml
:
[dependencies]
cadence = "1"
tokio = { version = "1", features = ["full"] }
tokio-cadence = "0.5"
The Metric Sink constructors return a tuple consisting of the sink instance itself as well as a future, which the application must drive to completion; e.g., spawn it in a Tokio task pool.
use cadence::prelude::*;
use cadence::{StatsdClient, DEFAULT_PORT};
use tokio_cadence::TokioBatchUdpMetricSink;
use tokio::{spawn, net::UdpSocket};
#[tokio::main]
async fn main() -> cadence::MetricResult<()> {
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();
Ok(())
}
Note that in order to ensure that all buffered metrics are submitted, the application must await the completion of the processing job after the client, as well as the sink along with it, are dropped.
Licensed under the MIT license.