Skip to content

Commit 5628e84

Browse files
committed
fix: otel resiliency
1 parent ef46462 commit 5628e84

File tree

17 files changed

+155
-100
lines changed

17 files changed

+155
-100
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ jsonc-parser = { version = "=0.26.2", features = ["serde"] }
141141
lazy-regex = "3"
142142
libc = "0.2.126"
143143
libz-sys = { version = "1.1.20", default-features = false }
144-
log = "0.4.20"
144+
log = { version = "0.4.20", features = ["kv"] }
145145
lsp-types = "=0.97.0" # used by tower-lsp and "proposed" feature is unstable in patch releases
146146
memmem = "0.1.1"
147147
monch = "=0.5.0"

cli/args/flags.rs

+17
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use deno_path_util::normalize_path;
3636
use deno_path_util::url_to_file_path;
3737
use deno_runtime::deno_permissions::PermissionsOptions;
3838
use deno_runtime::deno_permissions::SysDescriptor;
39+
use deno_runtime::ops::otel::OtelConfig;
3940
use log::debug;
4041
use log::Level;
4142
use serde::Deserialize;
@@ -968,6 +969,22 @@ impl Flags {
968969
args
969970
}
970971

972+
pub fn otel_config(&self) -> Option<OtelConfig> {
973+
if self
974+
.unstable_config
975+
.features
976+
.contains(&String::from("otel"))
977+
{
978+
Some(OtelConfig {
979+
runtime_name: Cow::Borrowed("deno"),
980+
runtime_version: Cow::Borrowed(crate::version::DENO_VERSION_INFO.deno),
981+
..Default::default()
982+
})
983+
} else {
984+
None
985+
}
986+
}
987+
971988
/// Extract the paths the config file should be discovered from.
972989
///
973990
/// Returns `None` if the config file should not be auto-discovered.

cli/args/mod.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,8 @@ impl CliOptions {
823823
};
824824
let msg =
825825
format!("DANGER: TLS certificate validation is disabled {}", domains);
826-
#[allow(clippy::print_stderr)]
827826
{
828-
// use eprintln instead of log::warn so this always gets shown
829-
eprintln!("{}", colors::yellow(msg));
827+
log::error!("{}", colors::yellow(msg));
830828
}
831829
}
832830

@@ -1131,20 +1129,7 @@ impl CliOptions {
11311129
}
11321130

11331131
pub fn otel_config(&self) -> Option<OtelConfig> {
1134-
if self
1135-
.flags
1136-
.unstable_config
1137-
.features
1138-
.contains(&String::from("otel"))
1139-
{
1140-
Some(OtelConfig {
1141-
runtime_name: Cow::Borrowed("deno"),
1142-
runtime_version: Cow::Borrowed(crate::version::DENO_VERSION_INFO.deno),
1143-
..Default::default()
1144-
})
1145-
} else {
1146-
None
1147-
}
1132+
self.flags.otel_config()
11481133
}
11491134

11501135
pub fn env_file_name(&self) -> Option<&String> {

cli/main.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,17 @@ fn setup_panic_hook() {
350350
eprintln!("Args: {:?}", env::args().collect::<Vec<_>>());
351351
eprintln!();
352352
orig_hook(panic_info);
353-
std::process::exit(1);
353+
deno_runtime::exit(1);
354354
}));
355355
}
356356

