Skip to content

Commit

Permalink
Use global metrics registry
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Sep 28, 2023
1 parent 22ac097 commit 16f2284
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 70 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ eth2_ssz = "0.4.0"
ethereum-types = "0.12.1"
ethportal-api = { path = "ethportal-api" }
jsonrpsee = "0.20.0"
lazy_static = "1.4.0"
parking_lot = "0.11.2"
portalnet = { path = "portalnet" }
prometheus_exporter = "0.8.4"
Expand Down
2 changes: 2 additions & 0 deletions portalnet/src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod labels;
pub mod overlay;
pub mod portalnet;
pub mod registry;
pub mod storage;
11 changes: 2 additions & 9 deletions portalnet/src/metrics/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::types::messages::{Request, Response};
use prometheus_exporter::{
self,
prometheus::{
default_registry, opts, register_int_counter_vec_with_registry,
register_int_gauge_vec_with_registry, IntCounterVec, IntGaugeVec, Registry,
opts, register_int_counter_vec_with_registry, register_int_gauge_vec_with_registry,
IntCounterVec, IntGaugeVec, Registry,
},
};

Expand All @@ -19,13 +19,6 @@ pub struct OverlayMetrics {
pub validation_count: IntCounterVec,
}

impl Default for OverlayMetrics {
fn default() -> Self {
let registry = default_registry();
Self::new(registry).expect("failed to create default metrics")
}
}

