Expand description

This is an opinionated library to share code and approach to Datadog logging in prima.it

Getting started

You need to call Datadog::init in your main binary, and to do so you’ll need as argument a type that implements the Configuration trait.

Inside the configuration you’ll find an implementation of this trait tailored for prima.it needs.

use prima_datadog::{*, configuration::PrimaConfiguration};

// initializes the PrimaConfiguration struct
let configuration = PrimaConfiguration::new(
    "0.0.0.0:1234", // to address
    "0.0.0.0:0", // from address
    "service_name", // namespace for all metrics
    "production".parse().unwrap() // environment
);

// Initializes a Datadog instance
Datadog::init(configuration);

Then you can use the macros exposed at the base level of the module. All macros accepts

  • a string value or a path to a type that implements AsRef as first argument.
  • zero or more arguments, separated by comma ,, for the metrics that needs more data. For example count! and timing! accepts a number while service_check! accepts a ServiceStatus and a ServiceCheckOptions
  • a list of tags (which is separated from the rest of the arguments by semicolon ;) in the form of "name" => "value"
incr!("test");
decr!("test"; "some" => "data");
count!("test", 20);
count!("test", 10; "some" => "data");
time!("test", || { println!("expensive computation");});
time!("test", || { println!("expensive computation");}; "some" => "data");
timing!("test", 20; "some" => "data");
gauge!("test", "gauge value"; "some" => "data");
histogram!("test", "histogram value"; "some" => "data");
distribution!("test", "distribution value"; "some" => "data");
set!("test", "set value"; "some" => "data");
service_check!("test", ServiceStatus::OK);
service_check!("test", ServiceStatus::OK, ServiceCheckOptions::default());
event!("test", "test event"; "some" => "data");

This is an example of a custom metric, in this case based on an enum type, but it can really be whatever you want, as long as it implements AsRef

enum Metric {
    John,
    Paul,
    George,
    Ringo,
}

impl AsRef<str> for Metric {
    fn as_ref(&self) -> &str {
        match self {
            Metric::John => "john",
            Metric::Paul => "paul",
            Metric::George => "george",
            Metric::Ringo => "ringo",
        }
    }
}

// now you can do
incr!(Metric::John; "play" => "guitar");
incr!(Metric::Paul; "play" => "bass");
incr!(Metric::George; "play" => "sitar");
incr!(Metric::Ringo; "play" => "drums");

References

Modules

Configuration module

Error module for this crate

Macros

Make an arbitrary change to a StatsD counter

Decrement a StatsD counter

Report a value in a distribution

Send a custom event as a title and a body

Report an arbitrary value as a gauge

Report a value in a histogram

Increment a StatsD counter

Report the status of a service

Report a value in a set

Time a block of code (reports in ms)

Send your own timing metric in milliseconds

Structs

The Datadog struct is the main entry point for the library

Struct for adding optional pieces to a service check

Enums

Represents the different states a service can be in

Traits

This trait represent a client that is able to interact with the datadog statsd collector. Its main use in this library is having a common interface for the underlying implementation, and being able to mock it for testing purposes