reth_provider/providers/static_file/
metrics.rs

1use std::{collections::HashMap, time::Duration};
2
3use itertools::Itertools;
4use metrics::{Counter, Gauge, Histogram};
5use reth_metrics::Metrics;
6use reth_static_file_types::StaticFileSegment;
7use strum::{EnumIter, IntoEnumIterator};
8
9/// Metrics for the static file provider.
10#[derive(Debug)]
11pub struct StaticFileProviderMetrics {
12    segments: HashMap<StaticFileSegment, StaticFileSegmentMetrics>,
13    segment_operations: HashMap<
14        (StaticFileSegment, StaticFileProviderOperation),
15        StaticFileProviderOperationMetrics,
16    >,
17}
18
19impl Default for StaticFileProviderMetrics {
20    fn default() -> Self {
21        Self {
22            segments: StaticFileSegment::iter()
23                .map(|segment| {
24                    (
25                        segment,
26                        StaticFileSegmentMetrics::new_with_labels(&[("segment", segment.as_str())]),
27                    )
28                })
29                .collect(),
30            segment_operations: StaticFileSegment::iter()
31                .cartesian_product(StaticFileProviderOperation::iter())
32                .map(|(segment, operation)| {
33                    (
34                        (segment, operation),
35                        StaticFileProviderOperationMetrics::new_with_labels(&[
36                            ("segment", segment.as_str()),
37                            ("operation", operation.as_str()),
38                        ]),
39                    )
40                })
41                .collect(),
42        }
43    }
44}
45
46impl StaticFileProviderMetrics {
47    pub(crate) fn record_segment(
48        &self,
49        segment: StaticFileSegment,
50        size: u64,
51        files: usize,
52        entries: usize,
53    ) {
54        self.segments.get(&segment).expect("segment metrics should exist").size.set(size as f64);
55        self.segments.get(&segment).expect("segment metrics should exist").files.set(files as f64);
56        self.segments
57            .get(&segment)
58            .expect("segment metrics should exist")
59            .entries
60            .set(entries as f64);
61    }
62
63    pub(crate) fn record_segment_operation(
64        &self,
65        segment: StaticFileSegment,
66        operation: StaticFileProviderOperation,
67        duration: Option<Duration>,
68    ) {
69        let segment_operation = self
70            .segment_operations
71            .get(&(segment, operation))
72            .expect("segment operation metrics should exist");
73
74        segment_operation.calls_total.increment(1);
75
76        if let Some(duration) = duration {
77            segment_operation.write_duration_seconds.record(duration.as_secs_f64());
78        }
79    }
80
81    pub(crate) fn record_segment_operations(
82        &self,
83        segment: StaticFileSegment,
84        operation: StaticFileProviderOperation,
85        count: u64,
86        duration: Option<Duration>,
87    ) {
88        self.segment_operations
89            .get(&(segment, operation))
90            .expect("segment operation metrics should exist")
91            .calls_total
92            .increment(count);
93
94        if let Some(duration) = duration {
95            self.segment_operations
96                .get(&(segment, operation))
97                .expect("segment operation metrics should exist")
98                .write_duration_seconds
99                .record(duration.as_secs_f64() / count as f64);
100        }
101    }
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
105pub(crate) enum StaticFileProviderOperation {
106    InitCursor,
107    OpenWriter,
108    Append,
109    Prune,
110    IncrementBlock,
111    CommitWriter,
112}
113
114impl StaticFileProviderOperation {
115    const fn as_str(&self) -> &'static str {
116        match self {
117            Self::InitCursor => "init-cursor",
118            Self::OpenWriter => "open-writer",
119            Self::Append => "append",
120            Self::Prune => "prune",
121            Self::IncrementBlock => "increment-block",
122            Self::CommitWriter => "commit-writer",
123        }
124    }
125}
126
127/// Metrics for a specific static file segment.
128#[derive(Metrics)]
129#[metrics(scope = "static_files.segment")]
130pub(crate) struct StaticFileSegmentMetrics {
131    /// The size of a static file segment
132    size: Gauge,
133    /// The number of files for a static file segment
134    files: Gauge,
135    /// The number of entries for a static file segment
136    entries: Gauge,
137}
138
139#[derive(Metrics)]
140#[metrics(scope = "static_files.jar_provider")]
141pub(crate) struct StaticFileProviderOperationMetrics {
142    /// Total number of static file jar provider operations made.
143    calls_total: Counter,
144    /// The time it took to execute the static file jar provider operation that writes data.
145    write_duration_seconds: Histogram,
146}