From f7465746f011b14316f91c6a3f0bd08d237b4812 Mon Sep 17 00:00:00 2001 From: Roman Date: Fri, 6 Dec 2024 22:22:42 +0800 Subject: [PATCH] chore: log config --- Cargo.toml | 1 + nomos-cluster-tests/Cargo.toml | 14 +++++ nomos-cluster-tests/README.md | 4 ++ nomos-cluster-tests/config.yaml | 5 ++ nomos-cluster-tests/src/config.rs | 101 ++++++++++++++++++++++++++++++ nomos-cluster-tests/src/main.rs | 34 ++++++++++ 6 files changed, 159 insertions(+) create mode 100644 nomos-cluster-tests/Cargo.toml create mode 100644 nomos-cluster-tests/README.md create mode 100644 nomos-cluster-tests/config.yaml create mode 100644 nomos-cluster-tests/src/config.rs create mode 100644 nomos-cluster-tests/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 91fc6861..08a335f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ members = [ "testnet/cfgsync", "tests", "clients/executor-http-client", + "nomos-cluster-tests", ] exclude = ["proof_of_leadership/risc0/risc0_proofs", "nomos-core/risc0_proofs"] resolver = "2" diff --git a/nomos-cluster-tests/Cargo.toml b/nomos-cluster-tests/Cargo.toml new file mode 100644 index 00000000..994fa023 --- /dev/null +++ b/nomos-cluster-tests/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "nomos-cluster-tests" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4", features = ["derive", "env"] } +serde_yaml = "0.9" +serde = { version = "1.0.215", features = ["derive"] } +nomos-tracing = { path = "../nomos-tracing" } +nomos-tracing-service = { path = "../nomos-services/tracing" } +overwatch-rs = { git = "https://github.com/logos-co/Overwatch", rev = "2f70806" } +color-eyre = "0.6.0" +tracing = "0.1" \ No newline at end of file diff --git a/nomos-cluster-tests/README.md b/nomos-cluster-tests/README.md new file mode 100644 index 00000000..742d8181 --- /dev/null +++ b/nomos-cluster-tests/README.md @@ -0,0 +1,4 @@ +# Nomos Cluster Tests + +Suite of small scale distributed tests for Nomos + diff --git a/nomos-cluster-tests/config.yaml b/nomos-cluster-tests/config.yaml new file mode 100644 index 00000000..af548e39 --- /dev/null +++ b/nomos-cluster-tests/config.yaml @@ -0,0 +1,5 @@ +log: + backend: "Stdout" + format: "Json" + level: "debug" + diff --git a/nomos-cluster-tests/src/config.rs b/nomos-cluster-tests/src/config.rs new file mode 100644 index 00000000..ed7d5b76 --- /dev/null +++ b/nomos-cluster-tests/src/config.rs @@ -0,0 +1,101 @@ +use std::net::ToSocketAddrs; +use std::path::PathBuf; +use clap::{Parser, ValueEnum}; +use serde::{Deserialize, Serialize}; +use color_eyre::eyre::{eyre, Result}; +use tracing::Level; + +use nomos_tracing_service::{LoggerLayer, Tracing}; +use overwatch_rs::services::ServiceData; +use nomos_tracing::logging::gelf::GelfConfig; +use nomos_tracing::logging::local::FileConfig; + +#[derive(ValueEnum, Clone, Debug, Default)] +pub enum LoggerLayerType { + Gelf, + File, + #[default] + Stdout, + Stderr, +} + + +#[derive(Parser, Debug, Clone)] +pub struct LogArgs { + /// Address for the Gelf backend + #[clap(long = "log-addr", env = "LOG_ADDR", required_if_eq("backend", "Gelf"))] + log_addr: Option, + + /// Directory for the File backend + #[clap(long = "log-dir", env = "LOG_DIR", required_if_eq("backend", "File"))] + directory: Option, + + /// Prefix for the File backend + #[clap(long = "log-path", env = "LOG_PATH", required_if_eq("backend", "File"))] + prefix: Option, + + /// Backend type + #[clap(long = "log-backend", env = "LOG_BACKEND", value_enum)] + backend: Option, + + #[clap(long = "log-level", env = "LOG_LEVEL")] + level: Option, +} + +#[derive(Deserialize, Debug, Clone, Serialize)] +pub struct Config { + pub tracing: ::Settings, +} + +impl Config { + pub fn update_from_args( + mut self, + log_args: LogArgs, + ) -> Result { + update_tracing(&mut self.tracing, log_args)?; + Ok(self) + } +} + +pub fn update_tracing( + tracing: &mut ::Settings, + tracing_args: LogArgs, +) -> Result<()> { + let LogArgs { + backend, + log_addr: addr, + directory, + prefix, + level, + } = tracing_args; + + // Override the file config with the one from env variables. + if let Some(backend) = backend { + tracing.logger = match backend { + LoggerLayerType::Gelf => LoggerLayer::Gelf(GelfConfig { + addr: addr + .ok_or_else(|| eyre!("Gelf backend requires an address."))? + .to_socket_addrs()? + .next() + .ok_or_else(|| eyre!("Invalid gelf address"))?, + }), + LoggerLayerType::File => LoggerLayer::File(FileConfig { + directory: directory.ok_or_else(|| eyre!("File backend requires a directory."))?, + prefix, + }), + LoggerLayerType::Stdout => LoggerLayer::Stdout, + LoggerLayerType::Stderr => LoggerLayer::Stderr, + } + }; + + if let Some(level_str) = level { + tracing.level = match level_str.as_str() { + "DEBUG" => Level::DEBUG, + "INFO" => Level::INFO, + "ERROR" => Level::ERROR, + "WARN" => Level::WARN, + _ => return Err(eyre!("Invalid log level provided.")), + }; + } + Ok(()) +} \ No newline at end of file diff --git a/nomos-cluster-tests/src/main.rs b/nomos-cluster-tests/src/main.rs new file mode 100644 index 00000000..4d7f8a9b --- /dev/null +++ b/nomos-cluster-tests/src/main.rs @@ -0,0 +1,34 @@ +mod config; + +use clap::Parser; +use crate::config::{Config, LogArgs}; +use color_eyre::eyre::{Result}; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Path for a yaml-encoded network config file + config: std::path::PathBuf, + /// Overrides log config. + #[clap(flatten)] + log_args: LogArgs, +} + + +fn main() -> Result<()> { + + // Parse cluster options + let Args { + config, + log_args, + } = Args::parse(); + let config = serde_yaml::from_reader::<_, Config>(std::fs::File::open(config)?)? + .update_from_args( + log_args, + )?; + + // Run the test suite + + + Ok(()) +}