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

prima_datadog

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

18 releases

0.7.1 Mar 6, 2024
0.6.0 Oct 2, 2023
0.6.0-rc.0 Dec 18, 2023
0.5.0 Dec 6, 2022
0.1.4 Sep 27, 2021

#60 in Science

Download history 1262/week @ 2024-01-01 924/week @ 2024-01-08 644/week @ 2024-01-15 913/week @ 2024-01-22 903/week @ 2024-01-29 960/week @ 2024-02-05 831/week @ 2024-02-12 796/week @ 2024-02-19 1103/week @ 2024-02-26 1497/week @ 2024-03-04 1501/week @ 2024-03-11 1412/week @ 2024-03-18 1172/week @ 2024-03-25 760/week @ 2024-04-01 1087/week @ 2024-04-08 768/week @ 2024-04-15

3,794 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–9MB
~48K SLoC