reth_downloaders/metrics.rs
1use reth_metrics::{
2 metrics::{Counter, Gauge},
3 Metrics,
4};
5use reth_network_p2p::error::DownloadError;
6
7/// Common body downloader metrics.
8///
9/// These metrics will be initialized with the `downloaders.bodies` scope.
10/// ```
11/// use reth_downloaders::metrics::BodyDownloaderMetrics;
12/// use reth_network_p2p::error::DownloadError;
13///
14/// // Initialize metrics.
15/// let metrics = BodyDownloaderMetrics::default();
16/// // Increment `downloaders.bodies.timeout_errors` counter by 1.
17/// metrics.increment_errors(&DownloadError::Timeout);
18/// ```
19#[derive(Clone, Metrics)]
20#[metrics(scope = "downloaders.bodies")]
21pub struct BodyDownloaderMetrics {
22 /// The number of items that were successfully sent to the poller (stage)
23 pub total_flushed: Counter,
24 /// Number of items that were successfully downloaded
25 pub total_downloaded: Counter,
26 /// The number of requests (can contain more than 1 item) currently in-flight.
27 pub in_flight_requests: Gauge,
28 /// The number of responses (can contain more than 1 item) in the internal buffer of the
29 /// downloader.
30 pub buffered_responses: Gauge,
31 /// The number of blocks the internal buffer of the
32 /// downloader.
33 /// These are bodies that have been received, but cannot be committed yet because they're
34 /// not contiguous
35 pub buffered_blocks: Gauge,
36 /// Total amount of memory used by the buffered blocks in bytes
37 pub buffered_blocks_size_bytes: Gauge,
38 /// The number blocks that are contiguous and are queued for insertion into the db.
39 pub queued_blocks: Gauge,
40 /// The number of out-of-order requests sent by the downloader.
41 /// The consumer of the download stream is able to re-request data (bodies) in case
42 /// it encountered a recoverable error (e.g. during insertion).
43 /// Out-of-order request happen when the new download range start for bodies downloader
44 /// is less than the last block number returned from the stream.
45 pub out_of_order_requests: Counter,
46 /// Number of timeout errors while requesting items
47 pub timeout_errors: Counter,
48 /// Number of validation errors while requesting items
49 pub validation_errors: Counter,
50 /// Number of unexpected errors while requesting items
51 pub unexpected_errors: Counter,
52}
53
54impl BodyDownloaderMetrics {
55 /// Increment errors counter.
56 pub fn increment_errors(&self, error: &DownloadError) {
57 match error {
58 DownloadError::Timeout => self.timeout_errors.increment(1),
59 DownloadError::BodyValidation { .. } => self.validation_errors.increment(1),
60 _error => self.unexpected_errors.increment(1),
61 }
62 }
63
64 /// Clear all gauge metrics by setting them to 0.
65 pub fn clear(&self) {
66 self.in_flight_requests.set(0);
67 self.buffered_responses.set(0);
68 self.buffered_blocks.set(0);
69 self.buffered_blocks_size_bytes.set(0);
70 self.queued_blocks.set(0);
71 }
72}
73
74/// Metrics for an individual response, i.e. the size in bytes, and length (number of bodies) in the
75/// response.
76///
77/// These metrics will be initialized with the `downloaders.bodies.response` scope.
78#[derive(Clone, Metrics)]
79#[metrics(scope = "downloaders.bodies.response")]
80pub struct ResponseMetrics {
81 /// The size (in bytes) of an individual bodies response received by the downloader.
82 pub response_size_bytes: Gauge,
83 /// The number of bodies in an individual bodies response received by the downloader.
84 pub response_length: Gauge,
85}
86
87/// Common header downloader metrics.
88///
89/// These metrics will be initialized with the `downloaders.headers` scope.
90/// ```
91/// use reth_downloaders::metrics::HeaderDownloaderMetrics;
92/// use reth_network_p2p::error::DownloadError;
93///
94/// // Initialize metrics.
95/// let metrics = HeaderDownloaderMetrics::default();
96/// // Increment `downloaders.headers.timeout_errors` counter by 1.
97/// metrics.increment_errors(&DownloadError::Timeout);
98/// ```
99#[derive(Clone, Metrics)]
100#[metrics(scope = "downloaders.headers")]
101pub struct HeaderDownloaderMetrics {
102 /// The number of items that were successfully sent to the poller (stage)
103 pub total_flushed: Counter,
104 /// Number of items that were successfully downloaded
105 pub total_downloaded: Counter,
106 /// The number of requests (can contain more than 1 item) currently in-flight.
107 pub in_flight_requests: Gauge,
108 /// The number of responses (can contain more than 1 item) in the internal buffer of the
109 /// downloader.
110 pub buffered_responses: Gauge,
111 /// The number of blocks the internal buffer of the
112 /// downloader.
113 /// These are bodies that have been received, but cannot be committed yet because they're
114 /// not contiguous
115 pub buffered_blocks: Gauge,
116 /// Total amount of memory used by the buffered blocks in bytes
117 pub buffered_blocks_size_bytes: Gauge,
118 /// The number blocks that are contiguous and are queued for insertion into the db.
119 pub queued_blocks: Gauge,
120 /// The number of out-of-order requests sent by the downloader.
121 /// The consumer of the download stream is able to re-request data (headers) in case
122 /// it encountered a recoverable error (e.g. during insertion).
123 /// Out-of-order request happen when the headers downloader `SyncTarget::Tip`
124 /// hash is different from the previous sync target hash.
125 pub out_of_order_requests: Counter,
126 /// Number of timeout errors while requesting items
127 pub timeout_errors: Counter,
128 /// Number of validation errors while requesting items
129 pub validation_errors: Counter,
130 /// Number of unexpected errors while requesting items
131 pub unexpected_errors: Counter,
132}
133
134impl HeaderDownloaderMetrics {
135 /// Increment errors counter.
136 pub fn increment_errors(&self, error: &DownloadError) {
137 match error {
138 DownloadError::Timeout => self.timeout_errors.increment(1),
139 DownloadError::HeaderValidation { .. } => self.validation_errors.increment(1),
140 _error => self.unexpected_errors.increment(1),
141 }
142 }
143}