357-
#[allow(clippy::print_stderr)]
358357
fn exit_with_message(message: &str, code: i32) -> ! {
359-
eprintln!(
358+
log::error!(
360359
"{}: {}",
361360
colors::red_bold("error"),
362361
message.trim_start_matches("error: ")
363362
);
364-
std::process::exit(code);
363+
deno_runtime::exit(code);
365364
}
366365

367366
fn exit_for_error(error: AnyError) -> ! {
@@ -380,13 +379,12 @@ fn exit_for_error(error: AnyError) -> ! {
380379
exit_with_message(&error_string, error_code);
381380
}
382381

383-
#[allow(clippy::print_stderr)]
384382
pub(crate) fn unstable_exit_cb(feature: &str, api_name: &str) {
385-
eprintln!(
383+
log::error!(
386384
"Unstable API '{api_name}'. The `--unstable-{}` flag must be provided.",
387385
feature
388386
);
389-
std::process::exit(70);
387+
deno_runtime::exit(70);
390388
}
391389

392390
pub fn main() {
@@ -415,11 +413,13 @@ pub fn main() {
415413

416414
let result = create_and_run_current_thread_with_maybe_metrics(future);
417415

416+
deno_runtime::ops::otel::flush();
417+
418418
#[cfg(feature = "dhat-heap")]
419419
drop(profiler);
420420

421421
match result {
422-
Ok(exit_code) => std::process::exit(exit_code),
422+
Ok(exit_code) => deno_runtime::exit(exit_code),
423423
Err(err) => exit_for_error(err),
424424
}
425425
}
@@ -434,11 +434,16 @@ fn resolve_flags_and_init(
434434
{
435435
// Ignore results to avoid BrokenPipe errors.
436436
let _ = err.print();
437-
std::process::exit(0);
437+
deno_runtime::exit(0);
438438
}
439439
Err(err) => exit_for_error(AnyError::from(err)),
440440
};
441441

442+
if let Some(otel_config) = flags.otel_config() {
443+
deno_runtime::ops::otel::init(otel_config)?;
444+
}
445+
util::logger::init(flags.log_level);
446+
442447
// TODO(bartlomieju): remove in Deno v2.5 and hard error then.
443448
if flags.unstable_config.legacy_flag_enabled {
444449
log::warn!(
@@ -467,7 +472,6 @@ fn resolve_flags_and_init(
467472
deno_core::JsRuntime::init_platform(
468473
None, /* import assertions enabled */ false,
469474
);
470-
util::logger::init(flags.log_level);
471475

472476
Ok(flags)
473477
}

cli/mainrt.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,21 @@ use std::env::current_exe;
4040

4141
use crate::args::Flags;
4242

43-
#[allow(clippy::print_stderr)]
4443
pub(crate) fn unstable_exit_cb(feature: &str, api_name: &str) {
45-
eprintln!(
44+
log::error!(
4645
"Unstable API '{api_name}'. The `--unstable-{}` flag must be provided.",
4746
feature
4847
);
49-
std::process::exit(70);
48+
deno_runtime::exit(70);
5049
}
5150

52-
#[allow(clippy::print_stderr)]
5351
fn exit_with_message(message: &str, code: i32) -> ! {
54-
eprintln!(
52+
log::error!(
5553
"{}: {}",
5654
colors::red_bold("error"),
5755
message.trim_start_matches("error: ")
5856
);
59-
std::process::exit(code);
57+
deno_runtime::exit(code);
6058
}
6159

6260
fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
@@ -92,7 +90,7 @@ fn main() {
9290
util::logger::init(data.metadata.log_level);
9391
load_env_vars(&data.metadata.env_vars_from_env_file);
9492
let exit_code = standalone::run(data).await?;
95-
std::process::exit(exit_code);
93+
deno_runtime::exit(exit_code);
9694
}
9795
Ok(None) => Ok(()),
9896
Err(err) => Err(err),

cli/standalone/binary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
517517
Some(bytes) => bytes,
518518
None => {
519519
log::info!("Download could not be found, aborting");
520-
std::process::exit(1)
520+
deno_runtime::exit(1);
521521
}
522522
};
523523

cli/util/file_watcher.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ impl DebouncedReceiver {
7373
}
7474
}
7575

76-
#[allow(clippy::print_stderr)]
7776
async fn error_handler<F>(watch_future: F) -> bool
7877
where
7978
F: Future<Output = Result<(), AnyError>>,
@@ -84,7 +83,7 @@ where
8483
Some(e) => format_js_error(e),
8584
None => format!("{err:?}"),
8685
};
87-
eprintln!(
86+
log::error!(
8887
"{}: {}",
8988
colors::red_bold("error"),
9089
error_string.trim_start_matches("error: ")

cli/util/logger.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl log::Log for CliLogger {
2929
// thread's state
3030
DrawThread::hide();
3131
self.0.log(record);
32+
deno_runtime::ops::otel::handle_log(record);
3233
DrawThread::show();
3334
}
3435
}

cli/util/v8.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ pub fn init_v8_flags(
4646
.skip(1)
4747
.collect::<Vec<_>>();
4848

49-
#[allow(clippy::print_stderr)]
5049
if !unrecognized_v8_flags.is_empty() {
5150
for f in unrecognized_v8_flags {
52-
eprintln!("error: V8 did not recognize flag '{f}'");
51+
log::error!("error: V8 did not recognize flag '{f}'");
5352
}
54-
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
55-
std::process::exit(1);
53+
log::error!("\nFor a list of V8 flags, use '--v8-flags=--help'");
54+
deno_runtime::exit(1);
5655
}
5756
if v8_flags_includes_help {
58-
std::process::exit(0);
57+
deno_runtime::exit(0);
5958
}
6059
}

ext/napi/node_api.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ fn napi_fatal_exception(env: &mut Env, err: napi_value) -> napi_status {
140140
}
141141

142142
#[napi_sym]
143-
#[allow(clippy::print_stderr)]
144143
fn napi_fatal_error(
145144
location: *const c_char,
146145
location_len: usize,
@@ -173,9 +172,9 @@ fn napi_fatal_error(
173172
};
174173

175174
if let Some(location) = location {
176-
eprintln!("NODE API FATAL ERROR: {} {}", location, message);
175+
log::error!("NODE API FATAL ERROR: {} {}", location, message);
177176
} else {
178-
eprintln!("NODE API FATAL ERROR: {}", message);
177+
log::error!("NODE API FATAL ERROR: {}", message);
179178
}
180179

181180
std::process::abort();

ext/napi/uv.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use deno_core::parking_lot::Mutex;
55
use std::mem::MaybeUninit;
66
use std::ptr::addr_of_mut;
77

8-
#[allow(clippy::print_stderr)]
98
fn assert_ok(res: c_int) -> c_int {
109
if res != 0 {
11-
eprintln!("bad result in uv polyfill: {res}");
10+
log::error!("bad result in uv polyfill: {res}");
1211
// don't panic because that might unwind into
1312
// c/c++
1413
std::process::abort();

runtime/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ pub static UNSTABLE_GRANULAR_FLAGS: &[UnstableGranularFlag] = &[
140140
},
141141
];
142142

143+
pub fn exit(code: i32) -> ! {
144+
crate::ops::otel::flush();
145+
std::process::exit(code);
146+
}
147+
143148
#[cfg(test)]
144149
mod test {
145150
use super::*;

runtime/ops/os/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,8 @@ fn op_get_exit_code(state: &mut OpState) -> i32 {
186186

187187
#[op2(fast)]
188188
fn op_exit(state: &mut OpState) {
189-
crate::ops::otel::otel_drop_state(state);
190-
191189
let code = state.borrow::<ExitCode>().get();
192-
std::process::exit(code)
190+
crate::exit(code)
193191
}
194192

195193
#[op2]

0 commit comments

Comments
 (0)