Giter VIP home page Giter VIP logo

rust-statsd's Introduction

Rust Statsd

Build Status

A StatsD client implementation of statsd in rust.

Using the client library

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

[dependencies]
statsd = "^0.16"

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 statsd;

// Import the client object.
use 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").unwrap();

Tracking Metrics

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

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

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

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

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

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

// Send a key/value.
client.kv("some.data", 15.26);

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);

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

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);

// Send a key/value.
pipe.kv("some.data", 15.26);

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

rust-statsd's People

Contributors

3pointer avatar alexeyvavilin avatar amirjang avatar cholcombe973 avatar diamondlord avatar frewsxcv avatar jnth avatar lilydjwg avatar markstory avatar messense avatar passy avatar quentinlesceller avatar roguelazer avatar urjitbhatia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rust-statsd's Issues

This library doesn't support IPv6

IPv6 doesn't work because the UdpSocket is always bound to IPv4. send_to then always fails, but the error is silently ignored.

Took me quite a while to find this ...

This could be fixed by checking if the address is IPv6 or IPv4 and then binding to 0.0.0.0:0 for IPv4 and to [::]:0 for IPv6.

Metric tags

Hey there,

Thank you for writing this, awesome project!

Curious, should there be support for tagging metrics? I could imagine something like this:

client.incr("some.metric", vec!["service:api"])

or maybe even better:

let client = Client::new("127.0.0.1:8125", "project", vec!["service:api"]).unwrap()

and this would add the tag service with value api to all metrics.

Thanks!

Edit: I think I was confused; tags seem to be a Datadog extension of StatsD, and it's not part of the StatsD spec.

Usage help - how to perform mutable operations in `client.time` closure

Hi, I am pretty new to rust. Using this lib was super simple, thanks for the work!

I don't know how to do mutable calls in the client.time closure callable because it is set as Fn and not FnOnce or FnMut.

I get this error: cannot assign to data in a captured outer variable in an Fn closure

Here is a little example of what I am trying to do. Any guidance appreciated:

extern crate statsd;
use statsd::Client;

#[derive(Debug)]
pub struct Foo {
    num: u64,
}

fn main() {
    let mut foo_num = Foo{num: 10};
    println!("before: {:?}", foo_num);
    let client = Client::new("127.0.0.1:8125", "foo").unwrap();
    client.time("timethis", || {
        // Assume this is an expensive operation I want to time that takes ownership of a mutable ref
        foo_num.num+=1;
        println!("foo: {:?}", foo_num);
    });
}

prefix should not end with dot

The example from the doc says:

use statsd::client::Client;

let client = Client::new("127.0.0.1:8125", "myapp.");
client.incr("some.metric.completed");

This will generate a metric named myapp..some.metric.completed. The extra dot will cause issues since Graphite treats this the same as myapp.some.metric.completed and stores data in the same file, but statsd thinks they are different.

Not compiling on rust 1.9

Compiling clock_ticks v0.0.6
/Users/marcusbuffett/.cargo/registry/src/github.com-88ac128001ac3a9a/clock_ticks-0.0.6/src/lib.rs:37:9: 37:75 error: a value named `mach_timebase_info` has already been imported in this module [E0255]
/Users/marcusbuffett/.cargo/registry/src/github.com-88ac128001ac3a9a/clock_ticks-0.0.6/src/lib.rs:37         pub fn mach_timebase_info(info: *mut mach_timebase_info) -> c_int;
                                                                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/marcusbuffett/.cargo/registry/src/github.com-88ac128001ac3a9a/clock_ticks-0.0.6/src/lib.rs:37:9: 37:75 help: run `rustc --explain E0255` to see a detailed explanation
/Users/marcusbuffett/.cargo/registry/src/github.com-88ac128001ac3a9a/clock_ticks-0.0.6/src/lib.rs:31:42: 31:60 note: previous import of `mach_timebase_info` here
/Users/marcusbuffett/.cargo/registry/src/github.com-88ac128001ac3a9a/clock_ticks-0.0.6/src/lib.rs:31     use libc::{timeval, timezone, c_int, mach_timebase_info};
                                                                                                                                              ^~~~~~~~~~~~~~~~~~
error: aborting due to previous error
error: Could not compile `clock_ticks`.

To learn more, run the command again with --verbose.

Can't return value from closure in `Client.time`

Client.time is declared as

fn time<F>(&mut self, metric: &str, callable: F) where F: Fn()

So it can't be used to run a function that returns a value. It'd be better to be:

fn time<F, R>(&mut self, metric: &str, callable: F) -> R where F: Fn() -> R

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.