Skip to content

Commit 04df31f

Browse files
grahamcclaude
andcommitted
fix(tracing): keep mysql.run span open across row fetch
Review finding #1: same defect as postgres.run — the mysql.run `#[tracing::instrument]` span closed when `run()` returned the result stream, so it did not cover row fetching. Create the span explicitly and attach it to the returned stream via `instrument_stream`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent edd3680 commit 04df31f

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

sqlx-mysql/src/connection/executor.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use futures_core::Stream;
2323
use futures_util::TryStreamExt;
2424
use sqlx_core::arguments::Arguments as _;
2525
use sqlx_core::column::{ColumnOrigin, TableColumn};
26+
use sqlx_core::instrument_stream::InstrumentStream;
2627
use sqlx_core::sql_str::SqlStr;
2728
use std::{pin::pin, sync::Arc};
2829

@@ -111,24 +112,24 @@ impl MySqlConnection {
111112
}
112113

113114
#[allow(clippy::needless_lifetimes)]
114-
#[tracing::instrument(
115-
target = "sqlx::query",
116-
name = "mysql.run",
117-
skip_all,
118-
fields(
119-
db.system = "mysql",
120-
db.operation.parameters = arguments.as_ref().map_or(0, |a| a.len()),
121-
db.mysql.prepared = arguments.is_some(),
122-
),
123-
level = "debug",
124-
)]
125115
pub(crate) async fn run<'e, 'c: 'e, 'q: 'e>(
126116
&'c mut self,
127117
sql: SqlStr,
128118
arguments: Option<MySqlArguments>,
129119
persistent: bool,
130120
) -> Result<impl Stream<Item = Result<Either<MySqlQueryResult, MySqlRow>, Error>> + 'e, Error>
131121
{
122+
// The span is attached to the returned stream (see `instrument_stream`)
123+
// rather than via `#[tracing::instrument]` so it stays open while rows
124+
// are fetched, not just while the query is set up.
125+
let span = tracing::debug_span!(
126+
target: "sqlx::query",
127+
"mysql.run",
128+
db.system = "mysql",
129+
db.operation.parameters = arguments.as_ref().map_or(0, |a| a.len()),
130+
db.mysql.prepared = arguments.is_some(),
131+
);
132+
132133
let mut logger = QueryLogger::new(sql, self.inner.log_settings.clone());
133134

134135
self.inner.stream.wait_until_ready().await?;
@@ -288,7 +289,8 @@ impl MySqlConnection {
288289
r#yield!(v);
289290
}
290291
}
291-
})
292+
}
293+
.instrument_stream(span))
292294
}
293295
}
294296

0 commit comments

Comments
 (0)