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

prima_datadog

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

20 unstable releases (7 breaking)

new 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

#46 in Science

Download history 1252/week @ 2024-02-29 1403/week @ 2024-03-07 1641/week @ 2024-03-14 1122/week @ 2024-03-21 942/week @ 2024-03-28 1057/week @ 2024-04-04 886/week @ 2024-04-11 940/week @ 2024-04-18 639/week @ 2024-04-25 1361/week @ 2024-05-02 925/week @ 2024-05-09 763/week @ 2024-05-16 1113/week @ 2024-05-23 1333/week @ 2024-05-30 1160/week @ 2024-06-06 674/week @ 2024-06-13

4,399 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.5–7.5MB
~46K SLoC