Skip to content

Commit

Permalink
add configurable metrics listening cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaMa committed Jul 11, 2024
1 parent 878624d commit 069ce38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ pub struct Start {
/// Specify the path to the file where logs will be stored
#[clap(default_value_os_t = std::env::temp_dir().join("snarkos.log"), long = "logfile")]
pub logfile: PathBuf,

/// Enables the metrics exporter
#[clap(default_value = "false", long = "metrics")]
pub metrics: bool,
/// Specify the IP address and port for the metrics exporter
#[clap(long = "metrics-ip")]
pub metrics_ip: Option<SocketAddr>,

/// Specify the path to a directory containing the storage database for the ledger
#[clap(long = "storage")]
Expand Down Expand Up @@ -558,7 +562,7 @@ impl Start {

// Initialize the metrics.
if self.metrics {
metrics::initialize_metrics();
metrics::initialize_metrics(self.metrics_ip);
}

// Initialize the storage mode.
Expand Down
2 changes: 1 addition & 1 deletion node/bft/examples/simple_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ async fn main() -> Result<()> {
#[cfg(feature = "metrics")]
if args.metrics {
info!("Initializing metrics...");
metrics::initialize_metrics();
metrics::initialize_metrics(SocketAddr::from_str(&format!("0.0.0.0:{}", 9000 + args.id)).ok());
}

// Start the monitoring server.
Expand Down
9 changes: 7 additions & 2 deletions node/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use snarkvm::{
};
use std::{
collections::HashMap,
net::SocketAddr,
str::FromStr,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
Expand All @@ -38,9 +40,12 @@ use std::{
use time::OffsetDateTime;

/// Initializes the metrics and returns a handle to the task running the metrics exporter.
pub fn initialize_metrics() {
pub fn initialize_metrics(ip: Option<SocketAddr>) {
// Build the Prometheus exporter.
metrics_exporter_prometheus::PrometheusBuilder::new().install().expect("can't build the prometheus exporter");
let builder = metrics_exporter_prometheus::PrometheusBuilder::new();
if let Some(ip) = ip { builder.with_http_listener(ip) } else { builder }
.install()
.expect("can't build the prometheus exporter");

// Register the snarkVM metrics.
snarkvm::metrics::register_metrics();
Expand Down

0 comments on commit 069ce38

Please sign in to comment.