#datadog #metrics #logging

prima_datadog

An opinionated library to share code and approach to Datadog logging in prima.it

20 unstable releases (7 breaking)

0.8.0 Jun 21, 2024
0.7.1 Mar 6, 2024
0.6.0 Oct 2, 2023
0.6.0-rc.0 Dec 18, 2023
0.1.4 Sep 27, 2021

#115 in Debugging

Download history 1068/week @ 2024-07-02 1139/week @ 2024-07-09 964/week @ 2024-07-16 1212/week @ 2024-07-23 1141/week @ 2024-07-30 1694/week @ 2024-08-06 954/week @ 2024-08-13 1674/week @ 2024-08-20 1448/week @ 2024-08-27 1511/week @ 2024-09-03 1567/week @ 2024-09-10 1265/week @ 2024-09-17 1977/week @ 2024-09-24 1979/week @ 2024-10-01 1704/week @ 2024-10-08 1733/week @ 2024-10-15

7,711 downloads per month

MIT license

105KB
2K SLoC

Prima Datadog

Build Status

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

Refer to the official docs for help on how to setup the library in your project

❕ Please note that prima_datadog.rs uses dogstatsd, which means metrics will be sent using the UDP protocol, so you'll need to specify a full address with both IP and port (the default one is 8125, but note that the library won't provide it for you). You can find more information on the official Datadog documentation. A full URL might then be 10.1.2.3:8125.


lib.rs:

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. If you never call Datadog::init in your binary NO metrics will be sent.

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

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

// initializes the Configuration struct
let configuration = Configuration::new(
    "0.0.0.0:1234", // to address
    "namespace", // namespace for all metrics
);

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

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


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

Note - Avoid high tag cardinality!

It's important to avoid passing a large number of values for a given tag, as Datadog tracks each unique combination of tag values as a separate metric, which can significantly impact billing. For example, avoid passing things like user IDs, session IDs, request IDs, or other values that vary significantly. See https://docs.datadoghq.com/getting_started/tagging/ for more information.

Users may configure some actions to be taken when a metric cardinality threshold is exceeded. See [TagTrackerConfiguration] for more information.

References

Dependencies

~1.4–7MB
~45K SLoC