Skip to content

Commit 3ef91d5

Browse files
authored
perf: avoid unnecessary clones of the log context (#4451)
2 parents 7d17f34 + 2a99af6 commit 3ef91d5

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ 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+
})
123+
.transpose()?;
123124

124125
let field_fromatter = DefaultFields::new();
125126
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,14 +789,14 @@ 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) =
793+
LogContext::maybe_with(mas_context::LogContext::stats)
794+
else {
793795
// This should never happen, but if it does it's fine: we're recovering fine
794796
// from panics in those tasks
795797
panic!("Missing log context, this should never happen");
796798
};
797799

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

0 commit comments

Comments
 (0)