Skip to content

Commit d815697

Browse files
committed
perf: avoid unnecessary clones of the log context
1 parent 7e715fb commit d815697

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

crates/cli/src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
#![allow(clippy::module_name_repetitions)]
88

9-
use std::{io::IsTerminal, process::ExitCode, sync::Arc};
9+
use std::{
10+
io::IsTerminal,
11+
process::ExitCode,
12+
sync::{
13+
Arc,
14+
atomic::{AtomicU64, Ordering},
15+
},
16+
};
1017

1118
use anyhow::Context;
1219
use clap::Parser;
@@ -52,6 +59,10 @@ impl sentry::TransportFactory for SentryTransportFactory {
5259
fn main() -> anyhow::Result<ExitCode> {
5360
let mut builder = tokio::runtime::Builder::new_multi_thread();
5461
builder.enable_all();
62+
builder.thread_name_fn(|| {
63+
static IDX: AtomicU64 = AtomicU64::new(0);
64+
format!("tokio-{}", IDX.fetch_add(1, Ordering::Relaxed))
65+
});
5566

5667
#[cfg(tokio_unstable)]
5768
builder

crates/cli/src/server.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,11 @@ async fn log_response_middleware(
184184

185185
let response = next.run(request).await;
186186

187-
let Some(log_context) = LogContext::current() else {
187+
let Some(stats) = LogContext::maybe_with(LogContext::stats) else {
188188
tracing::error!("Missing log context for request, this is a bug!");
189189
return response;
190190
};
191191

192-
let stats = log_context.stats();
193-
194192
let status_code = response.status();
195193
match status_code.as_u16() {
196194
100..=399 => tracing::info!(

crates/context/src/fmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ where
113113
write!(&mut writer, "{} ", style.apply_to(metadata.name()))?;
114114
}
115115

116-
if let Some(log_context) = LogContext::current() {
116+
LogContext::maybe_with(|log_context| {
117117
let log_context = Style::new()
118118
.bold()
119119
.force_styling(ansi)
120120
.apply_to(log_context);
121-
write!(&mut writer, "{log_context} - ")?;
122-
}
121+
write!(&mut writer, "{log_context} - ")
122+
}).transpose()?;
123123

124124
let field_fromatter = DefaultFields::new();
125125
field_fromatter.format_fields(writer.by_ref(), event)?;

crates/context/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ impl LogContext {
7676
}
7777
}
7878

79-
/// Get a copy of the current log context, if any
80-
pub fn current() -> Option<Self> {
81-
CURRENT_LOG_CONTEXT.try_with(Self::clone).ok()
79+
/// Run a closure with the current log context, if any
80+
pub fn maybe_with<F, R>(f: F) -> Option<R>
81+
where
82+
F: FnOnce(&Self) -> R,
83+
{
84+
CURRENT_LOG_CONTEXT.try_with(f).ok()
8285
}
8386

8487
/// Run the async function `f` with the given log context. It will wrap the

crates/tasks/src/new_queue.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,14 +789,12 @@ impl JobTracker {
789789
);
790790
let result = job.run(&state, context.clone()).await;
791791

792-
let Some(log_context) = LogContext::current() else {
792+
let Some(context_stats) = LogContext::maybe_with(mas_context::LogContext::stats) else {
793793
// This should never happen, but if it does it's fine: we're recovering fine
794794
// from panics in those tasks
795795
panic!("Missing log context, this should never happen");
796796
};
797797

798-
let context_stats = log_context.stats();
799-
800798
// We log the result here so that it's attached to the right span & log context
801799
match &result {
802800
Ok(()) => {

0 commit comments

Comments
 (0)