Struct datadog_statsd::client::Client
source · pub struct Client { /* private fields */ }
Expand description
Client socket for statsd servers.
After creating a metric you can use Client
to send metrics to the configured statsd server
§Example
Creating a client and sending metrics is easy.
use datadog_statsd::client::Client;
let client = Client::new("127.0.0.1:8125", "myapp", tags);
client.incr("some.metric.completed");
Implementations§
source§impl Client
impl Client
sourcepub fn new<T: ToSocketAddrs>(
host: T,
prefix: &str,
constant_tags: Option<Vec<&str>>
) -> Result<Client, StatsdError>
pub fn new<T: ToSocketAddrs>( host: T, prefix: &str, constant_tags: Option<Vec<&str>> ) -> Result<Client, StatsdError>
Construct a new statsd client given an host/port & prefix
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn incr(&self, metric: &str, tags: &Option<Vec<&str>>)
pub fn incr(&self, metric: &str, tags: &Option<Vec<&str>>)
Increment a metric by 1
client.incr("metric.completed", tags);
This modifies a counter with an effective sampling rate of 1.0.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn decr(&self, metric: &str, tags: &Option<Vec<&str>>)
pub fn decr(&self, metric: &str, tags: &Option<Vec<&str>>)
Decrement a metric by -1
client.decr("metric.completed", tags);
This modifies a counter with an effective sampling rate of 1.0.
sourcepub fn count(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
pub fn count(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
Modify a counter by value
.
Will increment or decrement a counter by value
with
a sampling rate of 1.0.
// Increment by 12
client.count("metric.completed", 12.0, tags);
sourcepub fn sampled_count(
&self,
metric: &str,
value: f64,
rate: f64,
tags: &Option<Vec<&str>>
)
pub fn sampled_count( &self, metric: &str, value: f64, rate: f64, tags: &Option<Vec<&str>> )
Modify a counter by value
only x% of the time.
Will increment or decrement a counter by value
with
a custom sampling rate.
// Increment by 4 50% of the time.
client.sampled_count("metric.completed", 4, 0.5, tags);
sourcepub fn gauge(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
pub fn gauge(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
Set a gauge value.
// set a gauge to 9001
client.gauge("power_level.observed", 9001.0, tags);
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn timer(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
pub fn timer(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
Send a timer value.
The value is expected to be in ms.
// pass a duration value
client.timer("response.duration", 10.123, tags);
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn time<F, R>(
&self,
metric: &str,
tags: &Option<Vec<&str>>,
callable: F
) -> Rwhere
F: FnOnce() -> R,
pub fn time<F, R>(
&self,
metric: &str,
tags: &Option<Vec<&str>>,
callable: F
) -> Rwhere
F: FnOnce() -> R,
Time a block of code.
The passed closure will be timed and executed. The block’s duration will be sent as a metric.
// pass a duration value
client.time("response.duration", tags, || {
// Your code here.
});
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn pipeline(&self) -> Pipeline
pub fn pipeline(&self) -> Pipeline
Get a pipeline struct that allows optimizes the number of UDP packets used to send multiple metrics
let mut pipeline = client.pipeline();
pipeline.incr("some.metric", 1);
pipeline.incr("other.metric", 1);
pipeline.send(&mut client);
sourcepub fn histogram(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
pub fn histogram(&self, metric: &str, value: f64, tags: &Option<Vec<&str>>)
Send a histogram value.
// pass response size value
client.histogram("response.size", 128.0, tags);
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn event(
&self,
title: &str,
text: &str,
alert_type: AlertType,
tags: &Option<Vec<&str>>
)
pub fn event( &self, title: &str, text: &str, alert_type: AlertType, tags: &Option<Vec<&str>> )
Send a event.
// pass a app start event
client.event("MyApp Start", "MyApp Details", AlertType::Info, &Some(vec!["tag1", "tag2:test"]));
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}
sourcepub fn service_check(
&self,
service_check_name: &str,
status: ServiceCheckStatus,
tags: &Option<Vec<&str>>
)
pub fn service_check( &self, service_check_name: &str, status: ServiceCheckStatus, tags: &Option<Vec<&str>> )
Send a service check.
// pass a app status
client.service_check("MyApp", ServiceCheckStatus::Ok, &Some(vec!["tag1", "tag2:test"]));
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let client = Client::new(
"127.0.0.1:8125",
"myapp",
Some(vec!["common1", "common2:test"]),
)
.unwrap();
let tags = &Some(vec!["tag1", "tag2:test"]);
client.incr("some.counter", tags);
println!("Sent a counter!");
client.gauge("some.gauge", 124.0, tags);
println!("Set a gauge!");
client.timer("timer.duration", 182.1, &None);
println!("Set a timer!");
client.time("closure.duration", tags, || {
println!("Timing a closure");
});
client.histogram("some.histogram", 104.3, tags);
println!("Set a histogram!");
client.event("event title", "event text", AlertType::Warning, tags);
println!("Sent a event!");
client.service_check(
"myapp.service.check.name",
ServiceCheckStatus::Critical,
tags,
);
println!("Sent a service_check!");
}