feat(node-core, storage): static files segment metrics (#6908)

This commit is contained in:
Alexey Shekhirin
2024-03-01 16:36:50 +00:00
committed by GitHub
parent 8ea032adad
commit ea44c1d63f
9 changed files with 819 additions and 213 deletions

View File

@ -114,6 +114,45 @@ impl StaticFileProvider {
Self(Arc::new(provider))
}
/// Reports metrics for the static files.
pub fn report_metrics(&self) -> ProviderResult<()> {
let Some(metrics) = &self.metrics else { return Ok(()) };
let static_files = iter_static_files(&self.path)?;
for (segment, ranges) in static_files {
let mut entries = 0;
let mut size = 0;
for (block_range, _) in &ranges {
let fixed_block_range = find_fixed_range(block_range.start());
let jar_provider = self
.get_segment_provider(segment, || Some(fixed_block_range), None)?
.ok_or(ProviderError::MissingStaticFileBlock(segment, block_range.start()))?;
entries += jar_provider.rows();
let data_size = reth_primitives::fs::metadata(jar_provider.data_path())
.map(|metadata| metadata.len())
.unwrap_or_default();
let index_size = reth_primitives::fs::metadata(jar_provider.index_path())
.map(|metadata| metadata.len())
.unwrap_or_default();
let offsets_size = reth_primitives::fs::metadata(jar_provider.offsets_path())
.map(|metadata| metadata.len())
.unwrap_or_default();
let config_size = reth_primitives::fs::metadata(jar_provider.config_path())
.map(|metadata| metadata.len())
.unwrap_or_default();
size += data_size + index_size + offsets_size + config_size;
}
metrics.record_segment(segment, size, ranges.len(), entries);
}
Ok(())
}
/// Gets the [`StaticFileJarProvider`] of the requested segment and block.
pub fn get_segment_provider_from_block(
&self,

View File

@ -1,7 +1,7 @@
use std::{collections::HashMap, time::Duration};
use itertools::Itertools;
use metrics::{Counter, Histogram};
use metrics::{Counter, Gauge, Histogram};
use reth_metrics::Metrics;
use reth_primitives::StaticFileSegment;
use strum::{EnumIter, IntoEnumIterator};
@ -9,6 +9,7 @@ use strum::{EnumIter, IntoEnumIterator};
/// Metrics for the static file provider.
#[derive(Debug)]
pub struct StaticFileProviderMetrics {
segments: HashMap<StaticFileSegment, StaticFileSegmentMetrics>,
segment_operations: HashMap<
(StaticFileSegment, StaticFileProviderOperation),
StaticFileProviderOperationMetrics,
@ -18,6 +19,14 @@ pub struct StaticFileProviderMetrics {
impl Default for StaticFileProviderMetrics {
fn default() -> Self {
Self {
segments: StaticFileSegment::iter()
.map(|segment| {
(
segment,
StaticFileSegmentMetrics::new_with_labels(&[("segment", segment.as_str())]),
)
})
.collect(),
segment_operations: StaticFileSegment::iter()
.cartesian_product(StaticFileProviderOperation::iter())
.map(|(segment, operation)| {
@ -35,6 +44,22 @@ impl Default for StaticFileProviderMetrics {
}
impl StaticFileProviderMetrics {
pub(crate) fn record_segment(
&self,
segment: StaticFileSegment,
size: u64,
files: usize,
entries: usize,
) {
self.segments.get(&segment).expect("segment metrics should exist").size.set(size as f64);
self.segments.get(&segment).expect("segment metrics should exist").files.set(files as f64);
self.segments
.get(&segment)
.expect("segment metrics should exist")
.entries
.set(entries as f64);
}
pub(crate) fn record_segment_operation(
&self,
segment: StaticFileSegment,
@ -80,6 +105,18 @@ impl StaticFileProviderOperation {
}
}
/// Metrics for a specific static file segment.
#[derive(Metrics)]
#[metrics(scope = "static_files.segment")]
pub(crate) struct StaticFileSegmentMetrics {
/// The size of a static file segment
size: Gauge,
/// The number of files for a static file segment
files: Gauge,
/// The number of entries for a static file segment
entries: Gauge,
}
#[derive(Metrics)]
#[metrics(scope = "static_files.jar_provider")]
pub(crate) struct StaticFileProviderOperationMetrics {