1use std::time::Duration;
23use crate::EngineApiError;
4use alloy_rpc_types_engine::{ForkchoiceUpdated, PayloadStatus, PayloadStatusEnum};
5use metrics::{Counter, Gauge, Histogram};
6use reth_metrics::Metrics;
78/// All beacon consensus engine metrics
9#[derive(Default)]
10pub(crate) struct EngineApiMetrics {
11/// Engine API latency metrics
12pub(crate) latency: EngineApiLatencyMetrics,
13/// Engine API forkchoiceUpdated response type metrics
14pub(crate) fcu_response: ForkchoiceUpdatedResponseMetrics,
15/// Engine API newPayload response type metrics
16pub(crate) new_payload_response: NewPayloadStatusResponseMetrics,
17/// Blob-related metrics
18pub(crate) blob_metrics: BlobMetrics,
19}
2021/// Beacon consensus engine latency metrics.
22#[derive(Metrics)]
23#[metrics(scope = "engine.rpc")]
24pub(crate) struct EngineApiLatencyMetrics {
25/// Latency for `engine_newPayloadV1`
26pub(crate) new_payload_v1: Histogram,
27/// Latency for `engine_newPayloadV2`
28pub(crate) new_payload_v2: Histogram,
29/// Latency for `engine_newPayloadV3`
30pub(crate) new_payload_v3: Histogram,
31/// Latency for `engine_newPayloadV4`
32pub(crate) new_payload_v4: Histogram,
33/// Latency for `engine_forkchoiceUpdatedV1`
34pub(crate) fork_choice_updated_v1: Histogram,
35/// Latency for `engine_forkchoiceUpdatedV2`
36pub(crate) fork_choice_updated_v2: Histogram,
37/// Latency for `engine_forkchoiceUpdatedV3`
38pub(crate) fork_choice_updated_v3: Histogram,
39/// Time diff between `engine_newPayloadV*` and the next FCU
40pub(crate) new_payload_forkchoice_updated_time_diff: Histogram,
41/// Latency for `engine_getPayloadV1`
42pub(crate) get_payload_v1: Histogram,
43/// Latency for `engine_getPayloadV2`
44pub(crate) get_payload_v2: Histogram,
45/// Latency for `engine_getPayloadV3`
46pub(crate) get_payload_v3: Histogram,
47/// Latency for `engine_getPayloadV4`
48pub(crate) get_payload_v4: Histogram,
49/// Latency for `engine_getPayloadBodiesByRangeV1`
50pub(crate) get_payload_bodies_by_range_v1: Histogram,
51/// Latency for `engine_getPayloadBodiesByHashV1`
52pub(crate) get_payload_bodies_by_hash_v1: Histogram,
53/// Latency for `engine_getBlobsV1`
54pub(crate) get_blobs_v1: Histogram,
55}
5657/// 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.
62pub(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).
65pub(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).
68pub(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).
71pub(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).
74pub(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`].
77pub(crate) forkchoice_updated_error: Counter,
78}
7980/// 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.
85pub(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).
88pub(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).
91pub(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).
94pub(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).
97pub(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`].
100pub(crate) new_payload_error: Counter,
101/// The total gas of valid new payload messages received.
102pub(crate) new_payload_total_gas: Histogram,
103/// The gas per second of valid new payload messages received.
104pub(crate) new_payload_gas_per_second: Histogram,
105/// Latency for the last `engine_newPayloadV*` call
106pub(crate) new_payload_last: Gauge,
107}
108109#[derive(Metrics)]
110#[metrics(scope = "engine.rpc.blobs")]
111pub(crate) struct BlobMetrics {
112/// Count of blobs successfully retrieved
113pub(crate) blob_count: Counter,
114/// Count of blob misses
115pub(crate) blob_misses: Counter,
116}
117118impl NewPayloadStatusResponseMetrics {
119/// Increment the newPayload counter based on the given rpc result
120pub(crate) fn update_response_metrics(
121&self,
122 result: &Result<PayloadStatus, EngineApiError>,
123 gas_used: u64,
124 time: Duration,
125 ) {
126self.new_payload_last.set(time);
127match result {
128Ok(status) => match status.status {
129 PayloadStatusEnum::Valid => {
130self.new_payload_valid.increment(1);
131self.new_payload_total_gas.record(gas_usedas f64);
132self.new_payload_gas_per_second.record(gas_usedas 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 },
138Err(_) => self.new_payload_error.increment(1),
139 }
140self.new_payload_messages.increment(1);
141 }
142}
143144impl ForkchoiceUpdatedResponseMetrics {
145/// Increment the forkchoiceUpdated counter based on the given rpc result
146pub(crate) fn update_response_metrics(
147&self,
148 result: &Result<ForkchoiceUpdated, EngineApiError>,
149 ) {
150match result {
151Ok(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 },
157Err(_) => self.forkchoice_updated_error.increment(1),
158 }
159self.forkchoice_updated_messages.increment(1);
160 }
161}