pub struct Metrics { /* private fields */ }
Expand description
The metrics struct is used to initialize the metrics communications channels and access the
Histogram
data for each named metric.
Implementations§
Source§impl Metrics
impl Metrics
Sourcepub fn for_each_histogram<F>(&self, f: F)
pub fn for_each_histogram<F>(&self, f: F)
Iterate the histograms created. This function accepts a closure of FnMut(&'static str, &Histogram<u64>)
taking the span name and the histogram as arguments.
Examples found in repository?
24fn main() {
25 let metrics = Metrics::new(1);
26
27 (0..2).for_each(|_| long_scoped());
28 (0..2).for_each(|_| long());
29 (0..2).for_each(|_| short_scoped());
30 (0..2).for_each(|_| short());
31
32 (0..2).for_each(|_| long_scoped());
33 (0..2).for_each(|_| long());
34 (0..2).for_each(|_| short_scoped());
35 (0..2).for_each(|_| short());
36
37 metrics.for_each_histogram(|span_name, h| {
38 println!("{} -> {:.2}ms", span_name, h.mean() / 1_000_000.0);
39 });
40}
More examples
26fn main() {
27 let metrics = Metrics::new(1);
28
29 let t1 = std::thread::spawn(|| {
30 (0..10).for_each(|_| long_scoped());
31 (0..10).for_each(|_| long());
32 (0..10).for_each(|_| short_scoped());
33 (0..10).for_each(|_| short());
34 });
35
36 let t2 = std::thread::spawn(|| {
37 (0..10).for_each(|_| long_scoped());
38 (0..10).for_each(|_| long());
39 (0..10).for_each(|_| short_scoped());
40 (0..10).for_each(|_| short());
41 });
42
43 t1.join().unwrap();
44 t2.join().unwrap();
45
46 metrics.for_each_histogram(|span_name, h| {
47 println!("{} -> {:.2}ms", span_name, h.mean() / 1_000_000.0);
48 });
49}
Sourcepub fn flush(&self)
pub fn flush(&self)
Blocks the current thread until the worker thread has completed flushing the receiver.
§Warning
In high contention situations, this may block indefinitely. This method is meant to be used in the context of a game engine, where execution can be guaranteed to be blocked for metric collection.
Sourcepub fn new(sigfig: u8) -> Metrics
pub fn new(sigfig: u8) -> Metrics
Creates a new metrcs instance, initializing metrics and spawning a worker to collect the data.
§Warning
Any given instance of Metrics
will globally collect a duplicate of the Histgram
data. Only
one instance should be active at a time.
Examples found in repository?
24fn main() {
25 let metrics = Metrics::new(1);
26
27 (0..2).for_each(|_| long_scoped());
28 (0..2).for_each(|_| long());
29 (0..2).for_each(|_| short_scoped());
30 (0..2).for_each(|_| short());
31
32 (0..2).for_each(|_| long_scoped());
33 (0..2).for_each(|_| long());
34 (0..2).for_each(|_| short_scoped());
35 (0..2).for_each(|_| short());
36
37 metrics.for_each_histogram(|span_name, h| {
38 println!("{} -> {:.2}ms", span_name, h.mean() / 1_000_000.0);
39 });
40}
More examples
26fn main() {
27 let metrics = Metrics::new(1);
28
29 let t1 = std::thread::spawn(|| {
30 (0..10).for_each(|_| long_scoped());
31 (0..10).for_each(|_| long());
32 (0..10).for_each(|_| short_scoped());
33 (0..10).for_each(|_| short());
34 });
35
36 let t2 = std::thread::spawn(|| {
37 (0..10).for_each(|_| long_scoped());
38 (0..10).for_each(|_| long());
39 (0..10).for_each(|_| short_scoped());
40 (0..10).for_each(|_| short());
41 });
42
43 t1.join().unwrap();
44 t2.join().unwrap();
45
46 metrics.for_each_histogram(|span_name, h| {
47 println!("{} -> {:.2}ms", span_name, h.mean() / 1_000_000.0);
48 });
49}