sending_tags/
sending-tags.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 might use Cadence to send DataDog-style tags
11// either with a method the returns the result of sending them, or with a
12// method the delegates any errors to a predefined error handler. It also
13// includes "default" tags which are automatically added to any metrics
14// sent.
15
16use cadence::prelude::*;
17use cadence::{MetricError, NopMetricSink, StatsdClient};
18
19fn main() {
20    fn my_error_handler(err: MetricError) {
21        eprintln!("Error sending metrics: {}", err);
22    }
23
24    // Create a client with an error handler and default "region" tag
25    let client = StatsdClient::builder("my.prefix", NopMetricSink)
26        .with_error_handler(my_error_handler)
27        .with_tag("region", "us-west-2")
28        .build();
29
30    // In this case we are sending a counter metric with two tag key-value
31    // pairs. If sending the metric fails, our error handler set above will
32    // be invoked to do something with the metric error.
33    client
34        .count_with_tags("requests.handled", 1)
35        .with_tag("app", "search")
36        .with_tag("user", "1234")
37        .send();
38
39    // In this case we are sending the same counter metrics with two tags.
40    // The results of sending the metric (or failing to send it) are returned
41    // to the caller to do something with.
42    let res = client
43        .count_with_tags("requests.handled", 1)
44        .with_tag("app", "search")
45        .with_tag("user", "1234")
46        .try_send();
47
48    println!("Result of metric send: {:?}", res);
49}