fuel_core_metrics/
importer.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
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
use crate::{
    buckets::{
        buckets,
        Buckets,
    },
    global_registry,
};
use prometheus_client::metrics::{
    gauge::Gauge,
    histogram::Histogram,
};
use std::sync::{
    atomic::AtomicU64,
    OnceLock,
};

pub struct ImporterMetrics {
    pub block_height: Gauge,
    pub latest_block_import_timestamp: Gauge<f64, AtomicU64>,
    pub execute_and_commit_duration: Histogram,
    pub gas_per_block: Gauge,
    pub fee_per_block: Gauge,
    pub transactions_per_block: Gauge,
    pub gas_price: Gauge,
}

impl Default for ImporterMetrics {
    fn default() -> Self {
        let block_height_gauge = Gauge::default();
        let latest_block_import_ms = Gauge::default();
        let execute_and_commit_duration = Histogram::new(buckets(Buckets::Timing));
        let gas_per_block = Gauge::default();
        let fee_per_block = Gauge::default();
        let transactions_per_block = Gauge::default();
        let gas_price = Gauge::default();

        let mut registry = global_registry().registry.lock();
        registry.register(
            "importer_block_height",
            "the current height of the chain",
            block_height_gauge.clone(),
        );

        registry.register(
            "importer_latest_block_commit_timestamp_s",
            "A timestamp of when the current block was imported",
            latest_block_import_ms.clone(),
        );

        registry.register(
            "importer_execute_and_commit_duration_s",
            "Records the duration time of executing and committing a block",
            execute_and_commit_duration.clone(),
        );

        registry.register(
            "importer_gas_per_block",
            "The total gas used in a block",
            gas_per_block.clone(),
        );

        registry.register(
            "importer_fee_per_block_gwei",
            "The total fee (gwei) paid by transactions in a block",
            fee_per_block.clone(),
        );

        registry.register(
            "importer_transactions_per_block",
            "The total number of transactions in a block",
            transactions_per_block.clone(),
        );

        registry.register(
            "importer_gas_price_for_block",
            "The gas prices used in a block",
            transactions_per_block.clone(),
        );

        Self {
            block_height: block_height_gauge,
            latest_block_import_timestamp: latest_block_import_ms,
            execute_and_commit_duration,
            gas_per_block,
            fee_per_block,
            transactions_per_block,
            gas_price,
        }
    }
}

// Setup a global static for accessing importer metrics
static IMPORTER_METRICS: OnceLock<ImporterMetrics> = OnceLock::new();

pub fn importer_metrics() -> &'static ImporterMetrics {
    IMPORTER_METRICS.get_or_init(ImporterMetrics::default)
}