#datadog #logging #metrics #opinionated #udp #approach #prima

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

#2 in #approach

Download history 1239/week @ 2024-07-24 1152/week @ 2024-07-31 1695/week @ 2024-08-07 1127/week @ 2024-08-14 1594/week @ 2024-08-21 1362/week @ 2024-08-28 1542/week @ 2024-09-04 1478/week @ 2024-09-11 1465/week @ 2024-09-18 1897/week @ 2024-09-25 2012/week @ 2024-10-02 1586/week @ 2024-10-09 1843/week @ 2024-10-16 1513/week @ 2024-10-23 1275/week @ 2024-10-30 1495/week @ 2024-11-06

6,393 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–6.5MB
~44K SLoC