perf: avoid unnecessary clones of the log context
This commit is contained in:
@@ -184,13 +184,11 @@ async fn log_response_middleware(
|
|||||||
|
|
||||||
let response = next.run(request).await;
|
let response = next.run(request).await;
|
||||||
|
|
||||||
let Some(log_context) = LogContext::current() else {
|
let Some(stats) = LogContext::maybe_with(LogContext::stats) else {
|
||||||
tracing::error!("Missing log context for request, this is a bug!");
|
tracing::error!("Missing log context for request, this is a bug!");
|
||||||
return response;
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
let stats = log_context.stats();
|
|
||||||
|
|
||||||
let status_code = response.status();
|
let status_code = response.status();
|
||||||
match status_code.as_u16() {
|
match status_code.as_u16() {
|
||||||
100..=399 => tracing::info!(
|
100..=399 => tracing::info!(
|
||||||
|
|||||||
@@ -113,13 +113,14 @@ where
|
|||||||
write!(&mut writer, "{} ", style.apply_to(metadata.name()))?;
|
write!(&mut writer, "{} ", style.apply_to(metadata.name()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(log_context) = LogContext::current() {
|
LogContext::maybe_with(|log_context| {
|
||||||
let log_context = Style::new()
|
let log_context = Style::new()
|
||||||
.bold()
|
.bold()
|
||||||
.force_styling(ansi)
|
.force_styling(ansi)
|
||||||
.apply_to(log_context);
|
.apply_to(log_context);
|
||||||
write!(&mut writer, "{log_context} - ")?;
|
write!(&mut writer, "{log_context} - ")
|
||||||
}
|
})
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
let field_fromatter = DefaultFields::new();
|
let field_fromatter = DefaultFields::new();
|
||||||
field_fromatter.format_fields(writer.by_ref(), event)?;
|
field_fromatter.format_fields(writer.by_ref(), event)?;
|
||||||
|
|||||||
@@ -76,9 +76,12 @@ impl LogContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a copy of the current log context, if any
|
/// Run a closure with the current log context, if any
|
||||||
pub fn current() -> Option<Self> {
|
pub fn maybe_with<F, R>(f: F) -> Option<R>
|
||||||
CURRENT_LOG_CONTEXT.try_with(Self::clone).ok()
|
where
|
||||||
|
F: FnOnce(&Self) -> R,
|
||||||
|
{
|
||||||
|
CURRENT_LOG_CONTEXT.try_with(f).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the async function `f` with the given log context. It will wrap the
|
/// Run the async function `f` with the given log context. It will wrap the
|
||||||
|
|||||||
@@ -789,14 +789,14 @@ impl JobTracker {
|
|||||||
);
|
);
|
||||||
let result = job.run(&state, context.clone()).await;
|
let result = job.run(&state, context.clone()).await;
|
||||||
|
|
||||||
let Some(log_context) = LogContext::current() else {
|
let Some(context_stats) =
|
||||||
|
LogContext::maybe_with(mas_context::LogContext::stats)
|
||||||
|
else {
|
||||||
// This should never happen, but if it does it's fine: we're recovering fine
|
// This should never happen, but if it does it's fine: we're recovering fine
|
||||||
// from panics in those tasks
|
// from panics in those tasks
|
||||||
panic!("Missing log context, this should never happen");
|
panic!("Missing log context, this should never happen");
|
||||||
};
|
};
|
||||||
|
|
||||||
let context_stats = log_context.stats();
|
|
||||||
|
|
||||||
// We log the result here so that it's attached to the right span & log context
|
// We log the result here so that it's attached to the right span & log context
|
||||||
match &result {
|
match &result {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user