From 126d33da1dda7ba3a7cb0ea88cb46a504f539ea9 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Tue, 15 Oct 2024 13:54:47 -0400 Subject: [PATCH] refactor(katana): separate metrics address and port into 2 args (#2537) --- bin/katana/src/cli/node.rs | 63 +++++++++++++++++------- crates/katana/node/src/config/metrics.rs | 18 ++++++- crates/katana/node/src/lib.rs | 11 +++-- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/bin/katana/src/cli/node.rs b/bin/katana/src/cli/node.rs index c397ab9a49..c0f210a41c 100644 --- a/bin/katana/src/cli/node.rs +++ b/bin/katana/src/cli/node.rs @@ -11,14 +11,13 @@ //! for more info. use std::collections::HashSet; -use std::net::{IpAddr, SocketAddr}; +use std::net::IpAddr; use std::path::PathBuf; use alloy_primitives::U256; use anyhow::{Context, Result}; use clap::{Args, Parser}; use console::Style; -use dojo_utils::parse::parse_socket_address; use katana_core::constants::{ DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_SEQUENCER_ADDRESS, DEFAULT_STRK_L1_GAS_PRICE, }; @@ -26,12 +25,12 @@ use katana_core::service::messaging::MessagingConfig; use katana_node::config::db::DbConfig; use katana_node::config::dev::DevConfig; use katana_node::config::execution::{ - ExecutionConfig, DEFAULT_INVOCATION_MAX_STEPS, DEFAULT_VALIDATION_MAX_STEPS, + DEFAULT_INVOCATION_MAX_STEPS, DEFAULT_VALIDATION_MAX_STEPS, ExecutionConfig, }; use katana_node::config::fork::ForkingConfig; -use katana_node::config::metrics::MetricsConfig; +use katana_node::config::metrics::{DEFAULT_METRICS_ADDR, DEFAULT_METRICS_PORT, MetricsConfig}; use katana_node::config::rpc::{ - ApiKind, RpcConfig, DEFAULT_RPC_ADDR, DEFAULT_RPC_MAX_CONNECTIONS, DEFAULT_RPC_PORT, + ApiKind, DEFAULT_RPC_ADDR, DEFAULT_RPC_MAX_CONNECTIONS, DEFAULT_RPC_PORT, RpcConfig, }; use katana_node::config::{Config, SequencingConfig}; use katana_primitives::block::BlockHashOrNumber; @@ -39,15 +38,15 @@ use katana_primitives::chain::ChainId; use katana_primitives::chain_spec::{self, ChainSpec}; use katana_primitives::class::ClassHash; use katana_primitives::contract::ContractAddress; +use katana_primitives::genesis::Genesis; use katana_primitives::genesis::allocation::{DevAllocationsGenerator, GenesisAccountAlloc}; use katana_primitives::genesis::constant::{ DEFAULT_LEGACY_ERC20_CLASS_HASH, DEFAULT_LEGACY_UDC_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE, DEFAULT_UDC_ADDRESS, }; -use katana_primitives::genesis::Genesis; -use tracing::{info, Subscriber}; +use tracing::{Subscriber, info}; use tracing_log::LogTracer; -use tracing_subscriber::{fmt, EnvFilter}; +use tracing_subscriber::{EnvFilter, fmt}; use url::Url; use crate::utils::{parse_block_hash_or_number, parse_genesis, parse_seed}; @@ -94,12 +93,6 @@ pub struct NodeArgs { #[arg(help = "Output logs in JSON format.")] pub json_log: bool, - /// Enable Prometheus metrics. - /// - /// The metrics will be served at the given interface and port. - #[arg(long, value_name = "SOCKET", value_parser = parse_socket_address, help_heading = "Metrics")] - pub metrics: Option, - #[arg(long)] #[arg(value_name = "PATH")] #[arg(value_parser = katana_core::service::messaging::MessagingConfig::parse)] @@ -110,20 +103,46 @@ pub struct NodeArgs { pub messaging: Option, #[command(flatten)] - #[command(next_help_heading = "Server options")] + pub metrics: MetricsOptions, + + #[command(flatten)] pub server: ServerOptions, #[command(flatten)] - #[command(next_help_heading = "Starknet options")] pub starknet: StarknetOptions, #[cfg(feature = "slot")] #[command(flatten)] - #[command(next_help_heading = "Slot options")] pub slot: SlotOptions, } #[derive(Debug, Args, Clone)] +#[command(next_help_heading = "Metrics options")] +pub struct MetricsOptions { + /// Whether to enable metrics. + /// + /// For now, metrics will still be collected even if this flag is not set. This only + /// controls whether the metrics server is started or not. + #[arg(long)] + pub metrics: bool, + + /// The metrics will be served at the given address. + #[arg(long = "metrics.addr")] + #[arg(value_name = "ADD")] + #[arg(requires = "metrics")] + #[arg(default_value_t = DEFAULT_METRICS_ADDR)] + pub metrics_addr: IpAddr, + + /// The metrics will be served at the given port. + #[arg(long = "metrics.port")] + #[arg(value_name = "PORT")] + #[arg(requires = "metrics")] + #[arg(default_value_t = DEFAULT_METRICS_PORT)] + pub metrics_port: u16, +} + +#[derive(Debug, Args, Clone)] +#[command(next_help_heading = "Server options")] pub struct ServerOptions { #[arg(short, long)] #[arg(default_value_t = DEFAULT_RPC_PORT)] @@ -147,6 +166,7 @@ pub struct ServerOptions { } #[derive(Debug, Args, Clone)] +#[command(next_help_heading = "Starknet options")] pub struct StarknetOptions { #[arg(long)] #[arg(default_value = "0")] @@ -168,7 +188,6 @@ pub struct StarknetOptions { pub disable_validate: bool, #[command(flatten)] - #[command(next_help_heading = "Environment options")] pub environment: EnvironmentOptions, #[arg(long)] @@ -178,6 +197,7 @@ pub struct StarknetOptions { } #[derive(Debug, Args, Clone)] +#[command(next_help_heading = "Environment options")] pub struct EnvironmentOptions { #[arg(long)] #[arg(help = "The chain ID.")] @@ -212,6 +232,7 @@ pub struct EnvironmentOptions { #[cfg(feature = "slot")] #[derive(Debug, Args, Clone)] +#[command(next_help_heading = "Slot options")] pub struct SlotOptions { #[arg(hide = true)] #[arg(long = "slot.controller")] @@ -369,7 +390,11 @@ impl NodeArgs { } fn metrics_config(&self) -> Option { - self.metrics.map(|addr| MetricsConfig { addr }) + if self.metrics.metrics { + Some(MetricsConfig { addr: self.metrics.metrics_addr, port: self.metrics.metrics_port }) + } else { + None + } } } diff --git a/crates/katana/node/src/config/metrics.rs b/crates/katana/node/src/config/metrics.rs index 40a1b234ad..9830660485 100644 --- a/crates/katana/node/src/config/metrics.rs +++ b/crates/katana/node/src/config/metrics.rs @@ -1,8 +1,22 @@ -use std::net::SocketAddr; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + +/// Metrics server default address. +pub const DEFAULT_METRICS_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST); +/// Metrics server default port. +pub const DEFAULT_METRICS_PORT: u16 = 9100; /// Node metrics configurations. #[derive(Debug, Clone)] pub struct MetricsConfig { /// The address to bind the metrics server to. - pub addr: SocketAddr, + pub addr: IpAddr, + /// The port to bind the metrics server to. + pub port: u16, +} + +impl MetricsConfig { + /// Returns the [`SocketAddr`] for the metrics server. + pub fn socket_addr(&self) -> SocketAddr { + SocketAddr::new(self.addr, self.port) + } } diff --git a/crates/katana/node/src/lib.rs b/crates/katana/node/src/lib.rs index 309fedf51d..8e6151091b 100644 --- a/crates/katana/node/src/lib.rs +++ b/crates/katana/node/src/lib.rs @@ -16,27 +16,27 @@ use config::{Config, SequencingConfig}; use dojo_metrics::exporters::prometheus::PrometheusRecorder; use dojo_metrics::{Report, Server as MetricsServer}; use hyper::{Method, Uri}; +use jsonrpsee::RpcModule; use jsonrpsee::server::middleware::proxy_get_request::ProxyGetRequestLayer; use jsonrpsee::server::{AllowHosts, ServerBuilder, ServerHandle}; -use jsonrpsee::RpcModule; -use katana_core::backend::storage::Blockchain; use katana_core::backend::Backend; +use katana_core::backend::storage::Blockchain; use katana_core::env::BlockContextGenerator; use katana_core::service::block_producer::BlockProducer; use katana_core::service::messaging::MessagingConfig; use katana_db::mdbx::DbEnv; use katana_executor::implementation::blockifier::BlockifierFactory; use katana_executor::{ExecutionFlags, ExecutorFactory}; -use katana_pipeline::{stage, Pipeline}; +use katana_pipeline::{Pipeline, stage}; +use katana_pool::TxPool; use katana_pool::ordering::FiFo; use katana_pool::validation::stateful::TxValidator; -use katana_pool::TxPool; use katana_primitives::env::{CfgEnv, FeeTokenAddressses}; use katana_rpc::dev::DevApi; use katana_rpc::metrics::RpcServerMetrics; use katana_rpc::saya::SayaApi; -use katana_rpc::starknet::forking::ForkedClient; use katana_rpc::starknet::StarknetApi; +use katana_rpc::starknet::forking::ForkedClient; use katana_rpc::torii::ToriiApi; use katana_rpc_api::dev::DevApiServer; use katana_rpc_api::saya::SayaApiServer; @@ -101,6 +101,7 @@ impl Node { // TODO: maybe move this to the build stage if let Some(ref cfg) = self.metrics_config { + let addr = cfg.socket_addr(); let mut reports: Vec> = Vec::new(); if let Some(ref db) = self.db {