pub struct StatsdClientBuilder { /* private fields */ }Expand description
Builder for creating and customizing StatsdClient instances.
Instances of the builder should be created by calling the ::builder()
method on the StatsClient struct.
§Example
use cadence::prelude::*;
use cadence::{MetricError, StatsdClient, NopMetricSink};
fn my_error_handler(err: MetricError) {
println!("Metric error! {}", err);
}
let client = StatsdClient::builder("prefix", NopMetricSink)
.with_error_handler(my_error_handler)
.with_tag("environment", "production")
.with_tag_value("rust")
.build();
client.count("something", 123);
client.count_with_tags("some.counter", 42)
.with_tag("region", "us-east-2")
.send();Implementations§
Source§impl StatsdClientBuilder
impl StatsdClientBuilder
Sourcepub fn with_error_handler<F>(self, errors: F) -> Self
pub fn with_error_handler<F>(self, errors: F) -> Self
Set an error handler to use for metrics sent via MetricBuilder::send()
The error handler is only invoked when metrics are not able to be sent
correctly. Either due to invalid input, I/O errors encountered when trying
to send them via a MetricSink, or some other reason.
The error handler should consume the error without panicking. The error may be logged, printed to stderr, discarded, etc. - this is up to the implementation.
Examples found in repository?
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}More examples
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}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}Sourcepub fn with_tag<K, V>(self, key: K, value: V) -> Self
pub fn with_tag<K, V>(self, key: K, value: V) -> Self
Add a default tag with key and value to every metric published by the built StatsdClient.
Examples found in repository?
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}Sourcepub fn with_tag_value<K>(self, value: K) -> Selfwhere
K: ToString,
pub fn with_tag_value<K>(self, value: K) -> Selfwhere
K: ToString,
Add a default tag with only a value to every metric published by the built StatsdClient.
Sourcepub fn with_container_id<K>(self, container_id: K) -> Selfwhere
K: ToString,
pub fn with_container_id<K>(self, container_id: K) -> Selfwhere
K: ToString,
Add a default container ID to every metric published by the built StatsdClient.
Examples found in repository?
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}Sourcepub fn build(self) -> StatsdClient
pub fn build(self) -> StatsdClient
Construct a new StatsdClient instance based on current settings.
Examples found in repository?
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}More examples
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}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}