Code Monkey home page Code Monkey logo

dogstatsd-rs's Introduction

dogstatsd-rs

Build Status Crate Version

A Rust client for interacting with Dogstatsd

Dogstatsd is a custom StatsD implementation by DataDog for sending metrics and events to their system. Through this client you can report any type of metric you want, tag it, and enjoy your custom metrics.

Full Documentation

Usage

Build an options struct and create a client:

use dogstatsd::{Client, Options};

// Binds to a udp socket on an available ephemeral port on 127.0.0.1 for
// transmitting, and sends to 127.0.0.1:8125, the default dogstatsd address.
let default_options = Options::default();
let default_client = Client::new(default_options).unwrap();

// Binds to 127.0.0.1:9000 for transmitting and sends to 10.1.2.3:8125, with a
// namespace of "analytics".
let custom_options = Options::new("127.0.0.1:9000", "10.1.2.3:8125", "analytics", vec!(String::new()));
let custom_client = Client::new(custom_options).unwrap();

// You can also use the OptionsBuilder API to avoid needing to specify every option.
let built_options = OptionsBuilder::new().from_addr(String::from("127.0.0.1:9001")).build();
let built_client = Client::new(built_options).unwrap();

Start sending metrics:

use dogstatsd::{Client, Options, ServiceCheckOptions, ServiceStatus};

let client = Client::new(Options::default()).unwrap();
let tags = &["env:production"];

// Increment a counter
client.incr("my_counter", tags).unwrap();

// Decrement a counter
client.decr("my_counter", tags).unwrap();

// Time a block of code (reports in ms)
client.time("my_time", tags, || {
    // Some time consuming code
}).unwrap();

// Report your own timing in ms
client.timing("my_timing", 500, tags).unwrap();

// Report an arbitrary value (a gauge)
client.gauge("my_gauge", "12345", tags).unwrap();

// Report a sample of a histogram
client.histogram("my_histogram", "67890", tags).unwrap();

// Report a sample of a distribution
client.distribution("distribution", "67890", tags).unwrap();

// Report a member of a set
client.set("my_set", "13579", tags).unwrap();

// Report a service check
let service_check_options = ServiceCheckOptions {
  hostname: Some("my-host.localhost"),
  ..Default::default()
};
client.service_check("redis.can_connect", ServiceStatus::OK, tags, Some(service_check_options)).unwrap();

// Send a custom event
client.event("My Custom Event Title", "My Custom Event Body", tags).unwrap();

Benchmarks

Support is provided for running benchmarks of all client commands. Until the Bencher type is stable Rust, the benchmarks are isolated behind the unstable feature flag. To run the benchmarks using rustup:

rustup run nightly cargo bench --features=unstable

dogstatsd-rs's People

Contributors

bgerstle avatar brent-statsig avatar cottinisimone avatar dwdking avatar ealui-statsig avatar guara92 avatar insanitybit avatar mcasper avatar mjkillough avatar nastevens avatar nwtnni avatar superfell avatar wayt 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

Watchers

 avatar  avatar  avatar

dogstatsd-rs's Issues

Convert public API to builder pattern

Trying to provide a clean API that has lots of optional fields is pretty verbose right now, as was made clear by service checks, not to mention all the options that events support that we should add. My thought is to transition the API to more of a builder pattern, so that you never have to specify the optional arguments that you don't care about:

client.service_check("redis.can_connect", ServiceStatus::OK)
    .hostname("my-host.localhost")
    .timestamp(1510327616)
    .send()

or

client.gauge("my_gauge", "12345")
    .tag("special:tag")
    .send()

Add metric batching

Might be nice to offer a batch block, so that multiple metrics that are sent together frequently can be batched into one packet

Support for async function in `time` function

Would be cool to be able to track duration of async functions too in time function. What i imagine is something like:

  use dogstatsd::{Client, Options};
  use std::thread;
  use std::time::Duration;

  let client = Client::new(Options::default()).unwrap();
  client
    .time("timer", &["tag:time"], || async {
      thread::sleep(Duration::from_millis(200))
    })
    .await
    .unwrap_or_else(|e| println!("Encountered error: {}", e))

Thank you in advance

DataDog Configuration not documented

Hi all, maybe a dumb question, but let's ask anyway :D

We've got some Rust Lambda's running and we want to add custom metrics, this library looks great, but I'm wondering what the approach if for configuring DataDog (API key etc).

Is there any example for that?

`Client` `timing` function should accept `u128` instead of `i64`

Since a duration of an execution is supposed to be greater than 0 i think that it might be better, in timing function (and in TimingMetric struct too), to use an u128 instead of i64.

Moreover i'm not sure about this comment (Send your own timing metric in milliseconds). Maybe i'm wrong, but in datadog documentation is written that duration metrics are reported as microseconds.

Thank you in advance and for the library :). We use it really a lot!

Documentation is out of date

The examples in the readme don't work in version 0.6.1.
If I create a new project and copy the example code:

use dogstatsd::{Client, Options};

fn main() {
    let client = Client::new(Options::default());

    // Increment a counter
    client.incr("my_counter", vec![]).unwrap();
}

I get a compiler error:

Compiling rust-test v0.1.0 (/Users/duncanuszkay/src/github.com/Shopify/rust-test)
error[E0599]: no method named `incr` found for type `std::result::Result<dogstatsd::Client, dogstatsd::error::DogstatsdError>` in the current scope
 --> src/main.rs:7:12
  |
7 |     client.incr("my_counter", vec![]).unwrap();
  |            ^^^^

error: aborting due to previous error

It seems that this is due to the fact that the Client::new method returns a result in the newer code.

Additionally the link to full documentation is out of date:
Screen Shot 2019-06-20 at 10 11 12 AM

async/await

I'm working on an async networking service that will need to emit metrics, soon. After looking at the existing crates available, dogstatsd is one of my top choices. Unfortunately for me, none of the existing crates support async/await. But fortunately for you, I will be able to dedicate some company time to implement this.

Before I begin any work or submit any PRs, I'd like to start a conversation to work out the details. Has there been any brainstorming on what the API might look like, or are there any immediate plans to begin work on async/await? Would you be happy with a feature gate to transform the public API into an async interface, or would you prefer a separate collection of structs? Or maybe even a separate crate altogether?

The code as-is seems like it would lend itself pretty nicely to the former; a feature gate. One challenge I can spot right away is that the Client struct assumes it will be responsible for creating the socket (tightly coupling it with the socket impl in std). This is easy to change to a type that is generic over anything that implements Read + Write (or AsyncRead + AsyncWrite).

This work will probably fall on my plate in early January. Let me know if you are interested in collaborating, and what (if anything) may have already been done.

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.