Trait dipstick::InputScope
source · pub trait InputScope: Flush {
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric;
fn counter(&self, name: &str) -> Counter { ... }
fn marker(&self, name: &str) -> Marker { ... }
fn timer(&self, name: &str) -> Timer { ... }
fn gauge(&self, name: &str) -> Gauge { ... }
fn level(&self, name: &str) -> Level { ... }
}
Expand description
InputScope Define metrics, write values and flush them.
Required Methods§
sourcefn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric
Define a generic metric of the specified type. It is preferable to use counter() / marker() / timer() / gauge() methods.
Provided Methods§
sourcefn counter(&self, name: &str) -> Counter
fn counter(&self, name: &str) -> Counter
Define a Counter.
Examples found in repository?
examples/graphite.rs (line 13)
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));
}
}
More examples
examples/text_format_label.rs (line 36)
32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let counter = Stream::write_to_stderr()
.formatting(MyFormat)
.metrics()
.counter("counter_a");
AppLabel::set("abc", "xyz");
loop {
// report some metric values from our "application" loop
counter.count(11);
sleep(Duration::from_millis(500));
}
}
examples/cache.rs (line 16)
8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let metrics = Stream::write_to(io::stdout())
.cached(5)
.metrics()
.named("cache");
loop {
// report some ad-hoc metric values from our "application" loop
metrics.counter("blorf").count(1134);
metrics.marker("burg").mark();
sleep(Duration::from_millis(500));
}
}
examples/statsd_sampling.rs (line 13)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let metrics = Statsd::send_to("localhost:8125")
.expect("Connected")
.sampled(Sampling::Random(0.2))
.named("my_app")
.metrics();
let counter = metrics.counter("counter_a");
loop {
for i in 1..11 {
counter.count(i);
}
std::thread::sleep(Duration::from_millis(3000));
}
}
examples/statsd_nosampling.rs (line 13)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let metrics = Statsd::send_to("localhost:8125")
.expect("Connected")
// .with_sampling(Sampling::Random(0.2))
.named("my_app")
.metrics();
let counter = metrics.counter("counter_a");
loop {
for i in 1..11 as usize {
counter.count(i);
}
std::thread::sleep(Duration::from_millis(3000));
}
}
examples/prometheus_labels.rs (line 16)
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));
}
}
sourcefn marker(&self, name: &str) -> Marker
fn marker(&self, name: &str) -> Marker
Define a Marker.
Examples found in repository?
examples/buffered_flush_on_drop.rs (line 16)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
let input = Stream::write_to_stdout().buffered(Buffering::Unlimited);
loop {
println!("\n------- open scope");
let metrics = input.metrics();
metrics.marker("marker_a").mark();
sleep(Duration::from_millis(1000));
println!("------- close scope: ");
}
}
More examples
examples/cache.rs (line 17)
8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let metrics = Stream::write_to(io::stdout())
.cached(5)
.metrics()
.named("cache");
loop {
// report some ad-hoc metric values from our "application" loop
metrics.counter("blorf").count(1134);
metrics.marker("burg").mark();
sleep(Duration::from_millis(500));
}
}
examples/bucket_cleanup.rs (line 12)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
let bucket = AtomicBucket::new();
AtomicBucket::default_drain(Stream::write_to_stdout());
let persistent_marker = bucket.marker("persistent");
let mut i = 0;
loop {
i += 1;
let transient_marker = bucket.marker(&format!("marker_{}", i));
transient_marker.mark();
persistent_marker.mark();
bucket.flush().unwrap();
sleep(Duration::from_secs(1));
}
}
examples/bench_bucket.rs (line 12)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let bucket = AtomicBucket::new();
let event = bucket.marker("a");
let args = &mut args();
args.next();
let tc: u8 = u8::from_str(&args.next().unwrap()).unwrap();
for _ in 0..tc {
let event = event.clone();
thread::spawn(move || {
loop {
// report some metric values from our "application" loop
event.mark();
}
});
}
sleep(Duration::from_secs(5));
bucket.stats(stats_all);
bucket
.flush_to(&Stream::write_to_stdout().metrics())
.unwrap();
}
examples/bench_bucket_proxy.rs (line 11)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
fn main() {
let event = Proxy::default().marker("a");
let bucket = AtomicBucket::new();
Proxy::default().target(bucket.clone());
let args = &mut args();
args.next();
let tc: u8 = u8::from_str(&args.next().unwrap()).unwrap();
for _ in 0..tc {
let event = event.clone();
thread::spawn(move || {
loop {
// report some metric values from our "application" loop
event.mark();
}
});
}
sleep(Duration::from_secs(5));
bucket
.flush_to(&Stream::write_to_stdout().metrics())
.unwrap();
}
examples/observer.rs (line 35)
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
fn main() {
let metrics = AtomicBucket::new().named("process");
metrics.drain(Stream::write_to_stdout());
metrics.flush_every(Duration::from_secs(3));
let uptime = metrics.gauge("uptime");
metrics.observe(uptime, |_| 6).on_flush();
// record number of threads in pool every second
let scheduled = metrics
.observe(metrics.gauge("threads"), thread_count)
.every(Duration::from_secs(1));
// "heartbeat" metric
let on_flush = metrics
.observe(metrics.marker("heartbeat"), |_| 1)
.on_flush();
for _ in 0..1000 {
std::thread::sleep(Duration::from_millis(40));
}
on_flush.cancel();
scheduled.cancel();
}
sourcefn timer(&self, name: &str) -> Timer
fn timer(&self, name: &str) -> Timer
Define a Timer.
Examples found in repository?
examples/graphite.rs (line 14)
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));
}
}
More examples
examples/prometheus_labels.rs (line 17)
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));
}
}
examples/bucket2stdout.rs (line 15)
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();
}
}
examples/bucket_summary.rs (line 14)
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();
}
}
examples/multi_input.rs (line 22)
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));
}
}
examples/bucket2graphite.rs (line 23)
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 42 43
fn main() {
// adding a name to the stats
let bucket = AtomicBucket::new().named("test");
// adding two names to Graphite output
// metrics will be prefixed with "machine1.application.test"
bucket.drain(
Graphite::send_to("localhost:2003")
.expect("Socket")
.named("machine1")
.add_name("application"),
);
bucket.flush_every(Duration::from_secs(3));
let counter = bucket.counter("counter_a");
let timer = bucket.timer("timer_a");
let gauge = bucket.gauge("gauge_a");
let marker = bucket.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();
}
}
Additional examples can be found in:
sourcefn gauge(&self, name: &str) -> Gauge
fn gauge(&self, name: &str) -> Gauge
Define a Gauge.
Examples found in repository?
examples/observer.rs (line 25)
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
fn main() {
let metrics = AtomicBucket::new().named("process");
metrics.drain(Stream::write_to_stdout());
metrics.flush_every(Duration::from_secs(3));
let uptime = metrics.gauge("uptime");
metrics.observe(uptime, |_| 6).on_flush();
// record number of threads in pool every second
let scheduled = metrics
.observe(metrics.gauge("threads"), thread_count)
.every(Duration::from_secs(1));
// "heartbeat" metric
let on_flush = metrics
.observe(metrics.marker("heartbeat"), |_| 1)
.on_flush();
for _ in 0..1000 {
std::thread::sleep(Duration::from_millis(40));
}
on_flush.cancel();
scheduled.cancel();
}
More examples
examples/bucket2stdout.rs (line 16)
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();
}
}
examples/bucket_summary.rs (line 15)
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();
}
}
examples/bucket2graphite.rs (line 24)
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 42 43
fn main() {
// adding a name to the stats
let bucket = AtomicBucket::new().named("test");
// adding two names to Graphite output
// metrics will be prefixed with "machine1.application.test"
bucket.drain(
Graphite::send_to("localhost:2003")
.expect("Socket")
.named("machine1")
.add_name("application"),
);
bucket.flush_every(Duration::from_secs(3));
let counter = bucket.counter("counter_a");
let timer = bucket.timer("timer_a");
let gauge = bucket.gauge("gauge_a");
let marker = bucket.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();
}
}
examples/basics.rs (line 24)
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);
}
examples/custom_publish.rs (line 53)
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn main() {
fn custom_statistics(
kind: InputKind,
mut name: MetricName,
score: ScoreType,
) -> Option<(InputKind, MetricName, MetricValue)> {
match (kind, score) {
// do not export gauge scores
(InputKind::Gauge, _) => None,
// prepend and append to metric name
(_, ScoreType::Count(count)) => {
if let Some(last) = name.pop_back() {
Some((
InputKind::Counter,
name.append("customized_add_prefix")
.append(format!("{}_and_a_suffix", last)),
count,
))
} else {
None
}
}
// scaling the score value and appending unit to name
(kind, ScoreType::Sum(sum)) => Some((kind, name.append("per_thousand"), sum / 1000)),
// using the unmodified metric name
(kind, ScoreType::Mean(avg)) => Some((kind, name, avg.round() as MetricValue)),
// do not export min and max
_ => None,
}
}
// send application metrics to aggregator
AtomicBucket::default_drain(Stream::write_to_stderr());
AtomicBucket::default_stats(custom_statistics);
let app_metrics = AtomicBucket::new();
// schedule aggregated metrics to be printed every 3 seconds
app_metrics.flush_every(Duration::from_secs(3));
let counter = app_metrics.counter("counter_a");
let timer = app_metrics.timer("timer_b");
let gauge = app_metrics.gauge("gauge_c");
loop {
counter.count(11);
timer.interval_us(654654);
gauge.value(3534);
}
}
sourcefn level(&self, name: &str) -> Level
fn level(&self, name: &str) -> Level
Define a Level.
Examples found in repository?
examples/basics.rs (line 15)
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);
}