Skip to content

Commit

Permalink
feat: added span setting and cmd line arg
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd committed Sep 4, 2023
1 parent d4e64c2 commit 02eee1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main() -> anyhow::Result<()> {

task_scope(|scope| {
async move {
let mut start_logger_server_fn = Some(utilities::init_json_logger().await);
let mut start_logger_server_fn = Some(utilities::init_json_logger(settings.logging.span_lifecycle).await);

let ws_rpc_client = jsonrpsee::ws_client::WsClientBuilder::default()
.build(&settings.state_chain.ws_endpoint)
Expand Down
21 changes: 21 additions & 0 deletions engine/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ pub struct HealthCheck {
pub port: Port,
}

#[derive(Debug, Deserialize, Clone, Default, PartialEq, Eq)]
pub struct Logging {
pub span_lifecycle: bool,
}

#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Signing {
#[serde(deserialize_with = "deser_path")]
Expand All @@ -115,6 +120,7 @@ pub struct Settings {

pub health_check: Option<HealthCheck>,
pub signing: Signing,
pub logging: Logging,
}

#[derive(Parser, Debug, Clone, Default)]
Expand Down Expand Up @@ -196,6 +202,10 @@ pub struct CommandLineOptions {
// Signing Settings
#[clap(long = "signing.db_file", parse(from_os_str))]
signing_db_file: Option<PathBuf>,

// Logging settings
#[clap(long = "logging.span_lifecycle")]
logging_span_lifecycle: bool,
}

impl Default for CommandLineOptions {
Expand All @@ -210,6 +220,7 @@ impl Default for CommandLineOptions {
health_check_hostname: None,
health_check_port: None,
signing_db_file: None,
logging_span_lifecycle: false,
}
}
}
Expand All @@ -225,6 +236,8 @@ const ETH_PRIVATE_KEY_FILE: &str = "eth.private_key_file";

const SIGNING_DB_FILE: &str = "signing.db_file";

const LOGGING_SPAN_LIFECYCLE: &str = "logging.span_lifecycle";

// We use PathBuf because the value must be Sized, Path is not Sized
fn deser_path<'de, D>(deserializer: D) -> std::result::Result<PathBuf, D::Error>
where
Expand Down Expand Up @@ -343,6 +356,7 @@ impl CfSettings for Settings {
) -> Result<ConfigBuilder<config::builder::DefaultState>, ConfigError> {
config_builder
.set_default(NODE_P2P_ALLOW_LOCAL_IP, false)?
.set_default(LOGGING_SPAN_LIFECYCLE, false)?
.set_default(
NODE_P2P_KEY_FILE,
PathBuf::from(config_root)
Expand Down Expand Up @@ -397,6 +411,11 @@ impl Source for CommandLineOptions {
insert_command_line_option(&mut map, "health_check.hostname", &self.health_check_hostname);
insert_command_line_option(&mut map, "health_check.port", &self.health_check_port);
insert_command_line_option_path(&mut map, SIGNING_DB_FILE, &self.signing_db_file);
insert_command_line_option(
&mut map,
LOGGING_SPAN_LIFECYCLE,
&Some(self.logging_span_lifecycle),
);

Ok(map)
}
Expand Down Expand Up @@ -680,12 +699,14 @@ mod tests {
health_check_hostname: Some("health_check_hostname".to_owned()),
health_check_port: Some(1337),
signing_db_file: Some(PathBuf::from_str("also/not/real.db").unwrap()),
logging_span_lifecycle: true,
};

// Load the test opts into the settings
let settings = Settings::new(opts.clone()).unwrap();

// Compare the opts and the settings
assert_eq!(opts.logging_span_lifecycle, settings.logging.span_lifecycle);
assert_eq!(opts.p2p_opts.node_key_file.unwrap(), settings.node_p2p.node_key_file);
assert_eq!(opts.p2p_opts.p2p_port.unwrap(), settings.node_p2p.port);
assert_eq!(opts.p2p_opts.ip_address.unwrap(), settings.node_p2p.ip_address);
Expand Down
8 changes: 6 additions & 2 deletions utilities/src/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,14 @@ macro_rules! print_starting {
/// '"debug,warp=off,hyper=off,jsonrpc=off,web3=off,reqwest=off"' 127.0.0.1:36079/tracing
///
/// The full syntax used for specifying filter directives used in both the REST api and in the RUST_LOG environment variable is specified here: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html
pub async fn init_json_logger() -> impl FnOnce(&task_scope::Scope<'_, anyhow::Error>) {
pub async fn init_json_logger(
log_span_lifecycle: bool,
) -> impl FnOnce(&task_scope::Scope<'_, anyhow::Error>) {
use tracing::metadata::LevelFilter;
use tracing_subscriber::EnvFilter;

let format_span = if log_span_lifecycle { FmtSpan::FULL } else { FmtSpan::NONE };

let reload_handle = {
let builder = tracing_subscriber::fmt()
.json()
Expand All @@ -323,7 +327,7 @@ pub async fn init_json_logger() -> impl FnOnce(&task_scope::Scope<'_, anyhow::Er
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.with_span_events(FmtSpan::FULL)
.with_span_events(format_span)
.with_filter_reloading();

let reload_handle = builder.reload_handle();
Expand Down

0 comments on commit 02eee1f

Please sign in to comment.