reth_rpc_engine_api/
metrics.rs

1use std::time::Duration;
2
3use crate::EngineApiError;
4use alloy_rpc_types_engine::{ForkchoiceUpdated, PayloadStatus, PayloadStatusEnum};
5use metrics::{Counter, Gauge, Histogram};
6use reth_metrics::Metrics;
7
8/// All beacon consensus engine metrics
9#[derive(Default)]
10pub(crate) struct EngineApiMetrics {
11    /// Engine API latency metrics
12    pub(crate) latency: EngineApiLatencyMetrics,
13    /// Engine API forkchoiceUpdated response type metrics
14    pub(crate) fcu_response: ForkchoiceUpdatedResponseMetrics,
15    /// Engine API newPayload response type metrics
16    pub(crate) new_payload_response: NewPayloadStatusResponseMetrics,
17    /// Blob-related metrics
18    pub(crate) blob_metrics: BlobMetrics,
19}
20
21/// Beacon consensus engine latency metrics.
22#[derive(Metrics)]
23#[metrics(scope = "engine.rpc")]
24pub(crate) struct EngineApiLatencyMetrics {
25    /// Latency for `engine_newPayloadV1`
26    pub(crate) new_payload_v1: Histogram,
27    /// Latency for `engine_newPayloadV2`
28    pub(crate) new_payload_v2: Histogram,
29    /// Latency for `engine_newPayloadV3`
30    pub(crate) new_payload_v3: Histogram,
31    /// Latency for `engine_newPayloadV4`
32    pub(crate) new_payload_v4: Histogram,
33    /// Latency for `engine_forkchoiceUpdatedV1`
34    pub(crate) fork_choice_updated_v1: Histogram,
35    /// Latency for `engine_forkchoiceUpdatedV2`
36    pub(crate) fork_choice_updated_v2: Histogram,
37    /// Latency for `engine_forkchoiceUpdatedV3`
38    pub(crate) fork_choice_updated_v3: Histogram,
39    /// Time diff between `engine_newPayloadV*` and the next FCU
40    pub(crate) new_payload_forkchoice_updated_time_diff: Histogram,
41    /// Latency for `engine_getPayloadV1`
42    pub(crate) get_payload_v1: Histogram,
43    /// Latency for `engine_getPayloadV2`
44    pub(crate) get_payload_v2: Histogram,
45    /// Latency for `engine_getPayloadV3`
46    pub(crate) get_payload_v3: Histogram,
47    /// Latency for `engine_getPayloadV4`
48    pub(crate) get_payload_v4: Histogram,
49    /// Latency for `engine_getPayloadBodiesByRangeV1`
50    pub(crate) get_payload_bodies_by_range_v1: Histogram,
51    /// Latency for `engine_getPayloadBodiesByHashV1`
52    pub(crate) get_payload_bodies_by_hash_v1: Histogram,
53    /// Latency for `engine_getBlobsV1`
54    pub(crate) get_blobs_v1: Histogram,
55}
56
57/// Metrics for engine API forkchoiceUpdated responses.
58#[derive(Metrics)]
59#[metrics(scope = "engine.rpc")]
60pub(crate) struct ForkchoiceUpdatedResponseMetrics {
61    /// The total count of forkchoice updated messages received.
62    pub(crate) forkchoice_updated_messages: Counter,
63    /// The total count of forkchoice updated messages that we responded to with
64    /// [`Invalid`](alloy_rpc_types_engine::PayloadStatusEnum#Invalid).
65    pub(crate) forkchoice_updated_invalid: Counter,
66    /// The total count of forkchoice updated messages that we responded to with
67    /// [`Valid`](alloy_rpc_types_engine::PayloadStatusEnum#Valid).
68    pub(crate) forkchoice_updated_valid: Counter,
69    /// The total count of forkchoice updated messages that we responded to with
70    /// [`Syncing`](alloy_rpc_types_engine::PayloadStatusEnum#Syncing).
71    pub(crate) forkchoice_updated_syncing: Counter,
72    /// The total count of forkchoice updated messages that we responded to with
73    /// [`Accepted`](alloy_rpc_types_engine::PayloadStatusEnum#Accepted).
74    pub(crate) forkchoice_updated_accepted: Counter,
75    /// The total count of forkchoice updated messages that were unsuccessful, i.e. we responded
76    /// with an error type that is not a [`PayloadStatusEnum`].
77    pub(crate) forkchoice_updated_error: Counter,
78}
79
80/// Metrics for engine API newPayload responses.
81#[derive(Metrics)]
82#[metrics(scope = "engine.rpc")]
83pub(crate) struct NewPayloadStatusResponseMetrics {
84    /// The total count of new payload messages received.
85    pub(crate) new_payload_messages: Counter,
86    /// The total count of new payload messages that we responded to with
87    /// [Invalid](alloy_rpc_types_engine::PayloadStatusEnum#Invalid).
88    pub(crate) new_payload_invalid: Counter,
89    /// The total count of new payload messages that we responded to with
90    /// [Valid](alloy_rpc_types_engine::PayloadStatusEnum#Valid).
91    pub(crate) new_payload_valid: Counter,
92    /// The total count of new payload messages that we responded to with
93    /// [Syncing](alloy_rpc_types_engine::PayloadStatusEnum#Syncing).
94    pub(crate) new_payload_syncing: Counter,
95    /// The total count of new payload messages that we responded to with
96    /// [Accepted](alloy_rpc_types_engine::PayloadStatusEnum#Accepted).
97    pub(crate) new_payload_accepted: Counter,
98    /// The total count of new payload messages that were unsuccessful, i.e. we responded with an
99    /// error type that is not a [`PayloadStatusEnum`].
100    pub(crate) new_payload_error: Counter,
101    /// The total gas of valid new payload messages received.
102    pub(crate) new_payload_total_gas: Histogram,
103    /// The gas per second of valid new payload messages received.
104    pub(crate) new_payload_gas_per_second: Histogram,
105    /// Latency for the last `engine_newPayloadV*` call
106    pub(crate) new_payload_last: Gauge,
107}
108
109#[derive(Metrics)]
110#[metrics(scope = "engine.rpc.blobs")]
111pub(crate) struct BlobMetrics {
112    /// Count of blobs successfully retrieved
113    pub(crate) blob_count: Counter,
114    /// Count of blob misses
115    pub(crate) blob_misses: Counter,
116}
117
118impl NewPayloadStatusResponseMetrics {
119    /// Increment the newPayload counter based on the given rpc result
120    pub(crate) fn update_response_metrics(
121        &self,
122        result: &Result<PayloadStatus, EngineApiError>,
123        gas_used: u64,
124        time: Duration,
125    ) {
126        self.new_payload_last.set(time);
127        match result {
128            Ok(status) => match status.status {
129                PayloadStatusEnum::Valid => {
130                    self.new_payload_valid.increment(1);
131                    self.new_payload_total_gas.record(gas_used as f64);
132                    self.new_payload_gas_per_second.record(gas_used as f64 / time.as_secs_f64());
133                }
134                PayloadStatusEnum::Syncing => self.new_payload_syncing.increment(1),
135                PayloadStatusEnum::Accepted => self.new_payload_accepted.increment(1),
136                PayloadStatusEnum::Invalid { .. } => self.new_payload_invalid.increment(1),
137            },
138            Err(_) => self.new_payload_error.increment(1),
139        }
140        self.new_payload_messages.increment(1);
141    }
142}
143
144impl ForkchoiceUpdatedResponseMetrics {
145    /// Increment the forkchoiceUpdated counter based on the given rpc result
146    pub(crate) fn update_response_metrics(
147        &self,
148        result: &Result<ForkchoiceUpdated, EngineApiError>,
149    ) {
150        match result {
151            Ok(status) => match status.payload_status.status {
152                PayloadStatusEnum::Valid => self.forkchoice_updated_valid.increment(1),
153                PayloadStatusEnum::Syncing => self.forkchoice_updated_syncing.increment(1),
154                PayloadStatusEnum::Accepted => self.forkchoice_updated_accepted.increment(1),
155                PayloadStatusEnum::Invalid { .. } => self.forkchoice_updated_invalid.increment(1),
156            },
157            Err(_) => self.forkchoice_updated_error.increment(1),
158        }
159        self.forkchoice_updated_messages.increment(1);
160    }
161}