Skip to content

Commit

Permalink
chore: configure tracing only once
Browse files Browse the repository at this point in the history
commit-id:ad22a238
  • Loading branch information
nadin-Starkware committed Dec 10, 2024
1 parent f630f48 commit b162c22
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn tx_generator() -> MultiAccountTransactionGenerator {
#[rstest]
#[tokio::test]
async fn end_to_end_flow(mut tx_generator: MultiAccountTransactionGenerator) {
configure_tracing();
configure_tracing().await;

const LISTEN_TO_BROADCAST_MESSAGES_TIMEOUT: std::time::Duration =
std::time::Duration::from_secs(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn test_end_to_end_integration(mut tx_generator: MultiAccountTransactionGe

const EXPECTED_BLOCK_NUMBER: BlockNumber = BlockNumber(15);

configure_tracing();
configure_tracing().await;
info!("Running integration test setup.");

// Creating the storage for the test.
Expand Down
23 changes: 16 additions & 7 deletions crates/starknet_sequencer_infra/src/trace_util.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
use tokio::sync::OnceCell;
use tracing::metadata::LevelFilter;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

const DEFAULT_LEVEL: LevelFilter = LevelFilter::INFO;
// Define a OnceCell to ensure the configuration is initialized only once
static TRACING_INITIALIZED: OnceCell<()> = OnceCell::const_new();

pub fn configure_tracing() {
let fmt_layer = fmt::layer().compact().with_target(true);
let level_filter_layer =
EnvFilter::builder().with_default_directive(DEFAULT_LEVEL.into()).from_env_lossy();
pub async fn configure_tracing() {
TRACING_INITIALIZED
.get_or_init(|| async {
let fmt_layer = fmt::layer().compact().with_target(true);
let level_filter_layer =
EnvFilter::builder().with_default_directive(DEFAULT_LEVEL.into()).from_env_lossy();

// This sets a single subscriber to all of the threads. We may want to implement different
// subscriber for some threads and use set_global_default instead of init.
tracing_subscriber::registry().with(fmt_layer).with(level_filter_layer).init();
// This sets a single subscriber to all of the threads. We may want to implement
// different subscriber for some threads and use set_global_default instead
// of init.
tracing_subscriber::registry().with(fmt_layer).with(level_filter_layer).init();
tracing::info!("Tracing has been successfully initialized.");
})
.await;
}
2 changes: 1 addition & 1 deletion crates/starknet_sequencer_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::{error, info};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
configure_tracing();
configure_tracing().await;

let config = SequencerNodeConfig::load_and_process(args().collect());
if let Err(ConfigError::CommandInput(clap_err)) = config {
Expand Down

0 comments on commit b162c22

Please sign in to comment.