Struct cadence::StatsdClient [] [src]

pub struct StatsdClient { /* fields omitted */ }

Client for Statsd that implements various traits to record metrics.

Traits

The client is the main entry point for users of this library. It supports several traits for recording metrics of different types.

  • Counted for emitting counters.
  • Timed for emitting timings.
  • Gauged for emitting gauge values.
  • Metered for emitting meter values.
  • Histogrammed for emitting histogram values.
  • MetricClient for a combination of all of the above.

For more information about the uses for each type of metric, see the documentation for each mentioned trait.

Sinks

The client uses some implementation of a MetricSink to emit the metrics.

In simple use cases when performance isn't critical, the UdpMetricSink is an acceptable choice since it is the simplest to use and understand.

When performance is more important, users will want to use the BufferedUdpMetricSink in combination with the QueuingMetricSink for maximum isolation between the sending of metrics and your application as well as minimum overhead when sending metrics.

Threading

The StatsdClient is designed to work in a multithreaded application. All parts of the client can be shared between threads (i.e. it is Send and Sync). Some common ways to use the client in a multithreaded environment are given below.

In each of these examples, we create a struct MyRequestHandler that has a single method that spawns a thread to do some work and emit a metric.

Wrapping With An Arc

One option is to put all accesses to the client behind an atomic reference counting pointer (std::sync::Arc). If you are doing this, it makes sense to just refer to the client by the trait of all its methods for recording metrics (MetricClient) as well as the Send and Sync traits since the idea is to share this between threads.

use std::net::UdpSocket;
use std::sync::Arc;
use std::thread;
use cadence::prelude::*;
use cadence::{StatsdClient, BufferedUdpMetricSink, DEFAULT_PORT};

struct MyRequestHandler {
    metrics: Arc<MetricClient + Send + Sync>,
}

impl MyRequestHandler {
    fn new() -> MyRequestHandler {
        let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
        let host = ("localhost", DEFAULT_PORT);
        let sink = BufferedUdpMetricSink::from(host, socket).unwrap();
        MyRequestHandler {
            metrics: Arc::new(StatsdClient::from_sink("some.prefix", sink))
        }
    }

    fn handle_some_request(&self) -> Result<(), String> {
        let metric_ref = self.metrics.clone();
        let _t = thread::spawn(move || {
            println!("Hello from the thread!");
            metric_ref.incr("request.handler");
        });

        Ok(())
    }
}

Clone Per Thread

Another option for sharing the client between threads is just to clone client itself. Clones of the client are relatively cheap, typically only requiring a single heap allocation (of a String). While this cost isn't nothing, it's not too bad. An example of this is given below.

use std::net::UdpSocket;
use std::thread;
use cadence::prelude::*;
use cadence::{StatsdClient, BufferedUdpMetricSink, DEFAULT_PORT};

struct MyRequestHandler {
    metrics: StatsdClient,
}

impl MyRequestHandler {
    fn new() -> MyRequestHandler {
        let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
        let host = ("localhost", DEFAULT_PORT);
        let sink = BufferedUdpMetricSink::from(host, socket).unwrap();
        MyRequestHandler {
            metrics: StatsdClient::from_sink("some.prefix", sink)
        }
    }

    fn handle_some_request(&self) -> Result<(), String> {
        let metric_clone = self.metrics.clone();
        let _t = thread::spawn(move || {
            println!("Hello from the thread!");
            metric_clone.incr("request.handler");
        });

        Ok(())
    }
}

As you can see, cloning the client itself looks a lot like using it with an Arc.

Methods

impl StatsdClient
[src]

Create a new client instance that will use the given prefix for all metrics emitted to the given MetricSink implementation.

No-op Example

use cadence::{StatsdClient, NopMetricSink};

let prefix = "my.stats";
let client = StatsdClient::from_sink(prefix, NopMetricSink);

UDP Socket Example

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

let prefix = "my.stats";
let host = ("127.0.0.1", DEFAULT_PORT);

let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
socket.set_nonblocking(true).unwrap();

let sink = UdpMetricSink::from(host, socket).unwrap();
let client = StatsdClient::from_sink(prefix, sink);

Buffered UDP Socket Example

use std::net::UdpSocket;
use cadence::{StatsdClient, BufferedUdpMetricSink, DEFAULT_PORT};

let prefix = "my.stats";
let host = ("127.0.0.1", DEFAULT_PORT);

let socket = UdpSocket::bind("0.0.0.0:0").unwrap();

let sink = BufferedUdpMetricSink::from(host, socket).unwrap();
let client = StatsdClient::from_sink(prefix, sink);

Create a new client instance that will use the given prefix to send metrics to the given host over UDP using an appropriate sink.

The created UDP socket will be put into non-blocking mode.

Example

use cadence::{StatsdClient, UdpMetricSink};

let prefix = "my.stats";
let host = ("metrics.example.com", 8125);

let client = StatsdClient::from_udp_host(prefix, host);

Failures

This method may fail if:

  • It is unable to create a local UDP socket.
  • It is unable to put the UDP socket into non-blocking mode.
  • It is unable to resolve the hostname of the metric server.
  • The host address is otherwise unable to be parsed.

Trait Implementations

impl Clone for StatsdClient
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for StatsdClient
[src]

Formats the value using the given formatter.

impl Counted for StatsdClient
[src]

Increment the counter by 1

Decrement the counter by 1

Increment or decrement the counter by the given amount

impl Timed for StatsdClient
[src]

Record a timing in milliseconds with the given key

Record a timing in milliseocnds with the given key Read more

impl Gauged for StatsdClient
[src]

Record a gauge value with the given key

impl Metered for StatsdClient
[src]

Record a single metered event with the given key

Record a meter value with the given key

impl Histogrammed for StatsdClient
[src]

Record a single histogram value with the given key

impl MetricClient for StatsdClient
[src]