Struct cadence::StatsdClient

source ·
pub struct StatsdClient { /* private fields */ }
Expand description

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.
  • Distributed for emitting distribution values.
  • Setted for emitting set 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). An example of how to use the client in a multithreaded environment is given below.

In the following example, 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

In order to share a client between multiple threads, you’ll need to wrap it with an atomic reference counting pointer (std::sync::Arc). You should 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::panic::RefUnwindSafe;
use std::net::UdpSocket;
use std::sync::Arc;
use std::thread;
use cadence::prelude::*;
use cadence::{StatsdClient, BufferedUdpMetricSink, DEFAULT_PORT};

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

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.count("request.handler", 1);
        });

        Ok(())
    }
}

Implementations§

source§

impl StatsdClient

source

pub fn from_sink<T>(prefix: &str, sink: T) -> Selfwhere T: MetricSink + Sync + Send + RefUnwindSafe + 'static,

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

Note that this client will discard errors encountered when sending metrics via the MetricBuilder::send() method.

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);
Examples found in repository?
examples/arc-wrapped-client.rs (line 30)
28
29
30
31
32
    fn new() -> ThreadedHandler {
        ThreadedHandler {
            metrics: Arc::new(StatsdClient::from_sink("example.prefix", NopMetricSink)),
        }
    }
More examples
Hide additional examples
examples/custom-value-type.rs (line 37)
35
36
37
38
39
40
41
42
fn main() {
    let sink = NopMetricSink;
    let metrics = StatsdClient::from_sink("example.prefix", sink);

    metrics.gauge("user.happiness", UserHappiness::VeryHappy).unwrap();
    metrics.gauge("user.happiness", UserHappiness::KindaHappy).unwrap();
    metrics.gauge("user.happiness", UserHappiness::Sad).unwrap();
}
examples/nop-sink.rs (line 20)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    let sink = NopMetricSink;
    let metrics = StatsdClient::from_sink("example.prefix", sink);

    metrics.count("example.counter", 1).unwrap();
    metrics.gauge("example.gauge", 5).unwrap();
    metrics.gauge("example.gauge", 5.0).unwrap();
    metrics.time("example.timer", 32).unwrap();
    metrics.time("example.timer", Duration::from_millis(32)).unwrap();
    metrics.histogram("example.histogram", 22).unwrap();
    metrics
        .histogram("example.histogram", Duration::from_nanos(22))
        .unwrap();
    metrics.histogram("example.histogram", 22.0).unwrap();
    metrics.distribution("example.distribution", 33).unwrap();
    metrics.distribution("example.distribution", 33.0).unwrap();
    metrics.meter("example.meter", 8).unwrap();
    metrics.set("example.set", 44).unwrap();
}
examples/simple-sink.rs (line 22)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn main() {
    let sock = UdpSocket::bind("0.0.0.0:0").unwrap();
    let sink = UdpMetricSink::from(("localhost", DEFAULT_PORT), sock).unwrap();
    let metrics = StatsdClient::from_sink("example.prefix", sink);

    metrics.count("example.counter", 1).unwrap();
    metrics.gauge("example.gauge", 5).unwrap();
    metrics.gauge("example.gauge", 5.0).unwrap();
    metrics.time("example.timer", 32).unwrap();
    metrics.time("example.timer", Duration::from_millis(32)).unwrap();
    metrics.histogram("example.histogram", 22).unwrap();
    metrics
        .histogram("example.histogram", Duration::from_nanos(22))
        .unwrap();
    metrics.histogram("example.histogram", 22.0).unwrap();
    metrics.distribution("example.distribution", 33).unwrap();
    metrics.distribution("example.distribution", 33.0).unwrap();
    metrics.meter("example.meter", 8).unwrap();
    metrics.set("example.set", 44).unwrap();
}
examples/production-sink.rs (line 24)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
fn main() {
    let sock = UdpSocket::bind("0.0.0.0:0").unwrap();
    let buffered = BufferedUdpMetricSink::from(("localhost", DEFAULT_PORT), sock).unwrap();
    let queued = QueuingMetricSink::from(buffered);
    let metrics = StatsdClient::from_sink("example.prefix", queued);

    metrics.count("example.counter", 1).unwrap();
    metrics.gauge("example.gauge", 5).unwrap();
    metrics.gauge("example.gauge", 5.0).unwrap();
    metrics.time("example.timer", 32).unwrap();
    metrics.time("example.timer", Duration::from_millis(32)).unwrap();
    metrics.histogram("example.histogram", 22).unwrap();
    metrics
        .histogram("example.histogram", Duration::from_nanos(22))
        .unwrap();
    metrics.histogram("example.histogram", 22.0).unwrap();
    metrics.distribution("example.distribution", 33).unwrap();
    metrics.distribution("example.distribution", 33.0).unwrap();
    metrics.meter("example.meter", 8).unwrap();
    metrics.set("example.set", 44).unwrap();
}
examples/spy-sink.rs (line 25)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn main() {
    // Ensure that the sink is dropped, forcing a flush of all buffered metrics.
    let rx = {
        // Use a buffer size larger than any metrics here so we can demonstrate that
        // each metric ends up with a newline (\n) after it.
        let (rx, sink) = BufferedSpyMetricSink::with_capacity(None, Some(64));
        let metrics = StatsdClient::from_sink("example.prefix", sink);

        metrics.count("example.counter", 1).unwrap();
        metrics.gauge("example.gauge", 5).unwrap();
        metrics.gauge("example.gauge", 5.0).unwrap();
        metrics.time("example.timer", 32).unwrap();
        metrics.time("example.timer", Duration::from_millis(32)).unwrap();
        metrics.histogram("example.histogram", 22).unwrap();
        metrics
            .histogram("example.histogram", Duration::from_nanos(22))
            .unwrap();
        metrics.histogram("example.histogram", 22.0).unwrap();
        metrics.distribution("example.distribution", 33).unwrap();
        metrics.distribution("example.distribution", 33.0).unwrap();
        metrics.meter("example.meter", 8).unwrap();
        metrics.set("example.set", 44).unwrap();

        rx
    };

    let mut buffer = Vec::new();
    while let Ok(v) = rx.try_recv() {
        buffer.extend(v);
    }

    println!("Contents of wrapped buffer:\n{}", String::from_utf8(buffer).unwrap());
}
source

