Rust Statsd
A StatsD client implementation of statsd in rust.
Using the client library
Add the statsd
package as a dependency in your Cargo.toml
file:
[]
= "^0.13.1"
You need rustc >= 1.8.0 for statsd to work.
You can then get a client instance and start tracking metrics:
// Load the crate
extern crate statsd;
// Import the client object.
use Client;
// Get a client with the prefix of `myapp`. The host should be the
// IP:port of your statsd daemon.
let client = new.unwrap;
Tracking Metrics
Once you've created a client, you can track timers and metrics:
// Increment a counter by 1
client.incr;
// Decrement a counter by 1
client.decr;
// Update a gauge
client.gauge;
// Modify a counter by an arbitrary float.
client.count;
// Send a histogram value as a float.
client.histogram;
Tracking Timers
Timers can be updated using timer()
and time()
:
// Update a timer based on a calculation you've done.
client.timer;
// Time a closure
client.time;
Pipeline
Multiple metrics can be sent to StatsD once using pipeline:
let mut pipe = client.pipeline:
// Increment a counter by 1
pipe.incr;
// Decrement a counter by 1
pipe.decr;
// Update a gauge
pipe.gauge;
// Modify a counter by an arbitrary float.
pipe.count;
// Send a histogram value as a float.
pipe.histogram;
// Set max UDP packet size if you wish, default is 512
pipe.set_max_udp_size;
// Send to StatsD
pipe.send;
Pipelines are also helpful to make functions simpler to test, as you can pass a pipeline and be confident that no UDP packets will be sent.
License
Licenesed under the MIT License.