Skip to content

Commit f5c212b

Browse files
Add option to disable SQL statement recording in tracing spans (#3045)
* Add option to remove params from traces * make sync and fix typos * Rename `tracing_statement_logging` to `record_stmt_in_spans`
1 parent 79e981e commit f5c212b

21 files changed

Lines changed: 161 additions & 15 deletions

DEVELOPMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cargo test --doc
4040

4141
### Launch some databases
4242

43-
There is a docker compose under `build-tools`, but usually I just pick the ones I need from `build-tools/docker-crete.sh`:
43+
There is a docker compose under `build-tools`, but usually I just pick the ones I need from `build-tools/docker-create.sh`:
4444

4545
```sh
4646
docker run \

sea-orm-sync/src/database/db_connection.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl ConnectionTrait for DatabaseConnection {
147147
"sea_orm.execute",
148148
self.get_database_backend(),
149149
stmt.sql.as_str(),
150-
record_stmt = true,
150+
record_stmt = self.get_record_stmt_in_spans(),
151151
{
152152
match &self.inner {
153153
#[cfg(feature = "sqlx-mysql")]
@@ -219,7 +219,7 @@ impl ConnectionTrait for DatabaseConnection {
219219
"sea_orm.query_one",
220220
self.get_database_backend(),
221221
stmt.sql.as_str(),
222-
record_stmt = true,
222+
record_stmt = self.get_record_stmt_in_spans(),
223223
{
224224
match &self.inner {
225225
#[cfg(feature = "sqlx-mysql")]
@@ -249,7 +249,7 @@ impl ConnectionTrait for DatabaseConnection {
249249
"sea_orm.query_all",
250250
self.get_database_backend(),
251251
stmt.sql.as_str(),
252-
record_stmt = true,
252+
record_stmt = self.get_record_stmt_in_spans(),
253253
{
254254
match &self.inner {
255255
#[cfg(feature = "sqlx-mysql")]
@@ -587,6 +587,25 @@ impl DatabaseConnection {
587587
}
588588

589589
impl DatabaseConnection {
590+
#[expect(unused)]
591+
pub(crate) fn get_record_stmt_in_spans(&self) -> bool {
592+
match &self.inner {
593+
#[cfg(feature = "sqlx-mysql")]
594+
DatabaseConnectionType::SqlxMySqlPoolConnection(conn) => conn.record_stmt_in_spans,
595+
#[cfg(feature = "sqlx-postgres")]
596+
DatabaseConnectionType::SqlxPostgresPoolConnection(conn) => conn.record_stmt_in_spans,
597+
#[cfg(feature = "sqlx-sqlite")]
598+
DatabaseConnectionType::SqlxSqlitePoolConnection(conn) => conn.record_stmt_in_spans,
599+
#[cfg(feature = "rusqlite")]
600+
DatabaseConnectionType::RusqliteSharedConnection(conn) => conn.record_stmt_in_spans,
601+
DatabaseConnectionType::Disconnected => true,
602+
#[cfg(feature = "mock")]
603+
DatabaseConnectionType::MockDatabaseConnection(_) => true,
604+
#[cfg(feature = "proxy")]
605+
DatabaseConnectionType::ProxyDatabaseConnection(_) => true,
606+
}
607+
}
608+
590609
/// Get the database backend for this connection
591610
///
592611
/// # Panics

sea-orm-sync/src/database/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ pub struct ConnectOptions {
9090
pub(crate) max_lifetime: Option<Option<Duration>>,
9191
/// Enable SQLx statement logging
9292
pub(crate) sqlx_logging: bool,
93+
/// Record SQL statements in tracing spans
94+
pub(crate) record_stmt_in_spans: bool,
9395
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
9496
pub(crate) sqlx_logging_level: log::LevelFilter,
9597
/// SQLx slow statements logging level (ignored if `sqlx_logging` is false)
@@ -231,6 +233,7 @@ impl ConnectOptions {
231233
acquire_timeout: None,
232234
max_lifetime: None,
233235
sqlx_logging: true,
236+
record_stmt_in_spans: true,
234237
sqlx_logging_level: log::LevelFilter::Info,
235238
sqlx_slow_statements_logging_level: log::LevelFilter::Off,
236239
sqlx_slow_statements_logging_threshold: Duration::from_secs(1),
@@ -344,6 +347,17 @@ impl ConnectOptions {
344347
self.sqlx_logging
345348
}
346349

350+
/// Enable recording `db.statement` in tracing spans (default true).
351+
pub fn record_stmt_in_spans(&mut self, value: bool) -> &mut Self {
352+
self.record_stmt_in_spans = value;
353+
self
354+
}
355+
356+
/// Get whether `db.statement` recording in tracing spans is enabled.
357+
pub fn get_record_stmt_in_spans(&self) -> bool {
358+
self.record_stmt_in_spans
359+
}
360+
347361
/// Set SQLx statement logging level (default INFO).
348362
/// (ignored if `sqlx_logging` is `false`)
349363
pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self {

sea-orm-sync/src/database/tracing_spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ macro_rules! with_db_span {
161161
if $record_stmt {
162162
span.record("db.statement", $sql);
163163
}
164-
span.in_scope($fut)
164+
span.in_scope(|| $fut)
165165
}
166166
#[cfg(not(feature = "tracing-spans"))]
167167
{

sea-orm-sync/src/database/transaction.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct DatabaseTransaction {
2323
backend: DbBackend,
2424
open: bool,
2525
metric_callback: Option<crate::metric::Callback>,
26+
record_stmt_in_spans: bool,
2627
}
2728

2829
impl std::fmt::Debug for DatabaseTransaction {
@@ -37,6 +38,7 @@ impl DatabaseTransaction {
3738
conn: Arc<Mutex<InnerConnection>>,
3839
backend: DbBackend,
3940
metric_callback: Option<crate::metric::Callback>,
41+
record_stmt_in_spans: bool,
4042
isolation_level: Option<IsolationLevel>,
4143
access_mode: Option<AccessMode>,
4244
sqlite_transaction_mode: Option<SqliteTransactionMode>,
@@ -46,6 +48,7 @@ impl DatabaseTransaction {
4648
backend,
4749
open: true,
4850
metric_callback,
51+
record_stmt_in_spans,
4952
};
5053

5154
let begin_result: Result<(), DbErr> = super::tracing_spans::with_db_span!(
@@ -321,7 +324,7 @@ impl ConnectionTrait for DatabaseTransaction {
321324
"sea_orm.execute",
322325
self.backend,
323326
stmt.sql.as_str(),
324-
record_stmt = true,
327+
record_stmt = self.record_stmt_in_spans,
325328
{
326329
#[cfg(not(feature = "sync"))]
327330
let conn = &mut *self.conn.lock();
@@ -437,7 +440,7 @@ impl ConnectionTrait for DatabaseTransaction {
437440
"sea_orm.query_one",
438441
self.backend,
439442
stmt.sql.as_str(),
440-
record_stmt = true,
443+
record_stmt = self.record_stmt_in_spans,
441444
{
442445
#[cfg(not(feature = "sync"))]
443446
let conn = &mut *self.conn.lock();
@@ -497,7 +500,7 @@ impl ConnectionTrait for DatabaseTransaction {
497500
"sea_orm.query_all",
498501
self.backend,
499502
stmt.sql.as_str(),
500-
record_stmt = true,
503+
record_stmt = self.record_stmt_in_spans,
501504
{
502505
#[cfg(not(feature = "sync"))]
503506
let conn = &mut *self.conn.lock();
@@ -584,6 +587,7 @@ impl TransactionTrait for DatabaseTransaction {
584587
Arc::clone(&self.conn),
585588
self.backend,
586589
self.metric_callback.clone(),
590+
self.record_stmt_in_spans,
587591
None,
588592
None,
589593
None,
@@ -600,6 +604,7 @@ impl TransactionTrait for DatabaseTransaction {
600604
Arc::clone(&self.conn),
601605
self.backend,
602606
self.metric_callback.clone(),
607+
self.record_stmt_in_spans,
603608
isolation_level,
604609
access_mode,
605610
None,
@@ -615,6 +620,7 @@ impl TransactionTrait for DatabaseTransaction {
615620
Arc::clone(&self.conn),
616621
self.backend,
617622
self.metric_callback.clone(),
623+
self.record_stmt_in_spans,
618624
options.isolation_level,
619625
options.access_mode,
620626
options.sqlite_transaction_mode,

sea-orm-sync/src/driver/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl crate::DatabaseTransaction {
270270
Arc::new(Mutex::new(crate::InnerConnection::Mock(inner))),
271271
backend,
272272
metric_callback,
273+
true,
273274
None,
274275
None,
275276
None,

sea-orm-sync/src/driver/proxy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl crate::DatabaseTransaction {
149149
Arc::new(Mutex::new(crate::InnerConnection::Proxy(inner))),
150150
backend,
151151
metric_callback,
152+
true,
152153
None,
153154
None,
154155
None,

sea-orm-sync/src/driver/rusqlite.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct RusqliteSharedConnection {
4343
pub(crate) conn: Arc<Mutex<State>>,
4444
acquire_timeout: Duration,
4545
metric_callback: Option<crate::metric::Callback>,
46+
pub(crate) record_stmt_in_spans: bool,
4647
}
4748

4849
/// A loaned connection that supports nested transactions.
@@ -175,6 +176,7 @@ impl From<RusqliteConnection> for RusqliteSharedConnection {
175176
conn: Arc::new(Mutex::new(State::Idle(conn))),
176177
acquire_timeout: DEFAULT_ACQUIRE_TIMEOUT,
177178
metric_callback: None,
179+
record_stmt_in_spans: true,
178180
}
179181
}
180182
}
@@ -196,6 +198,7 @@ impl RusqliteConnector {
196198
#[instrument(level = "trace")]
197199
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
198200
let acquire_timeout = options.acquire_timeout.unwrap_or(DEFAULT_ACQUIRE_TIMEOUT);
201+
let record_stmt_in_spans = options.get_record_stmt_in_spans();
199202
// TODO handle disable_statement_logging
200203
let after_conn = options.after_connect;
201204

@@ -244,6 +247,7 @@ impl RusqliteConnector {
244247
conn: Arc::new(Mutex::new(State::Idle(conn))),
245248
acquire_timeout,
246249
metric_callback: None,
250+
record_stmt_in_spans,
247251
};
248252

249253
#[cfg(feature = "sqlite-use-returning-for-3_35")]
@@ -420,6 +424,7 @@ impl RusqliteSharedConnection {
420424
Arc::new(Mutex::new(InnerConnection::Rusqlite(conn))),
421425
crate::DbBackend::Sqlite,
422426
self.metric_callback.clone(),
427+
self.record_stmt_in_spans,
423428
isolation_level,
424429
access_mode,
425430
sqlite_transaction_mode,

sea-orm-sync/src/driver/sqlx_mysql.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct SqlxMySqlConnector;
2929
pub struct SqlxMySqlPoolConnection {
3030
pub(crate) pool: MySqlPool,
3131
metric_callback: Option<crate::metric::Callback>,
32+
pub(crate) record_stmt_in_spans: bool,
3233
}
3334

3435
impl std::fmt::Debug for SqlxMySqlPoolConnection {
@@ -42,6 +43,7 @@ impl From<MySqlPool> for SqlxMySqlPoolConnection {
4243
SqlxMySqlPoolConnection {
4344
pool,
4445
metric_callback: None,
46+
record_stmt_in_spans: true,
4547
}
4648
}
4749
}
@@ -61,6 +63,7 @@ impl SqlxMySqlConnector {
6163
/// Add configuration options for the MySQL database
6264
#[instrument(level = "trace")]
6365
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
66+
let record_stmt_in_spans = options.get_record_stmt_in_spans();
6467
let mut sqlx_opts = options
6568
.url
6669
.parse::<MySqlConnectOptions>()
@@ -100,6 +103,7 @@ impl SqlxMySqlConnector {
100103
DatabaseConnectionType::SqlxMySqlPoolConnection(SqlxMySqlPoolConnection {
101104
pool,
102105
metric_callback: None,
106+
record_stmt_in_spans,
103107
})
104108
.into();
105109

@@ -117,6 +121,7 @@ impl SqlxMySqlConnector {
117121
DatabaseConnectionType::SqlxMySqlPoolConnection(SqlxMySqlPoolConnection {
118122
pool,
119123
metric_callback: None,
124+
record_stmt_in_spans: true,
120125
})
121126
.into()
122127
}
@@ -207,6 +212,7 @@ impl SqlxMySqlPoolConnection {
207212
DatabaseTransaction::new_mysql(
208213
conn,
209214
self.metric_callback.clone(),
215+
self.record_stmt_in_spans,
210216
isolation_level,
211217
access_mode,
212218
)
@@ -228,6 +234,7 @@ impl SqlxMySqlPoolConnection {
228234
let transaction = DatabaseTransaction::new_mysql(
229235
conn,
230236
self.metric_callback.clone(),
237+
self.record_stmt_in_spans,
231238
isolation_level,
232239
access_mode,
233240
)
@@ -337,13 +344,15 @@ impl crate::DatabaseTransaction {
337344
pub(crate) fn new_mysql(
338345
inner: PoolConnection<sqlx::MySql>,
339346
metric_callback: Option<crate::metric::Callback>,
347+
record_stmt_in_spans: bool,
340348
isolation_level: Option<IsolationLevel>,
341349
access_mode: Option<AccessMode>,
342350
) -> Result<crate::DatabaseTransaction, DbErr> {
343351
Self::begin(
344352
Arc::new(Mutex::new(crate::InnerConnection::MySql(inner))),
345353
crate::DbBackend::MySql,
346354
metric_callback,
355+
record_stmt_in_spans,
347356
isolation_level,
348357
access_mode,
349358
None,

sea-orm-sync/src/driver/sqlx_postgres.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct SqlxPostgresConnector;
2828
pub struct SqlxPostgresPoolConnection {
2929
pub(crate) pool: PgPool,
3030
metric_callback: Option<crate::metric::Callback>,
31+
pub(crate) record_stmt_in_spans: bool,
3132
}
3233

3334
impl std::fmt::Debug for SqlxPostgresPoolConnection {
@@ -41,6 +42,7 @@ impl From<PgPool> for SqlxPostgresPoolConnection {
4142
SqlxPostgresPoolConnection {
4243
pool,
4344
metric_callback: None,
45+
record_stmt_in_spans: true,
4446
}
4547
}
4648
}
@@ -60,6 +62,7 @@ impl SqlxPostgresConnector {
6062
/// Add configuration options for the PostgreSQL database
6163
#[instrument(level = "trace")]
6264
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
65+
let record_stmt_in_spans = options.get_record_stmt_in_spans();
6366
let mut sqlx_opts = options
6467
.url
6568
.parse::<PgConnectOptions>()
@@ -134,6 +137,7 @@ impl SqlxPostgresConnector {
134137
DatabaseConnectionType::SqlxPostgresPoolConnection(SqlxPostgresPoolConnection {
135138
pool,
136139
metric_callback: None,
140+
record_stmt_in_spans,
137141
})
138142
.into();
139143

@@ -151,6 +155,7 @@ impl SqlxPostgresConnector {
151155
DatabaseConnectionType::SqlxPostgresPoolConnection(SqlxPostgresPoolConnection {
152156
pool,
153157
metric_callback: None,
158+
record_stmt_in_spans: true,
154159
})
155160
.into()
156161
}
@@ -241,6 +246,7 @@ impl SqlxPostgresPoolConnection {
241246
DatabaseTransaction::new_postgres(
242247
conn,
243248
self.metric_callback.clone(),
249+
self.record_stmt_in_spans,
244250
isolation_level,
245251
access_mode,
246252
)
@@ -262,6 +268,7 @@ impl SqlxPostgresPoolConnection {
262268
let transaction = DatabaseTransaction::new_postgres(
263269
conn,
264270
self.metric_callback.clone(),
271+
self.record_stmt_in_spans,
265272
isolation_level,
266273
access_mode,
267274
)
@@ -372,13 +379,15 @@ impl crate::DatabaseTransaction {
372379
pub(crate) fn new_postgres(
373380
inner: PoolConnection<sqlx::Postgres>,
374381
metric_callback: Option<crate::metric::Callback>,
382+
record_stmt_in_spans: bool,
375383
isolation_level: Option<IsolationLevel>,
376384
access_mode: Option<AccessMode>,
377385
) -> Result<crate::DatabaseTransaction, DbErr> {
378386
Self::begin(
379387
Arc::new(Mutex::new(crate::InnerConnection::Postgres(inner))),
380388
crate::DbBackend::Postgres,
381389
metric_callback,
390+
record_stmt_in_spans,
382391
isolation_level,
383392
access_mode,
384393
None,

0 commit comments

Comments
 (0)