pub fn from_udp_host<A>(prefix: &str, host: A) -> MetricResult<Self>where A: ToSocketAddrs,

👎Deprecated since 0.19.0: Superseded by ::from_sink() and ::builder()

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.

Note that this client will discard errors encountered when sending metrics via the MetricBuilder::send() method.

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.
source

pub fn builder<T>(prefix: &str, sink: T) -> StatsdClientBuilderwhere T: MetricSink + Sync + Send + RefUnwindSafe + 'static,

Create a new builder with the provided prefix and metric sink.

A prefix and a metric sink are required to create a new client instance. All other optional customizations can be set by calling methods on the returned builder. Any customizations that aren’t set by the caller will use defaults.

Note, though a metric prefix is required, you may pass an empty string as a prefix. In this case, the metrics emitted will use only the bare keys supplied when you call the various methods to emit metrics.

General defaults:

  • A no-op error handler will be used by default. Note that this only affects errors encountered when using the MetricBuilder::send() method (as opposed to .try_send() or any other method for sending metrics).
Example
use cadence::prelude::*;
use cadence::{StatsdClient, MetricError, NopMetricSink};

fn my_handler(err: MetricError) {
    println!("Metric error: {}", err);
}

let client = StatsdClient::builder("some.prefix", NopMetricSink)
    .with_error_handler(my_handler)
    .build();

client.gauge_with_tags("some.key", 7)
   .with_tag("region", "us-west-1")
   .send();
Examples found in repository?
examples/sending-tags.rs (line 22)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fn main() {
    fn my_error_handler(err: MetricError) {
        eprintln!("Error sending metrics: {}", err);
    }

    let client = StatsdClient::builder("my.prefix", NopMetricSink)
        .with_error_handler(my_error_handler)
        .build();

    // In this case we are sending a counter metric with two tag key-value
    // pairs. If sending the metric fails, our error handler set above will
    // be invoked to do something with the metric error.
    client
        .count_with_tags("requests.handled", 1)
        .with_tag("app", "search")
        .with_tag("region", "us-west-2")
        .send();

    // In this case we are sending the same counter metrics with two tags.
    // The results of sending the metric (or failing to send it) are returned
    // to the caller to do something with.
    let res = client
        .count_with_tags("requests.handled", 1)
        .with_tag("app", "search")
        .with_tag("region", "us-west-2")
        .try_send();

    println!("Result of metric send: {:?}", res);
}
More examples
Hide additional examples
examples/value-packing.rs (line 22)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fn main() {
    fn my_error_handler(err: MetricError) {
        eprintln!("Error sending metrics: {}", err);
    }

    let client = StatsdClient::builder("my.prefix", NopMetricSink)
        .with_error_handler(my_error_handler)
        .build();

    // In this case we are sending a distribution metric with two tag key-value
    // pairs. If sending the metric fails, our error handler set above will
    // be invoked to do something with the metric error.
    client
        .distribution_with_tags("latency.milliseconds", vec![10, 20, 30, 40, 50])
        .with_tag("app", "search")
        .with_tag("region", "us-west-2")
        .send();

    // In this case we are sending the same distribution metrics with two tags.
    // The results of sending the metric (or failing to send it) are returned
    // to the caller to do something with.
    let res = client
        .distribution_with_tags("latency.milliseconds", vec![10, 20, 30, 40, 50])
        .with_tag("app", "search")
        .with_tag("region", "us-west-2")
        .try_send();

    println!("Result of metric send: {:?}", res);
}

