Cadence
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
MetricSinktrait. - 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.
[]
= "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 *;
use ;
// 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 = ;
let client = from_udp_host.unwrap;
// Emit metrics!
client.incr;
client.time;
client.gauge;
client.meter;
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 *;
use ;
// 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.
// Create a new Statsd client that writes to "metrics.example.com"
let host = ;
let counter = from_udp_host.unwrap;
// Create a new instance of the DAO that will use the client
let dao = new;
// Try to lookup a user by ID!
match dao.get_user_by_id ;
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 io;
use *;
use ;
;
let sink = MyMetricSink;
let client = from_sink;
client.count;
client.time;
client.incr;
Custom UDP Socket
Most users of the Cadence StatsdClient will be using it to send metrics over
a UDP socket. If you need to customize the socket, for example you want to make
sure it won't block, you can do that as demonstrated below.
use UdpSocket;
use *;
use ;
let socket = bind.unwrap;
socket.set_nonblocking.unwrap;
let host = ;
let sink = from.unwrap;
let client = from_sink;
client.count;
client.time;
client.incr;
Documentation
The documentation is available at https://tshlabs.github.io/cadence/
Source
The source code is available on GitHub at https://github.com/tshlabs/cadence
Changes
Release notes for Cadence can be found in the CHANGES.md file.
Development
Cadence uses Cargo for performing various development tasks.
To build Cadence:
$ cargo build
To run tests:
$ cargo test
or:
$ cargo test -- --ignored
To run benchmarks:
$ cargo bench
To build documentation:
$ cargo doc