kona_engine/metrics/
mod.rs

1//! Metrics for the engine
2
3/// Container for metrics.
4#[derive(Debug, Clone)]
5pub struct Metrics;
6
7impl Metrics {
8    /// Identifier for the gauge that tracks block labels.
9    pub const BLOCK_LABELS: &str = "kona_node_block_labels";
10    /// Unsafe block label.
11    pub const UNSAFE_BLOCK_LABEL: &str = "unsafe";
12    /// Cross-unsafe block label.
13    pub const CROSS_UNSAFE_BLOCK_LABEL: &str = "cross-unsafe";
14    /// Local-safe block label.
15    pub const LOCAL_SAFE_BLOCK_LABEL: &str = "local-safe";
16    /// Safe block label.
17    pub const SAFE_BLOCK_LABEL: &str = "safe";
18    /// Finalized block label.
19    pub const FINALIZED_BLOCK_LABEL: &str = "finalized";
20
21    /// Identifier for the counter that records engine task counts.
22    pub const ENGINE_TASK_COUNT: &str = "kona_node_engine_task_count";
23    /// Insert task label.
24    pub const INSERT_TASK_LABEL: &str = "insert";
25    /// Consolidate task label.
26    pub const CONSOLIDATE_TASK_LABEL: &str = "consolidate";
27    /// Forkchoice task label.
28    pub const FORKCHOICE_TASK_LABEL: &str = "forkchoice-update";
29    /// Build task label.
30    pub const BUILD_TASK_LABEL: &str = "build";
31    /// Finalize task label.
32    pub const FINALIZE_TASK_LABEL: &str = "finalize";
33
34    /// Identifier for the histogram that tracks engine method call time.
35    pub const ENGINE_METHOD_REQUEST_DURATION: &str = "kona_node_engine_method_request_duration";
36    /// `engine_forkchoiceUpdatedV<N>` label
37    pub const FORKCHOICE_UPDATE_METHOD: &str = "engine_forkchoiceUpdated";
38    /// `engine_newPayloadV<N>` label.
39    pub const NEW_PAYLOAD_METHOD: &str = "engine_newPayload";
40    /// `engine_getPayloadV<N>` label.
41    pub const GET_PAYLOAD_METHOD: &str = "engine_getPayload";
42
43    /// Identifier for the counter that tracks the number of times the engine has been reset.
44    pub const ENGINE_RESET_COUNT: &str = "kona_node_engine_reset_count";
45
46    /// Initializes metrics for the engine.
47    ///
48    /// This does two things:
49    /// * Describes various metrics.
50    /// * Initializes metrics to 0 so they can be queried immediately.
51    #[cfg(feature = "metrics")]
52    pub fn init() {
53        Self::describe();
54        Self::zero();
55    }
56
57    /// Describes metrics used in [`kona_engine`][crate].
58    #[cfg(feature = "metrics")]
59    pub fn describe() {
60        // Block labels
61        metrics::describe_gauge!(Self::BLOCK_LABELS, "Blockchain head labels");
62
63        // Engine task counts
64        metrics::describe_counter!(Self::ENGINE_TASK_COUNT, "Engine task counts");
65
66        // Engine method request duration histogram
67        metrics::describe_histogram!(
68            Self::ENGINE_METHOD_REQUEST_DURATION,
69            metrics::Unit::Seconds,
70            "Engine method request duration"
71        );
72
73        // Engine reset counter
74        metrics::describe_counter!(
75            Self::ENGINE_RESET_COUNT,
76            metrics::Unit::Count,
77            "Engine reset count"
78        );
79    }
80
81    /// Initializes metrics to `0` so they can be queried immediately by consumers of prometheus
82    /// metrics.
83    #[cfg(feature = "metrics")]
84    pub fn zero() {
85        // Engine task counts
86        kona_macros::set!(counter, Self::ENGINE_TASK_COUNT, Self::INSERT_TASK_LABEL, 0);
87        kona_macros::set!(counter, Self::ENGINE_TASK_COUNT, Self::CONSOLIDATE_TASK_LABEL, 0);
88        kona_macros::set!(counter, Self::ENGINE_TASK_COUNT, Self::FORKCHOICE_TASK_LABEL, 0);
89        kona_macros::set!(counter, Self::ENGINE_TASK_COUNT, Self::BUILD_TASK_LABEL, 0);
90        kona_macros::set!(counter, Self::ENGINE_TASK_COUNT, Self::FINALIZE_TASK_LABEL, 0);
91
92        // Engine reset count
93        kona_macros::set!(counter, Self::ENGINE_RESET_COUNT, 0);
94    }
95}