Crate cadence [] [src]

An extensible Statsd client for Rust!

Statsd is a network server that listens for metrics (things like counters and timers) sent over UDP and sends aggregates of these metrics to a backend service of some kind (often Graphite).

Cadence is a client written in Rust for interacting with a Statsd server. You might want to emit metrics (using Cadence, sending them to a Statsd server) in your Rust server application.

For example, if you are running a Rust web service you might want to record:

  • Number of succesful requests
  • Number of error requests
  • Time taken for each request

Cadence is a flexible and easy way to do this!

Features

  • Support for emitting counters, timers, gauges, and meters to Statsd over UDP.
  • Support for alternate backends via the MetricSink trait.
  • A simple yet flexible API for sending metrics.

Install

To make use of Cadence in your project, add it as a dependency in your Cargo.toml file.

[dependencies]
cadence = "x.y.z"

Then, link to it in your library or application.

// bin.rs or lib.rs
extern crate cadence;

// rest of your library or application

Usage

Some examples of how to use Cadence are shown below.

Simple Use

Simple usage of Cadence is shown below. In this example, we just import the client, create an instance that will write to some imaginary metrics server, and send a few metrics.

// Import the client.
use cadence::prelude::*;
use cadence::{StatsdClient, UdpMetricSink, DEFAULT_PORT};

fn main() {
    // Create client that will write to the given host over UDP.
    //
    // Note that you'll probably want to actually handle any errors creating the client
    // when you use it for real in your application. We're just using .unwrap() here
    // since this is an example!
    let host = ("metrics.example.com", DEFAULT_PORT);
    let client = StatsdClient::<UdpMetricSink>::from_udp_host(
        "my.metrics", host).unwrap();

    // Emit metrics!
    client.incr("some.counter");
    client.time("some.methodCall", 42);
    client.gauge("some.thing", 7);
    client.meter("some.value", 5);
}

Counted, Timed, Gauged, and Metered Traits

Each of the methods that the Cadence StatsdClient struct uses to send metrics are implemented as a trait. If we want, we can just use the trait type to refer to the client instance. This might be useful to you if you'd like to swap out the actual Cadence client with a dummy version when you are unit testing your code.

Each of these traits are exported in the prelude module. They are also available in the main module but aren't typically used like that.

use cadence::prelude::*;
use cadence::{StatsdClient, UdpMetricSink, DEFAULT_PORT};


pub struct User {
    id: u64,
    username: String,
    email: String
}


// Here's a simple DAO (Data Access Object) that doesn't do anything but
// uses a counter to keep track of the number of times the 'getUserById'
// method gets called.
pub struct MyUserDao<T: Counted> {
    counter: T
}


impl<T: Counted> MyUserDao<T> {
    // Create a new instance that will use the counter / client
    pub fn new(counter: T) -> MyUserDao<T> {
        MyUserDao{counter: counter}
    }

    /// Get a new user by their ID
    pub fn get_user_by_id(&self, id: u64) -> Option<User> {
        self.counter.incr("getUserById");
        None
    }
}


fn main() {
    // Create a new Statsd client that writes to "metrics.example.com"
    let host = ("metrics.example.com", DEFAULT_PORT);
    let counter = StatsdClient::<UdpMetricSink>::from_udp_host(
        "counter.example", host).unwrap();

    // Create a new instance of the DAO that will use the client
    let dao = MyUserDao::new(counter);

    // Try to lookup a user by ID!
    match dao.get_user_by_id(123) {
        Some(u) => println!("Found a user!"),
        None => println!("No user!")
    };
}

Custom Metric Sinks

The Cadence StatsdClient uses implementations of the MetricSink trait to send metrics to a metric server. Most users of the Candence library probably want to use the UdpMetricSink implementation. This is the way people typically interact with a Statsd server, sending packets over UDP.

However, maybe you'd like to do something custom: use a thread pool, send multiple metrics at the same time, or something else. An example of creating a custom sink is below.

use std::io;
use cadence::prelude::*;
use cadence::{StatsdClient, MetricSink, DEFAULT_PORT};

pub struct MyMetricSink;


impl MetricSink for MyMetricSink {
    fn emit(&self, metric: &str) -> io::Result<usize> {
        // Your custom metric sink implementation goes here!
        Ok(0)
    }
}


fn main() {
    let sink = MyMetricSink;
    let client = StatsdClient::from_sink("my.prefix", sink);

    client.count("my.counter.thing", 42);
    client.time("my.method.time", 25);
    client.incr("some.other.counter");
}

Modules

prelude

Export commonly used parts of Cadence for easy glob imports

Structs

ConsoleMetricSink

Implementation of a MetricSink that emits metrics to the console.

Counter

Counters are simple values incremented or decremented by a client.

Gauge

Gauges are an instantaneous value determined by the client.

LoggingMetricSink

Implementation of a MetricSink that emits metrics using thelog! macro.

Meter

Meters measure the rate at which events occur as determined by the server.

MetricError

Error generated by this library potentially wrapping another type of error (exposed via the Error trait).

NopMetricSink

Implementation of a MetricSink that discards all metrics.

StatsdClient

Client for Statsd that implements various traits to record metrics.

Timer

Timers are a positive number of milliseconds between a start and end point.

UdpMetricSink

Implementation of a MetricSink that emits metrics over UDP.

Enums

ErrorKind

Potential categories an error from this library falls into.

Constants

DEFAULT_PORT

Traits

Counted

Trait for incrementing and decrementing counters.

Gauged

Trait for recording gauge values.

Metered

Trait for recording meter values.

MetricSink

Trait for various backends that send Statsd metrics somewhere.

Timed

Trait for recording timings in milliseconds.

Type Definitions

MetricResult