reth_network/metrics.rs
1use metrics::Histogram;
2use reth_eth_wire::DisconnectReason;
3use reth_ethereum_primitives::TxType;
4use reth_metrics::{
5 metrics::{Counter, Gauge},
6 Metrics,
7};
8
9/// Scope for monitoring transactions sent from the manager to the tx manager
10pub(crate) const NETWORK_POOL_TRANSACTIONS_SCOPE: &str = "network.pool.transactions";
11
12/// Metrics for the entire network, handled by `NetworkManager`
13#[derive(Metrics)]
14#[metrics(scope = "network")]
15pub struct NetworkMetrics {
16 /// Number of currently connected peers
17 pub(crate) connected_peers: Gauge,
18
19 /// Number of currently backed off peers
20 pub(crate) backed_off_peers: Gauge,
21
22 /// Number of peers known to the node
23 pub(crate) tracked_peers: Gauge,
24
25 /// Cumulative number of failures of pending sessions
26 pub(crate) pending_session_failures: Counter,
27
28 /// Total number of sessions closed
29 pub(crate) closed_sessions: Counter,
30
31 /// Number of active incoming connections
32 pub(crate) incoming_connections: Gauge,
33
34 /// Number of active outgoing connections
35 pub(crate) outgoing_connections: Gauge,
36
37 /// Number of currently pending outgoing connections
38 pub(crate) pending_outgoing_connections: Gauge,
39
40 /// Total number of pending connections, incoming and outgoing.
41 pub(crate) total_pending_connections: Gauge,
42
43 /// Total Number of incoming connections handled
44 pub(crate) total_incoming_connections: Counter,
45
46 /// Total Number of outgoing connections established
47 pub(crate) total_outgoing_connections: Counter,
48
49 /// Number of invalid/malformed messages received from peers
50 pub(crate) invalid_messages_received: Counter,
51
52 /// Number of Eth Requests dropped due to channel being at full capacity
53 pub(crate) total_dropped_eth_requests_at_full_capacity: Counter,
54
55 /* ================ POLL DURATION ================ */
56
57 /* -- Total poll duration of `NetworksManager` future -- */
58 /// Duration in seconds of call to
59 /// [`NetworkManager`](crate::NetworkManager)'s poll function.
60 ///
61 /// True duration of this call, should be sum of the accumulated durations of calling nested
62 // items.
63 pub(crate) duration_poll_network_manager: Gauge,
64
65 /* -- Poll duration of items nested in `NetworkManager` future -- */
66 /// Time spent streaming messages sent over the [`NetworkHandle`](crate::NetworkHandle), which
67 /// can be cloned and shared via [`NetworkManager::handle`](crate::NetworkManager::handle), in
68 /// one call to poll the [`NetworkManager`](crate::NetworkManager) future. At least
69 /// [`TransactionsManager`](crate::transactions::TransactionsManager) holds this handle.
70 ///
71 /// Duration in seconds.
72 pub(crate) acc_duration_poll_network_handle: Gauge,
73 /// Time spent polling [`Swarm`](crate::swarm::Swarm), in one call to poll the
74 /// [`NetworkManager`](crate::NetworkManager) future.
75 ///
76 /// Duration in seconds.
77 pub(crate) acc_duration_poll_swarm: Gauge,
78}
79
80/// Metrics for `SessionManager`
81#[derive(Metrics)]
82#[metrics(scope = "network")]
83pub struct SessionManagerMetrics {
84 /// Number of successful outgoing dial attempts.
85 pub(crate) total_dial_successes: Counter,
86 /// Number of dropped outgoing peer messages.
87 pub(crate) total_outgoing_peer_messages_dropped: Counter,
88 /// Number of queued outgoing messages
89 pub(crate) queued_outgoing_messages: Gauge,
90}
91
92/// Metrics for the [`TransactionsManager`](crate::transactions::TransactionsManager).
93#[derive(Metrics)]
94#[metrics(scope = "network")]
95pub struct TransactionsManagerMetrics {
96 /* ================ BROADCAST ================ */
97 /// Total number of propagated transactions
98 pub(crate) propagated_transactions: Counter,
99 /// Total number of reported bad transactions
100 pub(crate) reported_bad_transactions: Counter,
101
102 /* -- Freq txns already marked as seen by peer -- */
103 /// Total number of messages from a peer, announcing transactions that have already been
104 /// marked as seen by that peer.
105 pub(crate) messages_with_hashes_already_seen_by_peer: Counter,
106 /// Total number of messages from a peer, with transaction that have already been marked as
107 /// seen by that peer.
108 pub(crate) messages_with_transactions_already_seen_by_peer: Counter,
109 /// Total number of occurrences, of a peer announcing a transaction that has already been
110 /// marked as seen by that peer.
111 pub(crate) occurrences_hash_already_seen_by_peer: Counter,
112 /// Total number of times a transaction is seen from a peer, that has already been marked as
113 /// seen by that peer.
114 pub(crate) occurrences_of_transaction_already_seen_by_peer: Counter,
115
116 /* -- Freq txns already in pool -- */
117 /// Total number of times a hash is announced that is already in the local pool.
118 pub(crate) occurrences_hashes_already_in_pool: Counter,
119 /// Total number of times a transaction is sent that is already in the local pool.
120 pub(crate) occurrences_transactions_already_in_pool: Counter,
121
122 /* ================ POOL IMPORTS ================ */
123 /// Number of transactions about to be imported into the pool.
124 pub(crate) pending_pool_imports: Gauge,
125 /// Total number of bad imports, imports that fail because the transaction is badly formed
126 /// (i.e. have no chance of passing validation, unlike imports that fail due to e.g. nonce
127 /// gaps).
128 pub(crate) bad_imports: Counter,
129 /// Number of inflight requests at which the
130 /// [`TransactionPool`](reth_transaction_pool::TransactionPool) is considered to be at
131 /// capacity. Note, this is not a limit to the number of inflight requests, but a health
132 /// measure.
133 pub(crate) capacity_pending_pool_imports: Counter,
134
135 /* ================ POLL DURATION ================ */
136
137 /* -- Total poll duration of `TransactionsManager` future -- */
138 /// Duration in seconds of call to
139 /// [`TransactionsManager`](crate::transactions::TransactionsManager)'s poll function.
140 ///
141 /// Updating metrics could take time, so the true duration of this call could
142 /// be longer than the sum of the accumulated durations of polling nested items.
143 pub(crate) duration_poll_tx_manager: Gauge,
144
145 /* -- Poll duration of items nested in `TransactionsManager` future -- */
146 /// Accumulated time spent streaming session updates and updating peers accordingly, in
147 /// one call to poll the [`TransactionsManager`](crate::transactions::TransactionsManager)
148 /// future.
149 ///
150 /// Duration in seconds.
151 pub(crate) acc_duration_poll_network_events: Gauge,
152 /// Accumulated time spent flushing the queue of batched pending pool imports into pool, in
153 /// one call to poll the [`TransactionsManager`](crate::transactions::TransactionsManager)
154 /// future.
155 ///
156 /// Duration in seconds.
157 pub(crate) acc_duration_poll_pending_pool_imports: Gauge,
158 /// Accumulated time spent streaming transaction and announcement broadcast, queueing for
159 /// pool import or requesting respectively, in one call to poll the
160 /// [`TransactionsManager`](crate::transactions::TransactionsManager) future.
161 ///
162 /// Duration in seconds.
163 pub(crate) acc_duration_poll_transaction_events: Gauge,
164 /// Accumulated time spent streaming fetch events, queueing for pool import on successful
165 /// fetch, in one call to poll the
166 /// [`TransactionsManager`](crate::transactions::TransactionsManager) future.
167 ///
168 /// Duration in seconds.
169 pub(crate) acc_duration_poll_fetch_events: Gauge,
170 /// Accumulated time spent streaming and propagating transactions that were successfully
171 /// imported into the pool, in one call to poll the
172 /// [`TransactionsManager`](crate::transactions::TransactionsManager) future.
173 ///
174 /// Duration in seconds.
175 pub(crate) acc_duration_poll_imported_transactions: Gauge,
176 /// Accumulated time spent assembling and sending requests for hashes fetching pending, in
177 /// one call to poll the [`TransactionsManager`](crate::transactions::TransactionsManager)
178 /// future.
179 ///
180 /// Duration in seconds.
181 pub(crate) acc_duration_fetch_pending_hashes: Gauge,
182 /// Accumulated time spent streaming commands and propagating, fetching and serving
183 /// transactions accordingly, in one call to poll the
184 /// [`TransactionsManager`](crate::transactions::TransactionsManager) future.
185 ///
186 /// Duration in seconds.
187 pub(crate) acc_duration_poll_commands: Gauge,
188}
189
190/// Metrics for the [`TransactionsManager`](crate::transactions::TransactionsManager).
191#[derive(Metrics)]
192#[metrics(scope = "network")]
193pub struct TransactionFetcherMetrics {
194 /// Currently active outgoing [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)
195 /// requests.
196 pub(crate) inflight_transaction_requests: Gauge,
197 /// Number of inflight requests at which the
198 /// [`TransactionFetcher`](crate::transactions::TransactionFetcher) is considered to be at
199 /// capacity. Note, this is not a limit to the number of inflight requests, but a health
200 /// measure.
201 pub(crate) capacity_inflight_requests: Counter,
202 /// Hashes in currently active outgoing
203 /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) requests.
204 pub(crate) hashes_inflight_transaction_requests: Gauge,
205 /// How often we failed to send a request to the peer because the channel was full.
206 pub(crate) egress_peer_channel_full: Counter,
207 /// Total number of hashes pending fetch.
208 pub(crate) hashes_pending_fetch: Gauge,
209 /// Total number of fetched transactions.
210 pub(crate) fetched_transactions: Counter,
211 /// Total number of transactions that were received in
212 /// [`PooledTransactions`](reth_eth_wire::PooledTransactions) responses, that weren't
213 /// requested.
214 pub(crate) unsolicited_transactions: Counter,
215 /* ================ SEARCH DURATION ================ */
216 /// Time spent searching for an idle peer in call to
217 /// [`TransactionFetcher::find_any_idle_fallback_peer_for_any_pending_hash`](crate::transactions::TransactionFetcher::find_any_idle_fallback_peer_for_any_pending_hash).
218 ///
219 /// Duration in seconds.
220 pub(crate) duration_find_idle_fallback_peer_for_any_pending_hash: Gauge,
221
222 /// Time spent searching for hashes pending fetch, announced by a given peer in
223 /// [`TransactionFetcher::fill_request_from_hashes_pending_fetch`](crate::transactions::TransactionFetcher::fill_request_from_hashes_pending_fetch).
224 ///
225 /// Duration in seconds.
226 pub(crate) duration_fill_request_from_hashes_pending_fetch: Gauge,
227}
228
229/// Measures the duration of executing the given code block. The duration is added to the given
230/// accumulator value passed as a mutable reference.
231#[macro_export]
232macro_rules! duration_metered_exec {
233 ($code:expr, $acc:expr) => {{
234 let start = std::time::Instant::now();
235
236 let res = $code;
237
238 $acc += start.elapsed();
239
240 res
241 }};
242}
243
244/// Metrics for Disconnection types
245///
246/// These are just counters, and ideally we would implement these metrics on a peer-by-peer basis,
247/// in that we do not double-count peers for `TooManyPeers` if we make an outgoing connection and
248/// get disconnected twice
249#[derive(Metrics)]
250#[metrics(scope = "network")]
251pub struct DisconnectMetrics {
252 /// Number of peer disconnects due to `DisconnectRequested` (0x00)
253 pub(crate) disconnect_requested: Counter,
254
255 /// Number of peer disconnects due to `TcpSubsystemError` (0x01)
256 pub(crate) tcp_subsystem_error: Counter,
257
258 /// Number of peer disconnects due to `ProtocolBreach` (0x02)
259 pub(crate) protocol_breach: Counter,
260
261 /// Number of peer disconnects due to `UselessPeer` (0x03)
262 pub(crate) useless_peer: Counter,
263
264 /// Number of peer disconnects due to `TooManyPeers` (0x04)
265 pub(crate) too_many_peers: Counter,
266
267 /// Number of peer disconnects due to `AlreadyConnected` (0x05)
268 pub(crate) already_connected: Counter,
269
270 /// Number of peer disconnects due to `IncompatibleP2PProtocolVersion` (0x06)
271 pub(crate) incompatible: Counter,
272
273 /// Number of peer disconnects due to `NullNodeIdentity` (0x07)
274 pub(crate) null_node_identity: Counter,
275
276 /// Number of peer disconnects due to `ClientQuitting` (0x08)
277 pub(crate) client_quitting: Counter,
278
279 /// Number of peer disconnects due to `UnexpectedHandshakeIdentity` (0x09)
280 pub(crate) unexpected_identity: Counter,
281
282 /// Number of peer disconnects due to `ConnectedToSelf` (0x0a)
283 pub(crate) connected_to_self: Counter,
284
285 /// Number of peer disconnects due to `PingTimeout` (0x0b)
286 pub(crate) ping_timeout: Counter,
287
288 /// Number of peer disconnects due to `SubprotocolSpecific` (0x10)
289 pub(crate) subprotocol_specific: Counter,
290}
291
292impl DisconnectMetrics {
293 /// Increments the proper counter for the given disconnect reason
294 pub(crate) fn increment(&self, reason: DisconnectReason) {
295 match reason {
296 DisconnectReason::DisconnectRequested => self.disconnect_requested.increment(1),
297 DisconnectReason::TcpSubsystemError => self.tcp_subsystem_error.increment(1),
298 DisconnectReason::ProtocolBreach => self.protocol_breach.increment(1),
299 DisconnectReason::UselessPeer => self.useless_peer.increment(1),
300 DisconnectReason::TooManyPeers => self.too_many_peers.increment(1),
301 DisconnectReason::AlreadyConnected => self.already_connected.increment(1),
302 DisconnectReason::IncompatibleP2PProtocolVersion => self.incompatible.increment(1),
303 DisconnectReason::NullNodeIdentity => self.null_node_identity.increment(1),
304 DisconnectReason::ClientQuitting => self.client_quitting.increment(1),
305 DisconnectReason::UnexpectedHandshakeIdentity => self.unexpected_identity.increment(1),
306 DisconnectReason::ConnectedToSelf => self.connected_to_self.increment(1),
307 DisconnectReason::PingTimeout => self.ping_timeout.increment(1),
308 DisconnectReason::SubprotocolSpecific => self.subprotocol_specific.increment(1),
309 }
310 }
311}
312
313/// Metrics for the `EthRequestHandler`
314#[derive(Metrics)]
315#[metrics(scope = "network")]
316pub struct EthRequestHandlerMetrics {
317 /// Number of `GetBlockHeaders` requests received
318 pub(crate) eth_headers_requests_received_total: Counter,
319
320 /// Number of `GetReceipts` requests received
321 pub(crate) eth_receipts_requests_received_total: Counter,
322
323 /// Number of `GetBlockBodies` requests received
324 pub(crate) eth_bodies_requests_received_total: Counter,
325
326 /// Number of `GetNodeData` requests received
327 pub(crate) eth_node_data_requests_received_total: Counter,
328
329 /// Duration in seconds of call to poll
330 /// [`EthRequestHandler`](crate::eth_requests::EthRequestHandler).
331 pub(crate) acc_duration_poll_eth_req_handler: Gauge,
332}
333
334/// Eth67 announcement metrics, track entries by `TxType`
335#[derive(Metrics)]
336#[metrics(scope = "network.transaction_fetcher")]
337pub struct AnnouncedTxTypesMetrics {
338 /// Histogram for tracking frequency of legacy transaction type
339 pub(crate) legacy: Histogram,
340
341 /// Histogram for tracking frequency of EIP-2930 transaction type
342 pub(crate) eip2930: Histogram,
343
344 /// Histogram for tracking frequency of EIP-1559 transaction type
345 pub(crate) eip1559: Histogram,
346
347 /// Histogram for tracking frequency of EIP-4844 transaction type
348 pub(crate) eip4844: Histogram,
349
350 /// Histogram for tracking frequency of EIP-7702 transaction type
351 pub(crate) eip7702: Histogram,
352}
353
354/// Counts the number of transactions by their type in a block or collection.
355///
356/// This struct keeps track of the count of different transaction types
357/// as defined by various Ethereum Improvement Proposals (EIPs).
358#[derive(Debug, Default)]
359pub struct TxTypesCounter {
360 /// Count of legacy transactions (pre-EIP-2718).
361 pub(crate) legacy: usize,
362
363 /// Count of transactions conforming to EIP-2930 (Optional access lists).
364 pub(crate) eip2930: usize,
365
366 /// Count of transactions conforming to EIP-1559 (Fee market change).
367 pub(crate) eip1559: usize,
368
369 /// Count of transactions conforming to EIP-4844 (Shard Blob Transactions).
370 pub(crate) eip4844: usize,
371
372 /// Count of transactions conforming to EIP-7702 (Restricted Storage Windows).
373 pub(crate) eip7702: usize,
374}
375
376impl TxTypesCounter {
377 pub(crate) const fn increase_by_tx_type(&mut self, tx_type: TxType) {
378 match tx_type {
379 TxType::Legacy => {
380 self.legacy += 1;
381 }
382 TxType::Eip2930 => {
383 self.eip2930 += 1;
384 }
385 TxType::Eip1559 => {
386 self.eip1559 += 1;
387 }
388 TxType::Eip4844 => {
389 self.eip4844 += 1;
390 }
391 TxType::Eip7702 => {
392 self.eip7702 += 1;
393 }
394 }
395 }
396}
397
398impl AnnouncedTxTypesMetrics {
399 /// Update metrics during announcement validation, by examining each announcement entry based on
400 /// `TxType`
401 pub(crate) fn update_eth68_announcement_metrics(&self, tx_types_counter: TxTypesCounter) {
402 self.legacy.record(tx_types_counter.legacy as f64);
403 self.eip2930.record(tx_types_counter.eip2930 as f64);
404 self.eip1559.record(tx_types_counter.eip1559 as f64);
405 self.eip4844.record(tx_types_counter.eip4844 as f64);
406 self.eip7702.record(tx_types_counter.eip7702 as f64);
407 }
408}