pub struct Timer { /* private fields */ }
Expand description
A timer that sends values to the metrics backend Timers can record time intervals in multiple ways :
- with the time! macrohich wraps an expression or block with start() and stop() calls.
- with the time(Fn) methodhich wraps a closure with start() and stop() calls.
- with start() and stop() methodsrapping around the operation to time
- with the interval_us() method, providing an externally determined microsecond interval
Implementations§
source§impl Timer
impl Timer
sourcepub fn interval_us(&self, interval_us: u64) -> u64
pub fn interval_us(&self, interval_us: u64) -> u64
Record a microsecond interval for this timer Can be used in place of start()/stop() if an external time interval source is used
Examples found in repository?
More examples
6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let metrics = Graphite::send_to("localhost:2003")
.expect("Connected")
.named("my_app")
.metrics();
loop {
metrics.counter("counter_a").count(123);
metrics.timer("timer_a").interval_us(2000000);
std::thread::sleep(Duration::from_millis(40));
}
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let metrics = Prometheus::push_to("http://localhost:9091/metrics/job/prometheus_example")
.expect("Prometheus Socket")
.named("my_app")
.metrics();
AppLabel::set("abc", "456");
ThreadLabel::set("xyz", "123");
loop {
metrics.counter("counter_a").count(123);
metrics.timer("timer_a").interval_us(2000000);
std::thread::sleep(Duration::from_millis(40));
}
}
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
fn main() {
let metrics = AtomicBucket::new().named("test");
metrics.drain(Stream::write_to_stdout());
metrics.flush_every(Duration::from_secs(3));
let counter = metrics.counter("counter_a");
let timer = metrics.timer("timer_a");
let gauge = metrics.gauge("gauge_a");
let marker = metrics.marker("marker_a");
loop {
// add counts forever, non-stop
counter.count(11);
counter.count(12);
counter.count(13);
timer.interval_us(11_000_000);
timer.interval_us(12_000_000);
timer.interval_us(13_000_000);
gauge.value(11);
gauge.value(12);
gauge.value(13);
marker.mark();
}
}
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
fn main() {
let app_metrics = AtomicBucket::new();
app_metrics.drain(Stream::write_to_stdout());
app_metrics.flush_every(Duration::from_secs(3));
let counter = app_metrics.counter("counter_a");
let timer = app_metrics.timer("timer_a");
let gauge = app_metrics.gauge("gauge_a");
let marker = app_metrics.marker("marker_a");
loop {
// add counts forever, non-stop
counter.count(11);
counter.count(12);
counter.count(13);
timer.interval_us(11_000_000);
timer.interval_us(12_000_000);
timer.interval_us(13_000_000);
gauge.value(11);
gauge.value(12);
gauge.value(13);
marker.mark();
}
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
// will output metrics to graphite and to stdout
let different_type_metrics = MultiInput::new()
.add_target(Graphite::send_to("localhost:2003").expect("Connecting"))
.add_target(Stream::write_to_stdout())
.metrics();
// will output metrics twice, once with "both.yeah" prefix and once with "both.ouch" prefix.
let same_type_metrics = MultiInput::new()
.add_target(Stream::write_to_stderr().named("yeah"))
.add_target(Stream::write_to_stderr().named("ouch"))
.named("both")
.metrics();
loop {
different_type_metrics.counter("counter_a").count(123);
same_type_metrics.timer("timer_a").interval_us(2000000);
std::thread::sleep(Duration::from_millis(400));
}
}
sourcepub fn start(&self) -> TimeHandle
pub fn start(&self) -> TimeHandle
Obtain a opaque handle to the current time. The handle is passed back to the stop() method to record a time interval. This is actually a convenience method to the TimeHandle::now() Beware, handles obtained here are not bound to this specific timer instance for now but might be in the future for safety. If you require safe multi-timer handles, get them through TimeType::now()
Examples found in repository?
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 42 43 44 45 46 47 48 49 50
fn main() {
// for this demo, print metric values to the console
let app_metrics = Stream::write_to(io::stdout()).metrics();
// metrics can be predefined by type and name
let counter = app_metrics.counter("counter_a");
let level = app_metrics.level("level_a");
let timer = app_metrics.timer("timer_b");
// metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
app_metrics.counter("just_once").count(4);
// metric names can be prepended with a common prefix
let prefixed_metrics = app_metrics.named("subsystem");
let event = prefixed_metrics.marker("event_c");
let gauge = prefixed_metrics.gauge("gauge_d");
// each kind of metric has a different method name to prevent errors
counter.count(11);
level.adjust(-4);
level.adjust(5);
gauge.value(22);
gauge.value(-24);
event.mark();
// time can be measured multiple equivalent ways:
// in microseconds (used internally)
timer.interval_us(35573);
// using the time! macro
time!(timer, sleep(Duration::from_millis(5)));
// using a closure
timer.time(|| sleep(Duration::from_millis(5)));
// using a "time handle"
let start_time = timer.start();
sleep(Duration::from_millis(5));
timer.stop(start_time);
}
sourcepub fn stop(&self, start_time: TimeHandle) -> MetricValue
pub fn stop(&self, start_time: TimeHandle) -> MetricValue
Record the time elapsed since the start_time handle was obtained. This call can be performed multiple times using the same handle, reporting distinct time intervals each time. Returns the microsecond interval value that was recorded.
Examples found in repository?
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 42 43 44 45 46 47 48 49 50
fn main() {
// for this demo, print metric values to the console
let app_metrics = Stream::write_to(io::stdout()).metrics();
// metrics can be predefined by type and name
let counter = app_metrics.counter("counter_a");
let level = app_metrics.level("level_a");
let timer = app_metrics.timer("timer_b");
// metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
app_metrics.counter("just_once").count(4);
// metric names can be prepended with a common prefix
let prefixed_metrics = app_metrics.named("subsystem");
let event = prefixed_metrics.marker("event_c");
let gauge = prefixed_metrics.gauge("gauge_d");
// each kind of metric has a different method name to prevent errors
counter.count(11);
level.adjust(-4);
level.adjust(5);
gauge.value(22);
gauge.value(-24);
event.mark();
// time can be measured multiple equivalent ways:
// in microseconds (used internally)
timer.interval_us(35573);
// using the time! macro
time!(timer, sleep(Duration::from_millis(5)));
// using a closure
timer.time(|| sleep(Duration::from_millis(5)));
// using a "time handle"
let start_time = timer.start();
sleep(Duration::from_millis(5));
timer.stop(start_time);
}
sourcepub fn time<F: FnOnce() -> R, R>(&self, operations: F) -> R
pub fn time<F: FnOnce() -> R, R>(&self, operations: F) -> R
Record the time taken to execute the provided closure
Examples found in repository?
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 42 43 44 45 46 47 48 49 50
fn main() {
// for this demo, print metric values to the console
let app_metrics = Stream::write_to(io::stdout()).metrics();
// metrics can be predefined by type and name
let counter = app_metrics.counter("counter_a");
let level = app_metrics.level("level_a");
let timer = app_metrics.timer("timer_b");
// metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
app_metrics.counter("just_once").count(4);
// metric names can be prepended with a common prefix
let prefixed_metrics = app_metrics.named("subsystem");
let event = prefixed_metrics.marker("event_c");
let gauge = prefixed_metrics.gauge("gauge_d");
// each kind of metric has a different method name to prevent errors
counter.count(11);
level.adjust(-4);
level.adjust(5);
gauge.value(22);
gauge.value(-24);
event.mark();
// time can be measured multiple equivalent ways:
// in microseconds (used internally)
timer.interval_us(35573);
// using the time! macro
time!(timer, sleep(Duration::from_millis(5)));
// using a closure
timer.time(|| sleep(Duration::from_millis(5)));
// using a "time handle"
let start_time = timer.start();
sleep(Duration::from_millis(5));
timer.stop(start_time);
}
Methods from Deref<Target = InputMetric>§
sourcepub fn write(&self, value: MetricValue, labels: Labels)
pub fn write(&self, value: MetricValue, labels: Labels)
Collect a new value for this metric.