datadog_extensions/
datadog-extensions.rs

1// Cadence - An extensible Statsd client for Rust!
2//
3// To the extent possible under law, the author(s) have dedicated all copyright and
4// related and neighboring rights to this file to the public domain worldwide.
5// This software is distributed without any warranty.
6//
7// You should have received a copy of the CC0 Public Domain Dedication along with this
8// software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
9
10// This example shows how you set a timestamp, sampling rate or container id
11// as described on https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/.
12
13use cadence::prelude::*;
14use cadence::{MetricError, NopMetricSink, StatsdClient};
15
16fn main() {
17    fn my_error_handler(err: MetricError) {
18        eprintln!("Error sending metrics: {}", err);
19    }
20
21    // Create a client with an error handler and default "region" tag
22    let client = StatsdClient::builder("my.prefix", NopMetricSink)
23        .with_error_handler(my_error_handler)
24        .with_container_id("container-123")
25        .build();
26
27    // In this case we are sending a counter metric with manually set timestamp,
28    // container id and sampling rate. If sending the metric fails, our error
29    // handler set above will be invoked to do something with the metric error.
30    client
31        .count_with_tags("counter.1", 1)
32        .with_timestamp(123456)
33        .with_container_id("container-456")
34        .with_sampling_rate(0.5)
35        .send();
36
37    // In this case we are sending the same counter metrics without any explicit container
38    // id, meaning that the client's container id will be used.
39    let res = client.count_with_tags("counter.2", 1).try_send();
40
41    println!("Result of metric send: {:?}", res);
42}