Trait Implementations§

source§

impl Compat for StatsdClient

source§

fn time_duration_with_tags<'a>( &'a self, key: &'a str, val: Duration ) -> MetricBuilder<'_, '_, Timer>

👎Deprecated: Use client.time_with_tags(key, val)
source§

fn gauge_f64_with_tags<'a>( &'a self, key: &'a str, val: f64 ) -> MetricBuilder<'_, '_, Gauge>

👎Deprecated: Use client.gauge_with_tags(key, val)
source§

fn mark_with_tags<'a>(&'a self, key: &'a str) -> MetricBuilder<'_, '_, Meter>

👎Deprecated: Use client.meter_with_tags(key, 1)
source§

fn histogram_duration_with_tags<'a>( &'a self, key: &'a str, val: Duration ) -> MetricBuilder<'_, '_, Histogram>

👎Deprecated: Use client.histogram_with_tags(key, val)
source§

fn time_duration(&self, key: &str, val: Duration) -> MetricResult<Timer>

👎Deprecated: Use client.time(key, val)
source§

fn gauge_f64(&self, key: &str, val: f64) -> MetricResult<Gauge>

👎Deprecated: Use client.gauge(key, val)
source§

fn mark(&self, key: &str) -> MetricResult<Meter>

👎Deprecated: Use client.meter(key, 1)
source§

fn histogram_duration( &self, key: &str, val: Duration ) -> MetricResult<Histogram>

👎Deprecated: Use client.histogram(key, val)
source§

impl<T> Counted<T> for StatsdClientwhere T: ToCounterValue,

source§

fn count_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Counter>

Increment or decrement the counter by the given amount and return a MetricBuilder that can be used to add tags to the metric.
source§

fn count(&self, key: &str, count: T) -> MetricResult<Counter>

Increment or decrement the counter by the given amount
source§

impl CountedExt for StatsdClient

source§

fn incr(&self, key: &str) -> MetricResult<Counter>

Increment the counter by 1
source§

fn incr_with_tags<'a>(&'a self, key: &'a str) -> MetricBuilder<'_, '_, Counter>

Increment the counter by 1 and return a MetricBuilder that can be used to add tags to the metric.
source§

fn decr(&self, key: &str) -> MetricResult<Counter>

Decrement the counter by 1
source§

fn decr_with_tags<'a>(&'a self, key: &'a str) -> MetricBuilder<'_, '_, Counter>

Decrement the counter by 1 and return a MetricBuilder that can be used to add tags to the metric.
source§

impl Debug for StatsdClient

source§

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

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

impl<T> Distributed<T> for StatsdClientwhere T: ToDistributionValue,

source§

fn distribution_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Distribution>

Record a single distribution value with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn distribution(&self, key: &str, value: T) -> MetricResult<Distribution>

Record a single distribution value with the given key
source§

impl<T> Gauged<T> for StatsdClientwhere T: ToGaugeValue,

source§

fn gauge_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Gauge>

Record a gauge value with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn gauge(&self, key: &str, value: T) -> MetricResult<Gauge>

Record a gauge value with the given key
source§

impl<T> Histogrammed<T> for StatsdClientwhere T: ToHistogramValue,

source§

fn histogram_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Histogram>

Record a single histogram value with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn histogram(&self, key: &str, value: T) -> MetricResult<Histogram>

Record a single histogram value with the given key
source§

impl<T> Metered<T> for StatsdClientwhere T: ToMeterValue,

source§

fn meter_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Meter>

Record a meter value with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn meter(&self, key: &str, value: T) -> MetricResult<Meter>

Record a meter value with the given key
source§

impl MetricBackend for StatsdClient

source§

fn send_metric<M>(&self, metric: &M) -> MetricResult<()>where M: Metric,

Send a full formed Metric implementation via the underlying MetricSink Read more
source§

fn consume_error(&self, err: MetricError)

Consume a possible error from attempting to send a metric. Read more
source§

impl<T> Setted<T> for StatsdClientwhere T: ToSetValue,

source§

fn set_with_tags<'a>( &'a self, key: &'a str, value: T ) -> MetricBuilder<'_, '_, Set>

Record a single set value with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn set(&self, key: &str, value: T) -> MetricResult<Set>

Record a single set value with the given key
source§

impl<T> Timed<T> for StatsdClientwhere T: ToTimerValue,

source§

fn time_with_tags<'a>( &'a self, key: &'a str, time: T ) -> MetricBuilder<'_, '_, Timer>

Record a timing in milliseconds with the given key and return a MetricBuilder that can be used to add tags to the metric.
source§

fn time(&self, key: &str, time: T) -> MetricResult<Timer>

Record a timing in milliseconds with the given key
source§

impl MetricClient for StatsdClient

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.