From 29c5f297a66dab92bc138c13fd7410cf40d70af1 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 1 Jun 2019 14:43:08 +1000 Subject: [PATCH] Add database size metric --- beacon_node/client/src/lib.rs | 1 + beacon_node/http_server/src/key.rs | 7 +++++++ beacon_node/http_server/src/lib.rs | 7 +++++-- beacon_node/http_server/src/metrics.rs | 11 +++++++++-- .../http_server/src/metrics/local_metrics.rs | 15 ++++++++++++++- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 4824d40ad2..d29d00ad49 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -128,6 +128,7 @@ where executor, network_send, beacon_chain.clone(), + config.db_name.clone(), metrics_registry, &log, )) diff --git a/beacon_node/http_server/src/key.rs b/beacon_node/http_server/src/key.rs index b84c5f6857..a69da6747f 100644 --- a/beacon_node/http_server/src/key.rs +++ b/beacon_node/http_server/src/key.rs @@ -3,6 +3,7 @@ use beacon_chain::{BeaconChain, BeaconChainTypes}; use iron::typemap::Key; use prometheus::Registry; use std::marker::PhantomData; +use std::path::PathBuf; use std::sync::Arc; pub struct BeaconChainKey { @@ -24,3 +25,9 @@ pub struct LocalMetricsKey; impl Key for LocalMetricsKey { type Value = LocalMetrics; } + +pub struct DBPathKey; + +impl Key for DBPathKey { + type Value = PathBuf; +} diff --git a/beacon_node/http_server/src/lib.rs b/beacon_node/http_server/src/lib.rs index cc54b6e17b..fb94348265 100644 --- a/beacon_node/http_server/src/lib.rs +++ b/beacon_node/http_server/src/lib.rs @@ -9,6 +9,7 @@ use network::NetworkMessage; use prometheus::Registry; use router::Router; use slog::{info, o, warn}; +use std::path::PathBuf; use std::sync::Arc; use tokio::runtime::TaskExecutor; @@ -32,6 +33,7 @@ impl Default for HttpServerConfig { /// Build the `iron` HTTP server, defining the core routes. pub fn create_iron_http_server( beacon_chain: Arc>, + db_path: PathBuf, metrics_registry: Registry, ) -> Iron { let mut router = Router::new(); @@ -39,7 +41,7 @@ pub fn create_iron_http_server( // A `GET` request to `/metrics` is handled by the `metrics` module. router.get( "/metrics", - metrics::build_handler(beacon_chain.clone(), metrics_registry), + metrics::build_handler(beacon_chain.clone(), db_path, metrics_registry), "metrics", ); @@ -55,6 +57,7 @@ pub fn start_service( executor: &TaskExecutor, _network_chan: crossbeam_channel::Sender, beacon_chain: Arc>, + db_path: PathBuf, metrics_registry: Registry, log: &slog::Logger, ) -> exit_future::Signal { @@ -66,7 +69,7 @@ pub fn start_service( let (shutdown_trigger, wait_for_shutdown) = exit_future::signal(); // Create an `iron` http, without starting it yet. - let iron = create_iron_http_server(beacon_chain, metrics_registry); + let iron = create_iron_http_server(beacon_chain, db_path, metrics_registry); // Create a HTTP server future. // diff --git a/beacon_node/http_server/src/metrics.rs b/beacon_node/http_server/src/metrics.rs index 7e716d5d7c..1b1ed1f3d4 100644 --- a/beacon_node/http_server/src/metrics.rs +++ b/beacon_node/http_server/src/metrics.rs @@ -1,5 +1,5 @@ use crate::{ - key::{BeaconChainKey, LocalMetricsKey, MetricsRegistryKey}, + key::{BeaconChainKey, DBPathKey, LocalMetricsKey, MetricsRegistryKey}, map_persistent_err_to_500, }; use beacon_chain::{BeaconChain, BeaconChainTypes}; @@ -7,6 +7,7 @@ use iron::prelude::*; use iron::{status::Status, Handler, IronResult, Request, Response}; use persistent::Read; use prometheus::{Encoder, Registry, TextEncoder}; +use std::path::PathBuf; use std::sync::Arc; pub use local_metrics::LocalMetrics; @@ -16,6 +17,7 @@ mod local_metrics; /// Yields a handler for the metrics endpoint. pub fn build_handler( beacon_chain: Arc>, + db_path: PathBuf, metrics_registry: Registry, ) -> impl Handler { let mut chain = Chain::new(handle_metrics::); @@ -26,6 +28,7 @@ pub fn build_handler( chain.link(Read::>::both(beacon_chain)); chain.link(Read::::both(metrics_registry)); chain.link(Read::::both(local_metrics)); + chain.link(Read::::both(db_path)); chain } @@ -46,8 +49,12 @@ fn handle_metrics(req: &mut Request) -> IronResul .get::>() .map_err(map_persistent_err_to_500)?; + let db_path = req + .get::>() + .map_err(map_persistent_err_to_500)?; + // Update metrics that are calculated on each scrape. - local_metrics.update(&beacon_chain); + local_metrics.update(&beacon_chain, &db_path); let mut buffer = vec![]; let encoder = TextEncoder::new(); diff --git a/beacon_node/http_server/src/metrics/local_metrics.rs b/beacon_node/http_server/src/metrics/local_metrics.rs index 68e02b0a11..e2b0e6513b 100644 --- a/beacon_node/http_server/src/metrics/local_metrics.rs +++ b/beacon_node/http_server/src/metrics/local_metrics.rs @@ -1,6 +1,8 @@ use beacon_chain::{BeaconChain, BeaconChainTypes}; use prometheus::{IntGauge, Opts, Registry}; use slot_clock::SlotClock; +use std::path::PathBuf; +use std::fs::File; use types::Slot; // If set to `true` will iterate and sum the balances of all validators in the state for each @@ -14,6 +16,7 @@ pub struct LocalMetrics { justified_epoch: IntGauge, finalized_epoch: IntGauge, validator_balances_sum: IntGauge, + database_size: IntGauge, } impl LocalMetrics { @@ -44,6 +47,10 @@ impl LocalMetrics { let opts = Opts::new("validator_balances_sum", "sum_of_all_validator_balances"); IntGauge::with_opts(opts)? }, + database_size: { + let opts = Opts::new("database_size", "size_of_on_disk_db_in_mb"); + IntGauge::with_opts(opts)? + }, }) } @@ -55,12 +62,13 @@ impl LocalMetrics { registry.register(Box::new(self.finalized_epoch.clone()))?; registry.register(Box::new(self.justified_epoch.clone()))?; registry.register(Box::new(self.validator_balances_sum.clone()))?; + registry.register(Box::new(self.database_size.clone()))?; Ok(()) } /// Update the metrics in `self` to the latest values. - pub fn update(&self, beacon_chain: &BeaconChain) { + pub fn update(&self, beacon_chain: &BeaconChain, db_path: &PathBuf) { let state = &beacon_chain.head().beacon_state; let present_slot = beacon_chain @@ -77,5 +85,10 @@ impl LocalMetrics { if SHOULD_SUM_VALIDATOR_BALANCES { self.validator_balances_sum.set(state.validator_balances.iter().sum::() as i64); } + let db_size = File::open(db_path) + .and_then(|f| f.metadata()) + .and_then(|m| Ok(m.len())) + .unwrap_or(0); + self.database_size.set(db_size as i64); } }