fuel_core_metrics/
lib.rs

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
#![deny(clippy::arithmetic_side_effects)]
#![deny(clippy::cast_possible_truncation)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]

use prometheus_client::{
    encoding::text::encode,
    registry::Registry,
};
use std::{
    ops::Deref,
    sync::OnceLock,
};

/// The global register of all metrics.
#[derive(Default)]
pub struct GlobalRegistry {
    // It is okay to use std mutex because we register each metric only one time.
    pub registry: parking_lot::Mutex<Registry>,
}

mod buckets;
pub mod config;
pub mod core_metrics;
pub mod futures;
pub mod graphql_metrics;
pub mod importer;
pub mod p2p_metrics;
pub mod txpool_metrics;

static GLOBAL_REGISTER: OnceLock<GlobalRegistry> = OnceLock::new();

pub fn global_registry() -> &'static GlobalRegistry {
    GLOBAL_REGISTER.get_or_init(GlobalRegistry::default)
}

pub fn encode_metrics() -> Result<String, std::fmt::Error> {
    let mut encoded = String::new();

    // encode the rest of the fuel-core metrics using latest prometheus
    let registry = global_registry().registry.lock();
    encode(&mut encoded, registry.deref())?;

    Ok(encoded)
}