Skip to content

Commit

Permalink
fix(sidecar): init fmt at the absolute beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevbirb committed Nov 8, 2024
1 parent 9d7b6f0 commit 328dcf9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
46 changes: 36 additions & 10 deletions bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use clap::Parser;
use eyre::{bail, Result};
use eyre::bail;
use tracing::info;

use tracing_subscriber::{
fmt::Layer as FmtLayer, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
Registry,
};

use bolt_sidecar::{
config::{strip_empty_envs, Opts},
config::{remove_empty_envs, Opts},
telemetry::init_telemetry_stack,
SidecarDriver,
};
Expand All @@ -17,16 +22,13 @@ const BOLT: &str = r#"
╚═════╝ ╚═════╝ ╚══════╝╚═╝ "#;

#[tokio::main]
async fn main() -> Result<()> {
if dotenvy::dotenv().is_ok() {
strip_empty_envs()?;
info!("Loaded .env file");
}
async fn main() -> eyre::Result<()> {
init_tracing()?;
read_env_file()?;

let opts = Opts::parse();

if let Err(err) = init_telemetry_stack(opts.telemetry.metrics_port()) {
bail!("Failed to initialize telemetry stack: {:?}", err)
}
init_telemetry_stack(opts.telemetry.metrics_port())?;

println!("{BOLT}");

Expand Down Expand Up @@ -55,3 +57,27 @@ async fn main() -> Result<()> {
}
}
}

fn init_tracing() -> eyre::Result<()> {
let std_layer = FmtLayer::default().with_writer(std::io::stdout).with_filter(
EnvFilter::builder()
.with_default_directive("bolt_sidecar=info".parse()?)
.from_env_lossy()
.add_directive("reqwest=error".parse()?)
.add_directive("alloy_transport_http=error".parse()?),
);
Registry::default().with(std_layer).try_init()?;
Ok(())
}

fn read_env_file() -> eyre::Result<()> {
match dotenvy::dotenv() {
// It means the .env file hasn't been found but it's okay since it's optional
Err(dotenvy::Error::Io(_)) => (),
Err(err) => bail!("Failed to load .env file: {:?}", err),
Ok(path) => info!(path = ?path, "Loaded .env file"),
};

remove_empty_envs()?;
Ok(())
}
14 changes: 9 additions & 5 deletions bolt-sidecar/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use alloy::primitives::Address;
use clap::Parser;
use reqwest::Url;
Expand All @@ -18,6 +20,7 @@ use telemetry::TelemetryOpts;
/// Operating limits for commitments and constraints.
pub mod limits;
use limits::LimitsOpts;
use tracing::debug;

use crate::common::{BlsSecretKeyWrapper, EcdsaSecretKeyWrapper, JwtSecretConfig};

Expand Down Expand Up @@ -103,10 +106,11 @@ pub struct Opts {
/// It removes environment variables that are set as empty strings, i.e. like `MY_VAR=`. This is
/// useful to avoid unexpected edge cases and because we don't have options that make sense with an
/// empty string value.
pub fn strip_empty_envs() -> eyre::Result<()> {
for item in dotenvy::dotenv_iter()? {
let (key, val) = item?;
if val.is_empty() {
pub fn remove_empty_envs() -> eyre::Result<()> {
for item in env::vars() {
let (key, val) = item;
if val.trim().is_empty() {
debug!("removing empty env var: {}", key);
std::env::remove_var(key)
}
}
Expand All @@ -124,7 +128,7 @@ mod tests {
#[ignore = "Doesn't need to run in CI, only for local development"]
fn test_strip_empty_envs() {
let _ = dotenv().expect("to load .env file");
strip_empty_envs().expect("to strip empty envs");
remove_empty_envs().expect("to strip empty envs");
let opts = Opts::parse();
println!("{:#?}", opts);
}
Expand Down
15 changes: 0 additions & 15 deletions bolt-sidecar/src/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use std::net::SocketAddr;
use eyre::{bail, Result};
use metrics_exporter_prometheus::PrometheusBuilder;
use tracing::info;
use tracing_subscriber::{
fmt::Layer as FmtLayer, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
Registry,
};

mod metrics;
pub use metrics::ApiMetrics;
Expand All @@ -15,17 +11,6 @@ pub use metrics::ApiMetrics;
///
/// **This function should be called at the beginning of the program.**
pub fn init_telemetry_stack(metrics_port: Option<u16>) -> Result<()> {
// 1. Initialize tracing to stdout
let std_layer = FmtLayer::default().with_writer(std::io::stdout).with_filter(
EnvFilter::builder()
.with_default_directive("bolt_sidecar=info".parse()?)
.from_env_lossy()
.add_directive("reqwest=error".parse()?)
.add_directive("alloy_transport_http=error".parse()?),
);
Registry::default().with(std_layer).try_init()?;

// 2. Initialize metrics recorder and start the Prometheus server
if let Some(metrics_port) = metrics_port {
let prometheus_addr = SocketAddr::from(([0, 0, 0, 0], metrics_port));
let builder = PrometheusBuilder::new().with_http_listener(prometheus_addr);
Expand Down

0 comments on commit 328dcf9

Please sign in to comment.