Skip to content

Commit f20500e

Browse files
committed
Use default registry over creating new registry
1 parent 2d84c74 commit f20500e

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

portalnet/src/metrics/overlay.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use prometheus_exporter::{
1010
},
1111
};
1212

13-
/// TODO COMMENTS ON SETUP
13+
/// Contains metrics reporters for use in the overlay network
14+
/// (eg. `portalnet/src/overlay.rs` & `portalnet/src/overlay_service.rs`).
15+
/// Metric types reported here include protocol messages, utp transfers,
16+
/// and content validation.
1417
#[derive(Clone)]
1518
pub struct OverlayMetrics {
1619
pub message_count: IntCounterVec,

portalnet/src/metrics/portalnet.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use crate::metrics::overlay::OverlayMetrics;
22
use crate::metrics::storage::StorageMetrics;
33
use lazy_static::lazy_static;
4-
use prometheus_exporter::prometheus::Registry;
4+
use prometheus_exporter::prometheus::{default_registry, Registry};
55

6+
// We use lazy_static to ensure that the metrics registry is initialized only once, for each
7+
// runtime. This is important because the registry is a global singleton, and if it is
8+
// initialized more than once, it will panic when trying to register the same metric for each
9+
// subnetwork.
610
lazy_static! {
711
pub static ref PORTALNET_METRICS: PortalnetMetrics = initialize_metrics_registry();
812
}
913

1014
fn initialize_metrics_registry() -> PortalnetMetrics {
11-
let registry = Registry::new();
12-
PortalnetMetrics::new(registry).expect("failed to initialize metrics")
15+
PortalnetMetrics::new().expect("failed to initialize metrics")
1316
}
1417

1518
pub struct PortalnetMetrics {
@@ -18,7 +21,8 @@ pub struct PortalnetMetrics {
1821
}
1922

2023
impl PortalnetMetrics {
21-
pub fn new(registry: Registry) -> anyhow::Result<Self> {
24+
pub fn new() -> anyhow::Result<Self> {
25+
let registry = default_registry();
2226
let overlay = OverlayMetrics::new(&registry)?;
2327
let storage = StorageMetrics::new(&registry)?;
2428
Ok(Self { overlay, storage })

portalnet/src/metrics/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use prometheus_exporter::{
77
},
88
};
99

10+
/// Contains metrics reporters for portalnet storage.
1011
#[derive(Clone, Debug)]
1112
pub struct StorageMetrics {
1213
pub content_storage_usage_bytes: GaugeVec,

0 commit comments

Comments
 (0)