Skip to content

Commit 4b8721c

Browse files
committed
chore: add downloader metrics
1 parent 881d8c2 commit 4b8721c

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/metrics/src/downloader.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use prometheus_exporter::prometheus::{
2+
opts, register_int_gauge_vec_with_registry, IntGaugeVec, Registry,
3+
};
4+
5+
use crate::portalnet::PORTALNET_METRICS;
6+
7+
/// Contains metrics reporters for portalnet bridge.
8+
#[derive(Clone, Debug)]
9+
pub struct DownloaderMetrics {
10+
pub current_block: IntGaugeVec,
11+
}
12+
13+
impl DownloaderMetrics {
14+
pub fn new(registry: &Registry) -> anyhow::Result<Self> {
15+
let current_block = register_int_gauge_vec_with_registry!(
16+
opts!(
17+
"downloader_current_block",
18+
"the current block number the downloader is on"
19+
),
20+
&["downloader"],
21+
registry
22+
)?;
23+
Ok(Self { current_block })
24+
}
25+
}
26+
27+
#[derive(Clone, Debug)]
28+
pub struct DownloaderMetricsReporter {
29+
metrics: DownloaderMetrics,
30+
}
31+
32+
impl Default for DownloaderMetricsReporter {
33+
fn default() -> Self {
34+
Self::new()
35+
}
36+
}
37+
38+
impl DownloaderMetricsReporter {
39+
pub fn new() -> Self {
40+
Self {
41+
metrics: PORTALNET_METRICS.downloader(),
42+
}
43+
}
44+
45+
pub fn report_current_block(&self, block_number: u64) {
46+
self.metrics
47+
.current_block
48+
.with_label_values(&["downloader"])
49+
.set(block_number as i64);
50+
}
51+
}

crates/metrics/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![warn(clippy::uninlined_format_args)]
33

44
pub mod bridge;
5+
pub mod downloader;
56
pub mod labels;
67
pub mod overlay;
78
pub mod portalnet;

crates/metrics/src/portalnet.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use lazy_static::lazy_static;
22
use prometheus_exporter::prometheus::default_registry;
33

4-
use crate::{bridge::BridgeMetrics, overlay::OverlayMetrics, storage::StorageMetrics};
4+
use crate::{
5+
bridge::BridgeMetrics, downloader::DownloaderMetrics, overlay::OverlayMetrics,
6+
storage::StorageMetrics,
7+
};
58

69
// We use lazy_static to ensure that the metrics registry is initialized only once, for each
710
// runtime. This is important because the registry is a global singleton, and if it is
@@ -17,6 +20,7 @@ fn initialize_metrics_registry() -> PortalnetMetrics {
1720

1821
pub struct PortalnetMetrics {
1922
bridge: BridgeMetrics,
23+
downloader: DownloaderMetrics,
2024
overlay: OverlayMetrics,
2125
storage: StorageMetrics,
2226
}
@@ -27,10 +31,12 @@ impl PortalnetMetrics {
2731
let overlay = OverlayMetrics::new(registry)?;
2832
let storage = StorageMetrics::new(registry)?;
2933
let bridge = BridgeMetrics::new(registry)?;
34+
let downloader = DownloaderMetrics::new(registry)?;
3035
Ok(Self {
3136
overlay,
3237
storage,
3338
bridge,
39+
downloader,
3440
})
3541
}
3642

@@ -45,4 +51,8 @@ impl PortalnetMetrics {
4551
pub fn bridge(&self) -> BridgeMetrics {
4652
self.bridge.clone()
4753
}
54+
55+
pub fn downloader(&self) -> DownloaderMetrics {
56+
self.downloader.clone()
57+
}
4858
}

crates/subnetworks/history/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tokio.workspace = true
2525
tracing.workspace = true
2626
tree_hash.workspace = true
2727
trin-storage.workspace = true
28+
trin-metrics.workspace = true
2829
trin-validation.workspace = true
2930
utp-rs.workspace = true
3031
portal-bridge.workspace = true

crates/subnetworks/history/src/downloader.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use portal_bridge::census::Census;
2828
use portalnet::{constants::DEFAULT_WEB3_HTTP_ADDRESS, overlay::protocol::OverlayProtocol};
2929
use ssz_types::BitList;
3030
use tracing::{info, warn};
31+
use trin_metrics::downloader::DownloaderMetricsReporter;
3132

3233
use crate::{storage::HistoryStorage, validation::ChainHistoryValidator};
3334

@@ -57,6 +58,7 @@ pub struct Downloader {
5758
pub census: Census,
5859
pub overlay_arc:
5960
Arc<OverlayProtocol<HistoryContentKey, XorMetric, ChainHistoryValidator, HistoryStorage>>,
61+
pub metrics: DownloaderMetricsReporter,
6062
}
6163

6264
impl Downloader {
@@ -74,10 +76,13 @@ impl Downloader {
7476
.map_err(|e| e.to_string())
7577
.expect("Failed to build http client");
7678

79+
let metrics = DownloaderMetricsReporter::new();
80+
7781
let census = Census::new(http_client, CENSUS_ENR_LIMIT, vec![]);
7882
Self {
7983
overlay_arc,
8084
census,
85+
metrics,
8186
}
8287
}
8388

@@ -119,6 +124,7 @@ impl Downloader {
119124
let mut futures = Vec::new();
120125

121126
for (block_number, block_hash) in batch {
127+
self.metrics.report_current_block(block_number);
122128
let block_body_content_key = generate_block_body_content_key(block_hash.clone());
123129
futures.push(self.find_content(
124130
block_body_content_key,

0 commit comments

Comments
 (0)