Initial work to switch to tracing (#2185)

* Add tracing dep

* Switch over basic events

* Switch over dynamically enabled events

* Fix missing SocketAddr formatting

* More format fixing

* refactor: Apply tracing changes to new crate structure
This commit is contained in:
CosmicHorror
2023-02-10 18:35:12 -07:00
committed by Austin Bonander
parent b72a52b066
commit acaee75a30
16 changed files with 183 additions and 119 deletions

View File

@@ -40,8 +40,8 @@ pub(crate) fn describe(conn: &mut ConnectionState, query: &str) -> Result<Descri
// if explain.. fails, ignore the failure and we'll have no fallback
let (fallback, fallback_nullable) = match explain(conn, stmt.handle.sql()) {
Ok(v) => v,
Err(err) => {
log::debug!("describe: explain introspection failed: {}", err);
Err(error) => {
tracing::debug!(%error, "describe: explain introspection failed");
(vec![], vec![])
}

View File

@@ -175,7 +175,7 @@ impl ConnectionWorker {
// `Transaction` was created and so there is no way to commit /
// rollback this transaction. We need to roll it back
// immediately otherwise it would remain started forever.
if let Err(e) = conn
if let Err(error) = conn
.handle
.exec(rollback_ansi_transaction_sql(depth + 1))
.map(|_| {
@@ -185,7 +185,7 @@ impl ConnectionWorker {
// The rollback failed. To prevent leaving the connection
// in an inconsistent state we shutdown this worker which
// causes any subsequent operation on the connection to fail.
log::error!("failed to rollback cancelled transaction: {}", e);
tracing::error!(%error, "failed to rollback cancelled transaction");
break;
}
}
@@ -242,8 +242,8 @@ impl ConnectionWorker {
}
}
Command::CreateCollation { create_collation } => {
if let Err(e) = (create_collation)(&mut conn) {
log::warn!("error applying collation in background worker: {}", e);
if let Err(error) = (create_collation)(&mut conn) {
tracing::warn!(%error, "error applying collation in background worker");
}
}
Command::ClearCache { tx } => {

View File

@@ -1,4 +1,4 @@
use sqlx_core::connection::LogSettings;
use sqlx_core::{connection::LogSettings, logger};
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
@@ -25,15 +25,13 @@ impl<'q, O: Debug + Hash + Eq, R: Debug, P: Debug> QueryPlanLogger<'q, O, R, P>
}
pub fn log_enabled(&self) -> bool {
if let Some(_lvl) = self
.settings
.statements_level
.to_level()
.filter(|lvl| log::log_enabled!(target: "sqlx::explain", *lvl))
if let Some((tracing_level, log_level)) =
logger::private_level_filter_to_levels(self.settings.statements_level)
{
return true;
log::log_enabled!(log_level)
|| sqlx_core::private_tracing_dynamic_enabled!(tracing_level)
} else {
return false;
false
}
}
@@ -48,37 +46,37 @@ impl<'q, O: Debug + Hash + Eq, R: Debug, P: Debug> QueryPlanLogger<'q, O, R, P>
pub fn finish(&self) {
let lvl = self.settings.statements_level;
if let Some(lvl) = lvl
.to_level()
.filter(|lvl| log::log_enabled!(target: "sqlx::explain", *lvl))
{
let mut summary = parse_query_summary(&self.sql);
if let Some((tracing_level, log_level)) = logger::private_level_filter_to_levels(lvl) {
let log_is_enabled = log::log_enabled!(target: "sqlx::explain", log_level)
|| private_tracing_dynamic_enabled!(target: "sqlx::explain", tracing_level);
if log_is_enabled {
let mut summary = parse_query_summary(&self.sql);
let sql = if summary != self.sql {
summary.push_str("");
format!(
"\n\n{}\n",
sqlformat::format(
&self.sql,
&sqlformat::QueryParams::None,
sqlformat::FormatOptions::default()
let sql = if summary != self.sql {
summary.push_str("");
format!(
"\n\n{}\n",
sqlformat::format(
&self.sql,
&sqlformat::QueryParams::None,
sqlformat::FormatOptions::default()
)
)
)
} else {
String::new()
};
} else {
String::new()
};
log::logger().log(
&log::Record::builder()
.args(format_args!(
"{}; program:{:?}, unknown_operations:{:?}, results: {:?}{}",
summary, self.program, self.unknown_operations, self.results, sql
))
.level(lvl)
.module_path_static(Some("sqlx::explain"))
.target("sqlx::explain")
.build(),
);
let message = format!(
"{}; program:{:?}, unknown_operations:{:?}, results: {:?}{}",
summary, self.program, self.unknown_operations, self.results, sql
);
sqlx_core::private_tracing_dynamic_event!(
target: "sqlx::explain",
tracing_level,
message,
);
}
}
}
}