3 releases

0.1.2 Mar 13, 2024
0.1.1 Apr 3, 2021
0.1.0 Apr 1, 2021

#86 in Profiling

Download history 309/week @ 2024-01-25 291/week @ 2024-02-01 443/week @ 2024-02-08 276/week @ 2024-02-15 572/week @ 2024-02-22 326/week @ 2024-02-29 616/week @ 2024-03-07 621/week @ 2024-03-14 408/week @ 2024-03-21 1105/week @ 2024-03-28 1558/week @ 2024-04-04 1904/week @ 2024-04-11 788/week @ 2024-04-18 821/week @ 2024-04-25 539/week @ 2024-05-02 360/week @ 2024-05-09

3,449 downloads per month

MIT license

30KB
513 lines

Rust DogStatsd

CI Latest version

A DogStatsD client implementation of statsd in rust.

Using the client library

Add the datadog-statsd package as a dependency in your Cargo.toml file:

[dependencies]
datadog-statsd = "0.1.2"

You need rustc >= 1.31.0 for statsd to work.

You can then get a client instance and start tracking metrics:

// Load the crate
extern crate datadog_statsd;

// Import the client object.
use datadog_statsd::Client;

// Get a client with the prefix of `myapp`. The host should be the
// IP:port of your statsd daemon.
let client = Client::new("127.0.0.1:8125", "myapp", Some(vec!["common1", "common2:test"]),).unwrap();

Tracking Metrics

Once you've created a client, you can track timers and metrics:

let tags = &Some(vec!["tag1", "tag2:test"]);

// Increment a counter by 1
client.incr("some.counter", tags);

// Decrement a counter by 1
client.decr("some.counter", tags);

// Update a gauge
client.gauge("some.value", 12.0, tags);

// Modify a counter by an arbitrary float.
client.count("some.counter", 511.0, tags);

// Send a histogram value as a float.
client.histogram("some.histogram", 511.0, tags);

Tracking Timers

Timers can be updated using timer() and time():

// Update a timer based on a calculation you've done.
client.timer("operation.duration", 13.4, tags);

// Time a closure
client.time("operation.duration", tags, || {
	// Do something expensive.
});

Events & ServiceChecks

// Send a datadog event.
client.event("event title", "event text", AlertType::Warning, tags);

// Send a datadog service check.
client.service_check(
    "myapp.service.check.name",
    ServiceCheckStatus::Critical,
    tags,
);

Pipeline

Multiple metrics can be sent to StatsD once using pipeline:

let mut pipe = client.pipeline():

// Increment a counter by 1
pipe.incr("some.counter");

// Decrement a counter by 1
pipe.decr("some.counter");

// Update a gauge
pipe.gauge("some.value", 12.0);

// Modify a counter by an arbitrary float.
pipe.count("some.counter", 511.0);

// Send a histogram value as a float.
pipe.histogram("some.histogram", 511.0);

// Set max UDP packet size if you wish, default is 512
pipe.set_max_udp_size(128);

// Send to StatsD
pipe.send(&client);

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.

Dependencies

~315–540KB