value_packing/value-packing.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 multiple values with each request
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.
13
14use cadence::prelude::*;
15use cadence::{MetricError, NopMetricSink, StatsdClient};
16
17fn main() {
18 fn my_error_handler(err: MetricError) {
19 eprintln!("Error sending metrics: {}", err);
20 }
21
22 let client = StatsdClient::builder("my.prefix", NopMetricSink)
23 .with_error_handler(my_error_handler)
24 .build();
25
26 // In this case we are sending a distribution metric with two tag key-value
27 // pairs. If sending the metric fails, our error handler set above will
28 // be invoked to do something with the metric error.
29 client
30 .distribution_with_tags("latency.milliseconds", vec![10, 20, 30, 40, 50])
31 .with_tag("app", "search")
32 .with_tag("region", "us-west-2")
33 .send();
34
35 // In this case we are sending the same distribution metrics with two tags.
36 // The results of sending the metric (or failing to send it) are returned
37 // to the caller to do something with.
38 let res = client
39 .distribution_with_tags("latency.milliseconds", vec![10, 20, 30, 40, 50])
40 .with_tag("app", "search")
41 .with_tag("region", "us-west-2")
42 .try_send();
43
44 println!("Result of metric send: {:?}", res);
45}