1 2 3 4 5 6 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
//! `Future`-powered Metrics collection and reporting //! //! //! Example use within a mixed multi-threaded app with tokio //! //! ```no_run //! extern crate log; //! extern crate env_logger; //! extern crate futures; //! extern crate tacho; //! extern crate tokio_core; //! extern crate tokio_timer; //! //! use futures::Stream; //! use std::io::{Error, ErrorKind}; //! use std::time::Duration; //! use std::thread; //! use tokio_core::reactor::Core; //! use tokio_timer::Timer as TokioTimer; //! //! use tacho::{Counter, Gauge}; //! use tacho::timer::Timer; //! use tacho::metrics; //! //! // A performance test for an asynchronous Metrics reporter with timers, counters, and gauges. //! fn main() { //! drop(env_logger::init()); //! //! let (recorder, aggregator) = metrics::new(); //! //! let work_thread = { //! let mut tx = recorder.clone(); //! let mut total_timer = Timer::new("total_time_us".to_owned()); //! thread::spawn(move || { //! for i in 0..100_000_000 { //! if i % 100 == 0 { //! thread::sleep(Duration::from_millis(1)); //! } //! let mut loop_timer = Timer::new("loop_timer_us".to_owned()); //! let mut loop_counter = Counter::new("total_loops".to_owned(), 0); //! let loop_gauge = Gauge::new("loop_iter".to_owned(), i); //! loop_timer.start(); //! loop_counter.incr(1); //! // Do your work here //! loop_timer.stop(); //! tx.record(vec![loop_counter], vec![loop_gauge], vec![loop_timer]); //! } //! total_timer.stop(); //! tx.record(vec![], vec![], vec![total_timer]) //! }) //! }; //! //! let mut core = Core::new().expect("Failed to create core"); //! let handle = core.handle(); //! let (aggregated, aggregating) = aggregator.aggregate(); //! handle.spawn(aggregating); //! handle.spawn(metrics::report_generator(aggregated)); //! //! let mut tx = recorder.clone(); //! let mut heartbeats = 0; //! let heartbeater = TokioTimer::default() //! .interval(Duration::from_millis(1000)) //! .map_err(|_| Error::new(ErrorKind::Other, "unable to run heartbeat")) //! .for_each(|_| { //! heartbeats += 1; //! let heartbeats_gauge = Gauge::new("heartbeats".to_owned(), heartbeats); //! tx.record(vec![], vec![heartbeats_gauge], vec![]); //! Ok(()) as Result<(), std::io::Error> //! }); //! core.run(heartbeater).expect("heartbeat failed"); //! work_thread.join().expect("work thread failed to join"); //! } //! ``` extern crate chrono; extern crate futures; extern crate hdrsample; extern crate tokio_core; extern crate tokio_timer; #[macro_use] extern crate log; extern crate twox_hash; use std::collections::VecDeque; pub mod metrics; pub mod reporter; pub mod timer; pub use metrics::{Metrics, Recorder}; pub use timer::Timer as Timed; // Counters are monotonically increasing values. #[derive(Clone, Debug)] pub struct Counter { pub name: String, pub value: u64, } impl Counter { // Creates a Counter with a given name and initial value. pub fn new(name: String, init: u64) -> Counter { Counter { name: name, value: init, } } pub fn incr(&mut self, n: u64) { self.value += n; } pub fn fresh(&self) -> Counter { Counter { name: self.name.clone(), value: 0, } } } #[derive(Clone, Debug)] pub struct Gauge { pub name: String, pub value: u64, } impl Gauge { pub fn new(name: String, n: u64) -> Gauge { Gauge { name: name, value: n, } } pub fn set(&mut self, n: u64) { self.value = n; } } pub struct Stat { pub name: String, pub values: VecDeque<u64>, } #[cfg(test)] mod tests { use super::*; #[test] fn counter_incr() { let mut counter = Counter::new("foo".to_owned(), 3); counter.incr(1); counter.incr(1); counter.incr(1); assert!(counter.value == 6); } #[test] fn test_basic_gauges() { let mut v = Gauge::new("foo".into(), 123); v.set(432); assert_eq!(v.value, 432); } }