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
65/// Metrics for an individual response, i.e. the size in bytes, and length (number of bodies) in the
66/// response.
67///
68/// These metrics will be initialized with the `downloaders.bodies.response` scope.
69#[derive(Clone, Metrics)]
70#[metrics(scope = "downloaders.bodies.response")]
71pub struct ResponseMetrics {
72    /// The size (in bytes) of an individual bodies response received by the downloader.
73    pub response_size_bytes: Gauge,
74    /// The number of bodies in an individual bodies response received by the downloader.
75    pub response_length: Gauge,
76}
77
78/// Common header downloader metrics.
79///
80/// These metrics will be initialized with the `downloaders.headers` scope.
81/// ```
82/// use reth_downloaders::metrics::HeaderDownloaderMetrics;
83/// use reth_network_p2p::error::DownloadError;
84///
85/// // Initialize metrics.
86/// let metrics = HeaderDownloaderMetrics::default();
87/// // Increment `downloaders.headers.timeout_errors` counter by 1.
88/// metrics.increment_errors(&DownloadError::Timeout);
89/// ```
90#[derive(Clone, Metrics)]
91#[metrics(scope = "downloaders.headers")]
92pub struct HeaderDownloaderMetrics {
93    /// The number of items that were successfully sent to the poller (stage)
94    pub total_flushed: Counter,
95    /// Number of items that were successfully downloaded
96    pub total_downloaded: Counter,
97    /// The number of requests (can contain more than 1 item) currently in-flight.
98    pub in_flight_requests: Gauge,
99    /// The number of responses (can contain more than 1 item) in the internal buffer of the
100    /// downloader.
101    pub buffered_responses: Gauge,
102    /// The number of blocks the internal buffer of the
103    /// downloader.
104    /// These are bodies that have been received, but cannot be committed yet because they're
105    /// not contiguous
106    pub buffered_blocks: Gauge,
107    /// Total amount of memory used by the buffered blocks in bytes
108    pub buffered_blocks_size_bytes: Gauge,
109    /// The number blocks that are contiguous and are queued for insertion into the db.
110    pub queued_blocks: Gauge,
111    /// The number of out-of-order requests sent by the downloader.
112    /// The consumer of the download stream is able to re-request data (headers) in case
113    /// it encountered a recoverable error (e.g. during insertion).
114    /// Out-of-order request happen when the headers downloader `SyncTarget::Tip`
115    /// hash is different from the previous sync target hash.
116    pub out_of_order_requests: Counter,
117    /// Number of timeout errors while requesting items
118    pub timeout_errors: Counter,
119    /// Number of validation errors while requesting items
120    pub validation_errors: Counter,
121    /// Number of unexpected errors while requesting items
122    pub unexpected_errors: Counter,
123}
124
125impl HeaderDownloaderMetrics {
126    /// Increment errors counter.
127    pub fn increment_errors(&self, error: &DownloadError) {
128        match error {
129            DownloadError::Timeout => self.timeout_errors.increment(1),
130            DownloadError::HeaderValidation { .. } => self.validation_errors.increment(1),
131            _error => self.unexpected_errors.increment(1),
132        }
133    }
134}