fuel_core_metrics/
importer.rs

1use crate::{
2    buckets::{
3        buckets,
4        Buckets,
5    },
6    global_registry,
7};
8use prometheus_client::metrics::{
9    gauge::Gauge,
10    histogram::Histogram,
11};
12use std::sync::{
13    atomic::AtomicU64,
14    OnceLock,
15};
16
17pub struct ImporterMetrics {
18    pub block_height: Gauge,
19    pub latest_block_import_timestamp: Gauge<f64, AtomicU64>,
20    pub execute_and_commit_duration: Histogram,
21    pub gas_per_block: Gauge,
22    pub fee_per_block: Gauge,
23    pub transactions_per_block: Gauge,
24    pub gas_price: Gauge,
25}
26
27impl Default for ImporterMetrics {
28    fn default() -> Self {
29        let block_height_gauge = Gauge::default();
30        let latest_block_import_ms = Gauge::default();
31        let execute_and_commit_duration = Histogram::new(buckets(Buckets::Timing));
32        let gas_per_block = Gauge::default();
33        let fee_per_block = Gauge::default();
34        let transactions_per_block = Gauge::default();
35        let gas_price = Gauge::default();
36
37        let mut registry = global_registry().registry.lock();
38        registry.register(
39            "importer_block_height",
40            "the current height of the chain",
41            block_height_gauge.clone(),
42        );
43
44        registry.register(
45            "importer_latest_block_commit_timestamp_s",
46            "A timestamp of when the current block was imported",
47            latest_block_import_ms.clone(),
48        );
49
50        registry.register(
51            "importer_execute_and_commit_duration_s",
52            "Records the duration time of executing and committing a block",
53            execute_and_commit_duration.clone(),
54        );
55
56        registry.register(
57            "importer_gas_per_block",
58            "The total gas used in a block",
59            gas_per_block.clone(),
60        );
61
62        registry.register(
63            "importer_fee_per_block_gwei",
64            "The total fee (gwei) paid by transactions in a block",
65            fee_per_block.clone(),
66        );
67
68        registry.register(
69            "importer_transactions_per_block",
70            "The total number of transactions in a block",
71            transactions_per_block.clone(),
72        );
73
74        registry.register(
75            "importer_gas_price_for_block",
76            "The gas prices used in a block",
77            gas_price.clone(),
78        );
79
80        Self {
81            block_height: block_height_gauge,
82            latest_block_import_timestamp: latest_block_import_ms,
83            execute_and_commit_duration,
84            gas_per_block,
85            fee_per_block,
86            transactions_per_block,
87            gas_price,
88        }
89    }
90}
91
92// Setup a global static for accessing importer metrics
93static IMPORTER_METRICS: OnceLock<ImporterMetrics> = OnceLock::new();
94
95pub fn importer_metrics() -> &'static ImporterMetrics {
96    IMPORTER_METRICS.get_or_init(ImporterMetrics::default)
97}