Skip to content

Commit 16d2184

Browse files
committed
Improve error logging
Prior to this commit, some log messages were lost. That happened to error messages created **before** the tracing system was initialized. Starting from this commit, we will print error messages to the stderr when the tracing system is not yet ready. Signed-off-by: Flavio Castelli <fcastelli@suse.com>
1 parent 281ba74 commit 16d2184

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/main.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ extern crate kube;
33
extern crate policy_evaluator;
44

55
use anyhow::Result;
6+
use lazy_static::lazy_static;
67
use opentelemetry::global::shutdown_tracer_provider;
78
use policy_evaluator::callback_handler::CallbackHandlerBuilder;
8-
use policy_evaluator::policy_metadata::Metadata;
9-
use std::path::PathBuf;
10-
use std::{process, thread};
9+
use std::{path::PathBuf, process, sync::RwLock, thread};
1110
use tokio::{runtime::Runtime, sync::mpsc, sync::oneshot};
1211
use tracing::{debug, error, info};
1312

@@ -18,7 +17,6 @@ mod kube_poller;
1817
mod metrics;
1918
mod server;
2019
mod settings;
21-
mod sigstore;
2220
mod utils;
2321
mod worker;
2422

@@ -28,6 +26,10 @@ use worker_pool::WorkerPool;
2826
mod communication;
2927
use communication::{EvalRequest, KubePollerBootRequest, WorkerPoolBootRequest};
3028

29+
lazy_static! {
30+
static ref TRACE_SYSTEM_INITIALIZED: RwLock<bool> = RwLock::new(false);
31+
}
32+
3133
fn main() -> Result<()> {
3234
let matches = cli::build_cli().get_matches();
3335

@@ -150,9 +152,17 @@ fn main() -> Result<()> {
150152
rt.block_on(async {
151153
// Setup the tracing system. This MUST be done inside of a tokio Runtime
152154
// because some collectors rely on it and would panic otherwise.
153-
if let Err(err) = cli::setup_tracing(&matches) {
154-
fatal_error(err.to_string());
155-
}
155+
match cli::setup_tracing(&matches) {
156+
Err(err) => {
157+
fatal_error(err.to_string());
158+
unreachable!();
159+
}
160+
Ok(_) => {
161+
debug!("tracing system ready");
162+
let mut w = TRACE_SYSTEM_INITIALIZED.write().unwrap();
163+
*w = true;
164+
}
165+
};
156166

157167
// The unused variable is required so the meter is not dropped early and
158168
// lives for the whole block lifetime, exporting metrics
@@ -416,8 +426,13 @@ fn main() -> Result<()> {
416426
}
417427

418428
fn fatal_error(msg: String) {
419-
error!("{}", msg);
420-
shutdown_tracer_provider();
429+
let trace_system_ready = TRACE_SYSTEM_INITIALIZED.read().unwrap();
430+
if *trace_system_ready {
431+
error!("{}", msg);
432+
shutdown_tracer_provider();
433+
} else {
434+
eprintln!("{}", msg);
435+
}
421436

422437
process::exit(1);
423438
}

0 commit comments

Comments
 (0)