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_getPayloadV5`
50    pub(crate) get_payload_v5: Histogram,
51    /// Latency for `engine_getPayloadBodiesByRangeV1`
52    pub(crate) get_payload_bodies_by_range_v1: Histogram,
53    /// Latency for `engine_getPayloadBodiesByHashV1`
54    pub(crate) get_payload_bodies_by_hash_v1: Histogram,
55    /// Latency for `engine_getBlobsV1`
56    pub(crate) get_blobs_v1: Histogram,
57    /// Latency for `engine_getBlobsV2`
58    pub(crate) get_blobs_v2: Histogram,
59}
60
61/// Metrics for engine API forkchoiceUpdated responses.
62#[derive(Metrics)]
63#[metrics(scope = "engine.rpc")]
64pub(crate) struct ForkchoiceUpdatedResponseMetrics {
65    /// The total count of forkchoice updated messages received.
66    pub(crate) forkchoice_updated_messages: Counter,
67    /// The total count of forkchoice updated messages that we responded to with
68    /// [`Invalid`](alloy_rpc_types_engine::PayloadStatusEnum#Invalid).
69    pub(crate) forkchoice_updated_invalid: Counter,
70    /// The total count of forkchoice updated messages that we responded to with
71    /// [`Valid`](alloy_rpc_types_engine::PayloadStatusEnum#Valid).
72    pub(crate) forkchoice_updated_valid: Counter,
73    /// The total count of forkchoice updated messages that we responded to with
74    /// [`Syncing`](alloy_rpc_types_engine::PayloadStatusEnum#Syncing).
75    pub(crate) forkchoice_updated_syncing: Counter,
76    /// The total count of forkchoice updated messages that we responded to with
77    /// [`Accepted`](alloy_rpc_types_engine::PayloadStatusEnum#Accepted).
78    pub(crate) forkchoice_updated_accepted: Counter,
79    /// The total count of forkchoice updated messages that were unsuccessful, i.e. we responded
80    /// with an error type that is not a [`PayloadStatusEnum`].
81    pub(crate) forkchoice_updated_error: Counter,
82}
83
84/// Metrics for engine API newPayload responses.
85#[derive(Metrics)]
86#[metrics(scope = "engine.rpc")]
87pub(crate) struct NewPayloadStatusResponseMetrics {
88    /// The total count of new payload messages received.
89    pub(crate) new_payload_messages: Counter,
90    /// The total count of new payload messages that we responded to with
91    /// [Invalid](alloy_rpc_types_engine::PayloadStatusEnum#Invalid).
92    pub(crate) new_payload_invalid: Counter,
93    /// The total count of new payload messages that we responded to with
94    /// [Valid](alloy_rpc_types_engine::PayloadStatusEnum#Valid).
95    pub(crate) new_payload_valid: Counter,
96    /// The total count of new payload messages that we responded to with
97    /// [Syncing](alloy_rpc_types_engine::PayloadStatusEnum#Syncing).
98    pub(crate) new_payload_syncing: Counter,
99    /// The total count of new payload messages that we responded to with
100    /// [Accepted](alloy_rpc_types_engine::PayloadStatusEnum#Accepted).
101    pub(crate) new_payload_accepted: Counter,
102    /// The total count of new payload messages that were unsuccessful, i.e. we responded with an
103    /// error type that is not a [`PayloadStatusEnum`].
104    pub(crate) new_payload_error: Counter,
105    /// The total gas of valid new payload messages received.
106    pub(crate) new_payload_total_gas: Histogram,
107    /// The gas per second of valid new payload messages received.
108    pub(crate) new_payload_gas_per_second: Histogram,
109    /// Latency for the last `engine_newPayloadV*` call
110    pub(crate) new_payload_last: Gauge,
111}
112
113#[derive(Metrics)]
114#[metrics(scope = "engine.rpc.blobs")]
115pub(crate) struct BlobMetrics {
116    /// Count of blobs successfully retrieved
117    pub(crate) blob_count: Counter,
118    /// Count of blob misses
119    pub(crate) blob_misses: Counter,
120    /// Number of blobs requested via getBlobsV2
121    pub(crate) get_blobs_requests_blobs_total: Counter,
122    /// Number of blobs requested via getBlobsV2 that are present in the blobpool
123    pub(crate) get_blobs_requests_blobs_in_blobpool_total: Counter,
124    /// Number of times getBlobsV2 responded with “hit”
125    pub(crate) get_blobs_requests_success_total: Counter,
126    /// Number of times getBlobsV2 responded with “miss”
127    pub(crate) get_blobs_requests_failure_total: Counter,
128}
129
130impl NewPayloadStatusResponseMetrics {
131    /// Increment the newPayload counter based on the given rpc result
132    pub(crate) fn update_response_metrics(
133        &self,
134        result: &Result<PayloadStatus, EngineApiError>,
135        gas_used: u64,
136        time: Duration,
137    ) {
138        self.new_payload_last.set(time);
139        match result {
140            Ok(status) => match status.status {
141                PayloadStatusEnum::Valid => {
142                    self.new_payload_valid.increment(1);
143                    self.new_payload_total_gas.record(gas_used as f64);
144                    self.new_payload_gas_per_second.record(gas_used as f64 / time.as_secs_f64());
145                }
146                PayloadStatusEnum::Syncing => self.new_payload_syncing.increment(1),
147                PayloadStatusEnum::Accepted => self.new_payload_accepted.increment(1),
148                PayloadStatusEnum::Invalid { .. } => self.new_payload_invalid.increment(1),
149            },
150            Err(_) => self.new_payload_error.increment(1),
151        }
152        self.new_payload_messages.increment(1);
153    }
154}
155
156impl ForkchoiceUpdatedResponseMetrics {
157    /// Increment the forkchoiceUpdated counter based on the given rpc result
158    pub(crate) fn update_response_metrics(
159        &self,
160        result: &Result<ForkchoiceUpdated, EngineApiError>,
161    ) {
162        match result {
163            Ok(status) => match status.payload_status.status {
164                PayloadStatusEnum::Valid => self.forkchoice_updated_valid.increment(1),
165                PayloadStatusEnum::Syncing => self.forkchoice_updated_syncing.increment(1),
166                PayloadStatusEnum::Accepted => self.forkchoice_updated_accepted.increment(1),
167                PayloadStatusEnum::Invalid { .. } => self.forkchoice_updated_invalid.increment(1),
168            },
169            Err(_) => self.forkchoice_updated_error.increment(1),
170        }
171        self.forkchoice_updated_messages.increment(1);
172    }
173}