Skip to content

Commit

Permalink
feat: use specific message for slow query logs
Browse files Browse the repository at this point in the history
closes #2669
  • Loading branch information
abonander committed Nov 16, 2023
1 parent 16eeea8 commit b03c23f
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions sqlx-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ impl<'q> QueryLogger<'q> {
pub fn finish(&self) {
let elapsed = self.start.elapsed();

let lvl = if elapsed >= self.settings.slow_statements_duration {
let was_slow = elapsed >= self.settings.slow_statements_duration;

let lvl = if was_slow {
self.settings.slow_statements_level
} else {
self.settings.statements_level
Expand Down Expand Up @@ -114,15 +116,32 @@ impl<'q> QueryLogger<'q> {
String::new()
};

private_tracing_dynamic_event!(
target: "sqlx::query",
tracing_level,
summary,
db.statement = sql,
rows_affected = self.rows_affected,
rows_returned= self.rows_returned,
?elapsed,
);
if was_slow {
private_tracing_dynamic_event!(
target: "sqlx::query",
tracing_level,
summary,
db.statement = sql,
rows_affected = self.rows_affected,
rows_returned = self.rows_returned,
?elapsed,
// When logging to JSON, one can trigger alerts from the presence of this field.
slow_threshold=?self.settings.slow_statements_duration,
// Make sure to use "slow" in the message as that's likely
// what people will grep for.
"slow statement: execution time exceeded alert threshold"
);
} else {
private_tracing_dynamic_event!(
target: "sqlx::query",
tracing_level,
summary,
db.statement = sql,
rows_affected = self.rows_affected,
rows_returned = self.rows_returned,
?elapsed,
);
}
}
}
}
Expand Down

0 comments on commit b03c23f

Please sign in to comment.