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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
//! # metrix //! //! Metrics for monitoring applications and alerting. //! //! ## Goal //! //! Applications/services can have a lot of metrics and one of the greatest //! challenges is organizing them. This is what `metrix` tries to help with. //! //! **Metrix** does not aim for providing exact numbers and aims for //! applications monitoring only. //! //! This crate is in a very **early** stage and the API might still change. //! There may be backends provided for monitoring solutions in the future //! but currently only a snapshot that can be //! serialized to JSON is provided. //! //! ## How does it work //! //! **Metrix** is based on observations collected while running your //! application. These observations will then be sent to a backend where //! the actual metrics(counters etc.) are updated. For the metrics configured //! a snapshot can be queried. //! //! The primary focus of **metrix** is to organize these metrics. There are //! several building blocks available. Most of them can have a name that will //! then be part of a path within a snapshot. //! //! ### Labels //! //! Labels link observations to panels. Labels can be of any type that //! implements `Clone + Eq + Send + 'static`. An `enum` is a good choice for a //! label. //! //! ### Observations //! //! An observation is made somewhere within your application. When an //! observation is sent to the backend it must have a label attached. This label //! is then matched against the label of a panel to determine whether an //! observation is handled for updating or not. //! //! ### Instruments //! //! Instruments are gauges, meters, etc. An instrument gets updated by an //! observation where an update is meaningful. Instruments are grouped by //! `Panel`s. //! //! You can find instruments in the module `instruments`. //! //! ### Panels //! //! A `Panel` groups instruments under same same label. So each instrument //! within a panel will be updated by observations that have the same label as //! the panel. //! //! Lets say you defined a label `OutgoingRequests`. If you are interested //! in the request rate and the latencies. You would then create a panel with a //! label `OutgoingRequests` and add a histogram and a meter. //! //! ### Cockpit //! //! A cockpit aggregates multiple `Panel`s. A cockpit can be used to monitor //! different tasks/parts of a component or workflow. A cockpit //! is bound to a label type. //! //! An example can be that you have service component that calls an external //! HTTP client. You could be interested in successful calls and failed calls //! individually. So for both cases you would create a value for your label //! and then add two panels to the cockpit. //! //! Cockpits are in the module `cockpit`. //! //! ### Processors //! //! The most important processor is the `TelemetryProcessor`. It has //! a label type as a type parameter and consist of a `TelemetryTransmitter` //! that sends observations to the backend(used within your app) //! and the actual `TelemetryProcessor` that forms the backend and //! processes observations. The `TelemetryProcessor` //! can **own** several cockpits for a label type. //! //! There is also a `ProcessorMount` that is label agnostic and can group //! several processors. It can also have a name that will be included in the //! snapshot. //! //! The processors can be found the module `processor`. //! //! ### Driver //! //! The driver **owns** processors and asks the **owned** processors //! to process their messages. You need to add your processors to //! a driver to start the machinery. A driver is also a processor //! which means it can have a name and it can also be part of another //! hierarchy. //! //! Each driver has its own thread for polling its processors //! so even when attached to another //! hierarchy all processors registered with the driver will only //! be driven by that driver. //! //! //! ## Contributing //! //! Contributing is welcome. Criticism is also welcome! //! //! ## License //! //! Metrix is primarily distributed under the terms of //! both the MIT license and the Apache License (Version 2.0). //! //! Copyright (c) 2018 Christian Douven //! #[cfg(feature = "log")] #[macro_use] extern crate log; use snapshot::Snapshot; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; use cockpit::Cockpit; use instruments::Panel; use processor::TelemetryMessage; pub use observation::*; pub use processor::AggregatesProcessors; pub mod cockpit; pub mod driver; pub mod instruments; mod observation; pub mod processor; pub mod snapshot; pub(crate) mod util; /// Something that can react on `Observation`s where /// the `Label` is the type of the label. /// /// You can use this to implement your own metrics. pub trait HandlesObservations: PutsSnapshot + Send + 'static { type Label: Send + 'static; fn handle_observation(&mut self, observation: &Observation<Self::Label>) -> usize; } /// Const for setting boolean values. `true` is `1`. #[deprecated(since = "0.10.0", note = "use a bool directly")] pub const TRUE: u64 = 1; /// Const for setting boolean values. `false` is `0`. #[deprecated(since = "0.10.0", note = "use a bool directly")] pub const FALSE: u64 = 0; /// Const for incrementing on instruments with support. `INCR` is `std::u64::MAX`. #[deprecated(since = "0.10.0", note = "use crate::Increment")] pub const INCR: u64 = std::u64::MAX; /// Const for decrementing on instruments with support. `DECR` is `std::u64::MAX -1`. #[deprecated(since = "0.10.0", note = "use crate::Decrement")] pub const DECR: u64 = std::u64::MAX - 1; /// Increments a value by one (e.g. in a `Gauge`) #[derive(Debug, Copy, Clone)] pub struct Increment; /// Increments a value by the given amount (e.g. in a `Gauge`) #[derive(Debug, Copy, Clone)] pub struct IncrementBy(pub u32); /// Decrements a value by one (e.g. in a `Gauge`) #[derive(Debug, Copy, Clone)] pub struct Decrement; /// Decrements a value by the given amount (e.g. in a `Gauge`) #[derive(Debug, Copy, Clone)] pub struct DecrementBy(u32); /// Changes a value by the given amount (e.g. in a `Gauge`) #[derive(Debug, Copy, Clone)] pub struct ChangeBy(pub i64); /// Transmits telemetry data to the backend. /// /// Implementors should transfer `Observations` to /// a backend and manipulate the instruments there to not /// to interfere to much with the actual task being measured/observed pub trait TransmitsTelemetryData<L> { /// Transit an observation to the backend. fn transmit(&self, observation: Observation<L>) -> &Self; /// Observed `count` occurrences at time `timestamp` /// /// Convenience method. Simply calls `transmit` fn observed(&self, label: L, count: u64, timestamp: Instant) -> &Self { self.transmit(Observation::Observed { label, count, timestamp, }) } /// Observed one occurrence at time `timestamp` /// /// Convenience method. Simply calls `transmit` fn observed_one(&self, label: L, timestamp: Instant) -> &Self { self.transmit(Observation::ObservedOne { label, timestamp }) } /// Observed one occurrence with value `value` at time `timestamp` /// /// Convenience method. Simply calls `transmit` fn observed_one_value<V: Into<ObservedValue>>( &self, label: L, value: V, timestamp: Instant, ) -> &Self { self.transmit(Observation::ObservedOneValue { label, value: value.into(), timestamp, }) } /// Sends a `Duration` as an observed value observed at `timestamp`. /// The `Duration` is converted to nanoseconds. fn observed_duration(&self, label: L, duration: Duration, timestamp: Instant) -> &Self { self.observed_one_value(label, duration, timestamp) } /// Observed `count` occurrences at now. /// /// Convenience method. Simply calls `observed` with /// the current timestamp. fn observed_now(&self, label: L, count: u64) -> &Self { self.observed(label, count, Instant::now()) } /// Observed one occurrence now /// /// Convenience method. Simply calls `observed_one` with /// the current timestamp. fn observed_one_now(&self, label: L) -> &Self { self.observed_one(label, Instant::now()) } /// Observed one occurrence with value `value` now /// /// Convenience method. Simply calls `observed_one_value` with /// the current timestamp. fn observed_one_value_now<V: Into<ObservedValue>>(&self, label: L, value: V) -> &Self { self.observed_one_value(label, value, Instant::now()) } /// Sends a `Duration` as an observed value observed with the current /// timestamp. /// /// The `Duration` is converted to nanoseconds internally. fn observed_one_duration_now(&self, label: L, duration: Duration) -> &Self { self.observed_duration(label, duration, Instant::now()) } /// Measures the time from `from` until now. /// /// The resulting duration is an observed value /// with the measured duration in nanoseconds. fn measure_time(&self, label: L, from: Instant) -> &Self { let now = Instant::now(); if from <= now { self.observed_duration(label, now - from, now); } self } /// Add a handler. fn add_handler<H: HandlesObservations<Label = L>>(&self, handler: H) -> &Self where L: Send + 'static; /// Add a `Copckpit` fn add_cockpit(&self, cockpit: Cockpit<L>) -> &Self; /// Add a `Panel` to a `Cockpit` if that `Cockpit` has the /// given name. fn add_panel_to_cockpit(&self, cockpit_name: String, panel: Panel<L>) -> &Self; } /// Transmits `Observation`s to the backend /// /// This struct does **not** implement the `Sync` trait /// and can therefore not be shared between threads. /// See `synced()` method. #[derive(Clone)] pub struct TelemetryTransmitter<L> { sender: crossbeam_channel::Sender<TelemetryMessage<L>>, } impl<L> TelemetryTransmitter<L> where L: Send + 'static, { /// Get a `TelemetryTransmitterSync`. pub fn synced(&self) -> TelemetryTransmitterSync<L> { TelemetryTransmitterSync { sender: Arc::new(Mutex::new(self.sender.clone())), } } } impl<L> TransmitsTelemetryData<L> for TelemetryTransmitter<L> { fn transmit(&self, observation: Observation<L>) -> &Self { if let Err(err) = self.sender.send(TelemetryMessage::Observation(observation)) { util::log_error(format!("Failed to transmit observation: {}", err)); }; self } fn add_handler<H: HandlesObservations<Label = L>>(&self, handler: H) -> &Self where L: Send + 'static, { if let Err(err) = self .sender .send(TelemetryMessage::AddHandler(Box::new(handler))) { util::log_error(format!("Failed to add handler: {}", err)); }; self } fn add_cockpit(&self, cockpit: Cockpit<L>) -> &Self { if let Err(err) = self.sender.send(TelemetryMessage::AddCockpit(cockpit)) { util::log_error(format!("Failed to add cockpit: {}", err)); }; self } fn add_panel_to_cockpit(&self, cockpit_name: String, panel: Panel<L>) -> &Self { if let Err(err) = self.sender.send(TelemetryMessage::AddPanel { cockpit_name, panel, }) { util::log_error(format!("Failed to add panel to cockpit: {}", err)); }; self } } /// Transmits `Observation`s to the backend and has the `Sync` marker. /// /// This is almost the same as the `TelemetryTransmitter`. /// /// Since a `Sender` for a channel is not `Sync` this /// struct wraps the `Sender` in an `Arc<Mutex<_>>` so that /// it can be shared between threads. #[derive(Clone)] pub struct TelemetryTransmitterSync<L> { sender: Arc<Mutex<crossbeam_channel::Sender<TelemetryMessage<L>>>>, } impl<L> TelemetryTransmitterSync<L> where L: Send + 'static {} impl<L> TransmitsTelemetryData<L> for TelemetryTransmitterSync<L> { fn transmit(&self, observation: Observation<L>) -> &Self { if let Err(err) = self .sender .lock() .unwrap() .send(TelemetryMessage::Observation(observation)) { util::log_error(format!("Failed to transmit observation: {}", err)); }; self } fn add_handler<H: HandlesObservations<Label = L>>(&self, handler: H) -> &Self where L: Send + 'static, { if let Err(err) = self .sender .lock() .unwrap() .send(TelemetryMessage::AddHandler(Box::new(handler))) { util::log_error(format!("Failed to add handler: {}", err)); }; self } fn add_cockpit(&self, cockpit: Cockpit<L>) -> &Self { if let Err(err) = self .sender .lock() .unwrap() .send(TelemetryMessage::AddCockpit(cockpit)) { util::log_error(format!("Failed to add cockpit: {}", err)); }; self } fn add_panel_to_cockpit(&self, cockpit_name: String, panel: Panel<L>) -> &Self { if let Err(err) = self .sender .lock() .unwrap() .send(TelemetryMessage::AddPanel { cockpit_name, panel, }) { util::log_error(format!("Failed to add panel to cockpit: {}", err)); }; self } } /// Something that has a title and a description /// /// This is mostly useful for snapshots. When a `Snapshot` /// is taken there is usually a parameter `descriptive` /// that determines whether title and description should /// be part of a `Snapshot`. See also `PutsSnapshot`. pub trait Descriptive { fn title(&self) -> Option<&str> { None } fn description(&self) -> Option<&str> { None } } /// Implementors are able to write their current data into given `Snapshot`. /// /// Guidelines for writing snapshots: /// /// * A `PutsSnapshot` that has a name should create a new sub snapshot /// and add its values there /// /// * A `PutsSnapshot` that does not have a name should add its values /// directly to the given snapshot /// /// * When `descriptive` is set to `true` `PutsSnapshot` should put /// its `title` and `description` into the same `Snapshot` it put /// its values(exception: instruments) thereby not overwriting already /// existing descriptions so that the more general top level ones survive. /// /// * When `descriptive` is set to `true` on an instrument the instrument /// should put its description into the snapshot it got passed therby adding the /// suffixes "_title" and "_description" to its name. /// /// Implementors of this trait can be added to almost all components via /// the `add_snapshooter` method which is also defined on trait /// `AggregatesProcessors`. pub trait PutsSnapshot: Send + 'static { /// Puts the current snapshot values into the given `Snapshot` thereby /// following the guidelines of `PutsSnapshot`. fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool); }