impl OverlayMetrics {
pub fn new(registry: &Registry) -> anyhow::Result<Self> {
let message_count = register_int_counter_vec_with_registry!(
Expand Down
16 changes: 16 additions & 0 deletions portalnet/src/metrics/portalnet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::metrics::overlay::OverlayMetrics;
use crate::metrics::storage::StorageMetrics;
use prometheus_exporter::{self, prometheus::Registry};
pub struct PortalnetMetrics {
pub overlay: OverlayMetrics,
pub storage: StorageMetrics,
}

impl PortalnetMetrics {
// remove this borrow?
pub fn new(registry: &Registry) -> anyhow::Result<Self> {
let overlay = OverlayMetrics::new(registry)?;
let storage = StorageMetrics::new(registry)?;
Ok(Self { overlay, storage })
}
}
12 changes: 12 additions & 0 deletions portalnet/src/metrics/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::metrics::portalnet::PortalnetMetrics;
use lazy_static::lazy_static;
use prometheus_exporter::prometheus::Registry;

lazy_static! {
pub static ref METRICS_REGISTRY: PortalnetMetrics = initialize_metrics_registry();
}

fn initialize_metrics_registry() -> PortalnetMetrics {
let registry = Registry::new();
PortalnetMetrics::new(&registry).expect("failed to initialize metrics")
}
58 changes: 30 additions & 28 deletions portalnet/src/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2727,29 +2727,28 @@ fn pop_while_ssz_bytes_len_gt(enrs: &mut Vec<SszEnr>, max_size: usize) {
mod tests {
use super::*;

use std::net::SocketAddr;
use std::time::Instant;

use rstest::rstest;
use discv5::kbucket::Entry;
use ethereum_types::U256;
use rstest::*;
use serial_test::serial;
use tokio::sync::mpsc::unbounded_channel;
use tokio_test::{assert_pending, assert_ready, task};

use crate::metrics::registry::METRICS_REGISTRY;
use crate::{
discovery::{Discovery, NodeAddress},
overlay::OverlayConfig,
storage::{DistanceFunction, MemoryContentStore},
types::messages::PortalnetConfig,
};

use ethportal_api::types::content_key::overlay::IdentityContentKey;
use ethportal_api::types::distance::XorMetric;
use ethportal_api::types::enr::generate_random_remote_enr;
use trin_validation::validator::MockValidator;

use discv5::kbucket::Entry;
use ethereum_types::U256;
use serial_test::serial;
use std::net::SocketAddr;
use tokio::sync::mpsc::unbounded_channel;
use tokio_test::{assert_pending, assert_ready, task};

macro_rules! poll_command_rx {
($service:ident) => {
$service.enter(|cx, mut service| service.command_rx.poll_recv(cx))
Expand Down Expand Up @@ -2788,7 +2787,8 @@ mod tests {
let peers_to_ping = HashSetDelay::default();
let (command_tx, command_rx) = mpsc::unbounded_channel();
let (response_tx, response_rx) = mpsc::unbounded_channel();
let metrics = Arc::new(OverlayMetrics::default());
let metrics = METRICS_REGISTRY.overlay.clone();
let metrics = Arc::new(metrics);
let validator = Arc::new(MockValidator {});

OverlayService {
Expand Down Expand Up @@ -2895,6 +2895,7 @@ mod tests {
service.process_ping(ping, node_id);

assert_pending!(poll_command_rx!(service));
std::mem::drop(service);
}

#[test_log::test(tokio::test)]
Expand Down Expand Up @@ -2937,6 +2938,7 @@ mod tests {
}
_ => panic!(),
};
std::mem::drop(service);
}

#[test_log::test(tokio::test)]
Expand Down Expand Up @@ -3172,12 +3174,12 @@ mod tests {

// Node has maximum radius, so there should be one offer in the channel.
OverlayService::<IdentityContentKey, XorMetric, MockValidator, MemoryContentStore>::poke_content(
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
let cmd = assert_ready!(poll_command_rx!(service));
let cmd = cmd.unwrap();
if let OverlayCommand::Request(req) = cmd {
Expand Down Expand Up @@ -3209,12 +3211,12 @@ mod tests {

// No nodes in the routing table, so no commands should be in the channel.
OverlayService::<IdentityContentKey, XorMetric, MockValidator, MemoryContentStore>::poke_content(
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
assert_pending!(poll_command_rx!(service));
}

Expand Down Expand Up @@ -3260,12 +3262,12 @@ mod tests {

// One offer should be in the channel for the maximum radius node.
OverlayService::<IdentityContentKey, XorMetric, MockValidator, MemoryContentStore>::poke_content(
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
service.kbuckets.clone(),
service.command_tx.clone(),
content_key,
content,
peer_node_ids,
);
let cmd = assert_ready!(poll_command_rx!(service));
let cmd = cmd.unwrap();
if let OverlayCommand::Request(req) = cmd {
Expand Down
40 changes: 21 additions & 19 deletions portalnet/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
use anyhow::anyhow;
use discv5::enr::NodeId;
use ethportal_api::types::portal::PaginateLocalContentInfo;
use prometheus_exporter::{self, prometheus::Registry};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rocksdb::{Options, DB};
Expand Down Expand Up @@ -186,11 +185,10 @@ impl PortalStorageConfig {
storage_capacity_mb: u64,
node_data_dir: PathBuf,
node_id: NodeId,
registry: &Registry,
metrics: StorageMetrics,
) -> anyhow::Result<Self> {
let db = Arc::new(PortalStorage::setup_rocksdb(&node_data_dir)?);
let sql_connection_pool = PortalStorage::setup_sql(&node_data_dir)?;
let metrics = StorageMetrics::new(registry)?;
Ok(Self {
storage_capacity_mb,
node_id,
Expand Down Expand Up @@ -821,11 +819,11 @@ pub mod test {
use super::*;

use discv5::enr::{CombinedKey, EnrBuilder};
use prometheus_exporter::prometheus::default_registry;
use quickcheck::{quickcheck, QuickCheck, TestResult};
use rand::RngCore;
use serial_test::serial;

use crate::metrics::registry::METRICS_REGISTRY;
use crate::utils::db::{configure_node_data_dir, setup_temp_dir};
use ethportal_api::types::content_key::overlay::IdentityContentKey;

Expand All @@ -847,7 +845,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand Down Expand Up @@ -875,7 +873,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History).unwrap();
Expand Down Expand Up @@ -903,7 +901,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand All @@ -929,7 +927,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand All @@ -956,7 +954,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand All @@ -977,7 +975,7 @@ pub mod test {
1,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let new_storage = PortalStorage::new(new_storage_config, ProtocolId::History)?;
Expand All @@ -996,7 +994,7 @@ pub mod test {
0,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let new_storage = PortalStorage::new(new_storage_config, ProtocolId::History)?;
Expand All @@ -1023,7 +1021,7 @@ pub mod test {
min_capacity,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config.clone(), ProtocolId::History)?;
Expand Down Expand Up @@ -1074,7 +1072,7 @@ pub mod test {
CAPACITY_MB,
node_data_dir.clone(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand All @@ -1093,9 +1091,13 @@ pub mod test {
std::mem::drop(storage);

// test with increased capacity
let new_storage_config =
PortalStorageConfig::new(2 * CAPACITY_MB, node_data_dir, node_id, default_registry())
.unwrap();
let new_storage_config = PortalStorageConfig::new(
2 * CAPACITY_MB,
node_data_dir,
node_id,
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let new_storage = PortalStorage::new(new_storage_config, ProtocolId::History)?;

// test that previously set value has not been pruned
Expand Down Expand Up @@ -1123,7 +1125,7 @@ pub mod test {
0,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand Down Expand Up @@ -1151,7 +1153,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let storage = PortalStorage::new(storage_config, ProtocolId::History)?;
Expand All @@ -1176,7 +1178,7 @@ pub mod test {
CAPACITY_MB,
temp_dir.path().to_path_buf(),
node_id,
default_registry(),
METRICS_REGISTRY.storage.clone(),
)
.unwrap();
let mut storage = PortalStorage::new(storage_config, ProtocolId::History).unwrap();
Expand Down
8 changes: 3 additions & 5 deletions portalnet/src/types/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{
};

use ethereum_types::{H256, U256};
use prometheus_exporter::prometheus::Registry;
use rlp::Encodable;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
Expand All @@ -18,6 +17,7 @@ use thiserror::Error;
use validator::ValidationError;

use crate::metrics::overlay::OverlayMetrics;
use crate::metrics::registry::METRICS_REGISTRY;
use ethportal_api::types::bootnodes::Bootnodes;
use ethportal_api::types::bytes::ByteList;
use ethportal_api::types::cli::TrinConfig;
Expand Down Expand Up @@ -176,15 +176,13 @@ pub struct PortalnetConfig {
}

impl PortalnetConfig {
pub fn new(trin_config: &TrinConfig, private_key: H256, registry: &Registry) -> Self {
pub fn new(trin_config: &TrinConfig, private_key: H256) -> Self {
Self {
external_addr: trin_config.external_addr,
private_key,
listen_port: trin_config.discovery_port,
no_stun: trin_config.no_stun,
bootnodes: trin_config.bootnodes.clone(),
metrics: OverlayMetrics::new(registry)
.expect("to be able to intialize from default metrics registry"),
..Default::default()
}
}
Expand All @@ -201,7 +199,7 @@ impl Default for PortalnetConfig {
internal_ip: false,
no_stun: false,
node_addr_cache_capacity: NODE_ADDR_CACHE_CAPACITY,
metrics: OverlayMetrics::default(),
metrics: METRICS_REGISTRY.overlay.clone(),
}
}
}
Expand Down
Loading

0 comments on commit 16f2284

Please sign in to comment.