Skip to main content

elfo_telemeter/
lib.rs

1//! Interaction with the `metrics` crate. [Configuration].
2//!
3//! Records metrics in the OpenMetrics exposition format.
4//!
5//! Note that push gateways aren't supported, histogram buckets overrides
6//! don't work, only summaries.
7//!
8//! All metrics include information about the actor, where they were produced.
9//! Such information is added as labels. By default, only the `actor_group`
10//! label is added, but it's possible to provide `actor_key` on a group basis.
11//! It's useful, if a group has few actors inside.
12//!
13//! [Configuration]: config::Config
14
15#![cfg_attr(docsrs, feature(doc_cfg))]
16
17use std::sync::Arc;
18
19use tracing::error;
20
21use elfo_core::Blueprint;
22
23use self::{recorder::Recorder, storage::Storage};
24
25pub mod config;
26pub mod protocol;
27
28mod actor;
29mod hyper;
30mod metrics;
31mod recorder;
32mod render;
33mod stats;
34mod storage;
35
36#[cfg(feature = "unstable")]
37mod allocator;
38
39#[cfg(feature = "unstable")]
40pub use allocator::AllocatorStats;
41
42/// Installs a global metric recorder and returns a group to handle metrics.
43///
44/// # Example
45/// ```
46/// # use elfo_core as elfo;
47///
48/// let topology = elfo::Topology::empty();
49/// let telemeters = topology.local("telemeters");
50///
51/// // Usually, it's `elfo::batteries::telemeter::init`.
52/// telemeters.mount(elfo_telemeter::init());
53/// ```
54pub fn init() -> Blueprint {
55    let storage = Arc::new(Storage::default());
56    let recorder = Recorder::new(storage.clone());
57    let blueprint = actor::new(storage);
58
59    match ::metrics::set_boxed_recorder(Box::new(recorder)) {
60        Ok(_) => stats::register(),
61        Err(err) => error!(error = %err, "failed to set a metric recorder"),
62    }
63
64    blueprint
65}