Skip to content

Commit 399dfe6

Browse files
committed
solution: metric for a single block archive time in streaming mode
1 parent 8528d6a commit 399dfe6

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/archiver/archiver.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl<B: BlockchainTypes, TS: TargetStorage> ArchiveAll<Height> for Archiver<B, T
100100

101101
let success = success_tx & success_trace;
102102
let duration = Utc::now().signed_duration_since(start_time);
103+
let duration_secs = duration.num_milliseconds() as f64 / 1000.0;
104+
crate::metrics::observe_block_archive(duration_secs);
103105
if success {
104106
tracing::info!("Blocks {} is archived in {}ms", what, duration.num_milliseconds());
105107
} else {
@@ -159,6 +161,10 @@ impl<B: BlockchainTypes, TS: TargetStorage> ArchiveAll<Range> for Archiver<B, TS
159161
}
160162

161163
let duration = Utc::now().signed_duration_since(start_time);
164+
if what.len() == 1 {
165+
let duration_secs = duration.num_milliseconds() as f64 / 1000.0;
166+
crate::metrics::observe_block_archive(duration_secs);
167+
}
162168
if duration.num_seconds() > 2 {
163169
tracing::info!("Range {} is archived in {}sec", what, duration.num_seconds());
164170
} else {

src/metrics/archive.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,33 @@
22
//
33
// Licensed under the Apache License, Version 2.0
44

5-
use prometheus::{CounterVec, Opts, Registry};
5+
use prometheus::{CounterVec, Histogram, HistogramOpts, Opts, Registry};
66
use crate::archiver::datakind::DataKind;
77
use super::Direction;
88

99
/// Metrics for the archive processing zone.
1010
///
11-
/// Uses two `CounterVec` metrics with `type` and `direction` labels:
12-
/// - `items` — total number of items (blocks, transactions, traces) processed
13-
/// - `bytes` — total number of bytes transferred to/from storage
11+
/// - `items` — counter of items (blocks, transactions, traces) processed, by type and direction
12+
/// - `bytes` — counter of bytes transferred, by type and direction
13+
/// - `block_archive_duration` — histogram of single-block archival time (block + txes + traces)
1414
pub struct ArchiveMetrics {
1515
/// Total items processed, labeled by type and direction
1616
pub items: CounterVec,
1717
/// Total bytes transferred, labeled by type and direction
1818
pub bytes: CounterVec,
19+
/// Time to archive a single block with all its tables
20+
pub block_archive_duration: Histogram,
1921
}
2022

2123
impl ArchiveMetrics {
2224
pub fn new(app_name: &str) -> Self {
25+
// Buckets tuned for the expected 500ms–2s range, with tails for slow blocks
26+
let buckets = vec![
27+
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
28+
1.25, 1.5, 1.75, 2.0, 2.5,
29+
3.0, 4.0, 5.0, 7.5, 10.0, 12.5, 15.0,
30+
20.0, 25.0, 30.0,
31+
];
2332
Self {
2433
items: CounterVec::new(
2534
Opts::new(
@@ -37,12 +46,21 @@ impl ArchiveMetrics {
3746
&["type", "direction"],
3847
)
3948
.unwrap(),
49+
block_archive_duration: Histogram::with_opts(
50+
HistogramOpts::new(
51+
format!("{}_archive_blockTime_seconds", app_name),
52+
"Time to archive a single block with all its tables (block, txes, traces)",
53+
)
54+
.buckets(buckets),
55+
)
56+
.unwrap(),
4057
}
4158
}
4259

4360
pub fn register(&self, registry: &Registry) {
4461
registry.register(Box::new(self.items.clone())).unwrap();
4562
registry.register(Box::new(self.bytes.clone())).unwrap();
63+
registry.register(Box::new(self.block_archive_duration.clone())).unwrap();
4664
}
4765

4866
/// Record that `n` items of the given data kind have been processed.
@@ -58,4 +76,9 @@ impl ArchiveMetrics {
5876
.with_label_values(&[kind.metrics_label(), direction.metrics_label()])
5977
.inc_by(n as f64);
6078
}
79+
80+
/// Record the time it took to archive a single block (including txes and traces).
81+
pub fn observe_block_archive(&self, duration_secs: f64) {
82+
self.block_archive_duration.observe(duration_secs);
83+
}
6184
}

src/metrics/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ pub fn add_bytes(kind: &DataKind, direction: Direction, n: usize) {
104104
METRICS.archive.add_bytes(kind, &direction, n);
105105
}
106106

107+
/// Record the time it took to archive a single block (including txes and traces).
108+
pub fn observe_block_archive(duration_secs: f64) {
109+
if !ENABLED.load(Ordering::Relaxed) {
110+
return;
111+
}
112+
METRICS.archive.observe_block_archive(duration_secs);
113+
}
114+
107115
/// Observe the duration of a blockchain RPC request.
108116
pub fn observe_request(method: &str, blockchain: &str, duration_secs: f64) {
109117
if !ENABLED.load(Ordering::Relaxed) {

0 commit comments

Comments
 (0)