Skip to content

Commit

Permalink
refactor(katana): separate metrics address and port into 2 args (#2537)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Nov 1, 2024
1 parent af63e5f commit 126d33d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
63 changes: 44 additions & 19 deletions bin/katana/src/cli/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,42 @@
//! 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,
};
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;
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};
Expand Down Expand Up @@ -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<SocketAddr>,

#[arg(long)]
#[arg(value_name = "PATH")]
#[arg(value_parser = katana_core::service::messaging::MessagingConfig::parse)]
Expand All @@ -110,20 +103,46 @@ pub struct NodeArgs {
pub messaging: Option<MessagingConfig>,

#[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)]
Expand All @@ -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")]
Expand All @@ -168,7 +188,6 @@ pub struct StarknetOptions {
pub disable_validate: bool,

#[command(flatten)]
#[command(next_help_heading = "Environment options")]
pub environment: EnvironmentOptions,

#[arg(long)]
Expand All @@ -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.")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -369,7 +390,11 @@ impl NodeArgs {
}

fn metrics_config(&self) -> Option<MetricsConfig> {
self.metrics.map(|addr| MetricsConfig { addr })
if self.metrics.metrics {
Some(MetricsConfig { addr: self.metrics.metrics_addr, port: self.metrics.metrics_port })
} else {
None
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions crates/katana/node/src/config/metrics.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 6 additions & 5 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Box<dyn Report>> = Vec::new();

if let Some(ref db) = self.db {
Expand Down

0 comments on commit 126d33d

Please sign in to comment.