Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cargo test --doc

### Launch some databases

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

```sh
docker run \
Expand Down
25 changes: 22 additions & 3 deletions sea-orm-sync/src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl ConnectionTrait for DatabaseConnection {
"sea_orm.execute",
self.get_database_backend(),
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.get_record_stmt_in_spans(),
{
match &self.inner {
#[cfg(feature = "sqlx-mysql")]
Expand Down Expand Up @@ -219,7 +219,7 @@ impl ConnectionTrait for DatabaseConnection {
"sea_orm.query_one",
self.get_database_backend(),
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.get_record_stmt_in_spans(),
{
match &self.inner {
#[cfg(feature = "sqlx-mysql")]
Expand Down Expand Up @@ -249,7 +249,7 @@ impl ConnectionTrait for DatabaseConnection {
"sea_orm.query_all",
self.get_database_backend(),
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.get_record_stmt_in_spans(),
{
match &self.inner {
#[cfg(feature = "sqlx-mysql")]
Expand Down Expand Up @@ -587,6 +587,25 @@ impl DatabaseConnection {
}

impl DatabaseConnection {
#[expect(unused)]
pub(crate) fn get_record_stmt_in_spans(&self) -> bool {
match &self.inner {
#[cfg(feature = "sqlx-mysql")]
DatabaseConnectionType::SqlxMySqlPoolConnection(conn) => conn.record_stmt_in_spans,
#[cfg(feature = "sqlx-postgres")]
DatabaseConnectionType::SqlxPostgresPoolConnection(conn) => conn.record_stmt_in_spans,
#[cfg(feature = "sqlx-sqlite")]
DatabaseConnectionType::SqlxSqlitePoolConnection(conn) => conn.record_stmt_in_spans,
#[cfg(feature = "rusqlite")]
DatabaseConnectionType::RusqliteSharedConnection(conn) => conn.record_stmt_in_spans,
DatabaseConnectionType::Disconnected => true,
#[cfg(feature = "mock")]
DatabaseConnectionType::MockDatabaseConnection(_) => true,
#[cfg(feature = "proxy")]
DatabaseConnectionType::ProxyDatabaseConnection(_) => true,
}
}

/// Get the database backend for this connection
///
/// # Panics
Expand Down
14 changes: 14 additions & 0 deletions sea-orm-sync/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub struct ConnectOptions {
pub(crate) max_lifetime: Option<Option<Duration>>,
/// Enable SQLx statement logging
pub(crate) sqlx_logging: bool,
/// Record SQL statements in tracing spans
pub(crate) record_stmt_in_spans: bool,
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
pub(crate) sqlx_logging_level: log::LevelFilter,
/// SQLx slow statements logging level (ignored if `sqlx_logging` is false)
Expand Down Expand Up @@ -231,6 +233,7 @@ impl ConnectOptions {
acquire_timeout: None,
max_lifetime: None,
sqlx_logging: true,
record_stmt_in_spans: true,
sqlx_logging_level: log::LevelFilter::Info,
sqlx_slow_statements_logging_level: log::LevelFilter::Off,
sqlx_slow_statements_logging_threshold: Duration::from_secs(1),
Expand Down Expand Up @@ -344,6 +347,17 @@ impl ConnectOptions {
self.sqlx_logging
}

/// Enable recording `db.statement` in tracing spans (default true).
pub fn record_stmt_in_spans(&mut self, value: bool) -> &mut Self {
self.record_stmt_in_spans = value;
self
}

/// Get whether `db.statement` recording in tracing spans is enabled.
pub fn get_record_stmt_in_spans(&self) -> bool {
self.record_stmt_in_spans
}

/// Set SQLx statement logging level (default INFO).
/// (ignored if `sqlx_logging` is `false`)
pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self {
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-sync/src/database/tracing_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ macro_rules! with_db_span {
if $record_stmt {
span.record("db.statement", $sql);
}
span.in_scope($fut)
span.in_scope(|| $fut)
}
#[cfg(not(feature = "tracing-spans"))]
{
Expand Down
12 changes: 9 additions & 3 deletions sea-orm-sync/src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct DatabaseTransaction {
backend: DbBackend,
open: bool,
metric_callback: Option<crate::metric::Callback>,
record_stmt_in_spans: bool,
}

impl std::fmt::Debug for DatabaseTransaction {
Expand All @@ -37,6 +38,7 @@ impl DatabaseTransaction {
conn: Arc<Mutex<InnerConnection>>,
backend: DbBackend,
metric_callback: Option<crate::metric::Callback>,
record_stmt_in_spans: bool,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
sqlite_transaction_mode: Option<SqliteTransactionMode>,
Expand All @@ -46,6 +48,7 @@ impl DatabaseTransaction {
backend,
open: true,
metric_callback,
record_stmt_in_spans,
};

let begin_result: Result<(), DbErr> = super::tracing_spans::with_db_span!(
Expand Down Expand Up @@ -321,7 +324,7 @@ impl ConnectionTrait for DatabaseTransaction {
"sea_orm.execute",
self.backend,
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.record_stmt_in_spans,
{
#[cfg(not(feature = "sync"))]
let conn = &mut *self.conn.lock();
Expand Down Expand Up @@ -437,7 +440,7 @@ impl ConnectionTrait for DatabaseTransaction {
"sea_orm.query_one",
self.backend,
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.record_stmt_in_spans,
{
#[cfg(not(feature = "sync"))]
let conn = &mut *self.conn.lock();
Expand Down Expand Up @@ -497,7 +500,7 @@ impl ConnectionTrait for DatabaseTransaction {
"sea_orm.query_all",
self.backend,
stmt.sql.as_str(),
record_stmt = true,
record_stmt = self.record_stmt_in_spans,
{
#[cfg(not(feature = "sync"))]
let conn = &mut *self.conn.lock();
Expand Down Expand Up @@ -584,6 +587,7 @@ impl TransactionTrait for DatabaseTransaction {
Arc::clone(&self.conn),
self.backend,
self.metric_callback.clone(),
self.record_stmt_in_spans,
None,
None,
None,
Expand All @@ -600,6 +604,7 @@ impl TransactionTrait for DatabaseTransaction {
Arc::clone(&self.conn),
self.backend,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
None,
Expand All @@ -615,6 +620,7 @@ impl TransactionTrait for DatabaseTransaction {
Arc::clone(&self.conn),
self.backend,
self.metric_callback.clone(),
self.record_stmt_in_spans,
options.isolation_level,
options.access_mode,
options.sqlite_transaction_mode,
Expand Down
1 change: 1 addition & 0 deletions sea-orm-sync/src/driver/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl crate::DatabaseTransaction {
Arc::new(Mutex::new(crate::InnerConnection::Mock(inner))),
backend,
metric_callback,
true,
None,
None,
None,
Expand Down
1 change: 1 addition & 0 deletions sea-orm-sync/src/driver/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl crate::DatabaseTransaction {
Arc::new(Mutex::new(crate::InnerConnection::Proxy(inner))),
backend,
metric_callback,
true,
None,
None,
None,
Expand Down
5 changes: 5 additions & 0 deletions sea-orm-sync/src/driver/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct RusqliteSharedConnection {
pub(crate) conn: Arc<Mutex<State>>,
acquire_timeout: Duration,
metric_callback: Option<crate::metric::Callback>,
pub(crate) record_stmt_in_spans: bool,
}

/// A loaned connection that supports nested transactions.
Expand Down Expand Up @@ -175,6 +176,7 @@ impl From<RusqliteConnection> for RusqliteSharedConnection {
conn: Arc::new(Mutex::new(State::Idle(conn))),
acquire_timeout: DEFAULT_ACQUIRE_TIMEOUT,
metric_callback: None,
record_stmt_in_spans: true,
}
}
}
Expand All @@ -196,6 +198,7 @@ impl RusqliteConnector {
#[instrument(level = "trace")]
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let acquire_timeout = options.acquire_timeout.unwrap_or(DEFAULT_ACQUIRE_TIMEOUT);
let record_stmt_in_spans = options.get_record_stmt_in_spans();
// TODO handle disable_statement_logging
let after_conn = options.after_connect;

Expand Down Expand Up @@ -244,6 +247,7 @@ impl RusqliteConnector {
conn: Arc::new(Mutex::new(State::Idle(conn))),
acquire_timeout,
metric_callback: None,
record_stmt_in_spans,
};

#[cfg(feature = "sqlite-use-returning-for-3_35")]
Expand Down Expand Up @@ -420,6 +424,7 @@ impl RusqliteSharedConnection {
Arc::new(Mutex::new(InnerConnection::Rusqlite(conn))),
crate::DbBackend::Sqlite,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
sqlite_transaction_mode,
Expand Down
9 changes: 9 additions & 0 deletions sea-orm-sync/src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct SqlxMySqlConnector;
pub struct SqlxMySqlPoolConnection {
pub(crate) pool: MySqlPool,
metric_callback: Option<crate::metric::Callback>,
pub(crate) record_stmt_in_spans: bool,
}

impl std::fmt::Debug for SqlxMySqlPoolConnection {
Expand All @@ -42,6 +43,7 @@ impl From<MySqlPool> for SqlxMySqlPoolConnection {
SqlxMySqlPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans: true,
}
}
}
Expand All @@ -61,6 +63,7 @@ impl SqlxMySqlConnector {
/// Add configuration options for the MySQL database
#[instrument(level = "trace")]
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let record_stmt_in_spans = options.get_record_stmt_in_spans();
let mut sqlx_opts = options
.url
.parse::<MySqlConnectOptions>()
Expand Down Expand Up @@ -100,6 +103,7 @@ impl SqlxMySqlConnector {
DatabaseConnectionType::SqlxMySqlPoolConnection(SqlxMySqlPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans,
})
.into();

Expand All @@ -117,6 +121,7 @@ impl SqlxMySqlConnector {
DatabaseConnectionType::SqlxMySqlPoolConnection(SqlxMySqlPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans: true,
})
.into()
}
Expand Down Expand Up @@ -207,6 +212,7 @@ impl SqlxMySqlPoolConnection {
DatabaseTransaction::new_mysql(
conn,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
)
Expand All @@ -228,6 +234,7 @@ impl SqlxMySqlPoolConnection {
let transaction = DatabaseTransaction::new_mysql(
conn,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
)
Expand Down Expand Up @@ -337,13 +344,15 @@ impl crate::DatabaseTransaction {
pub(crate) fn new_mysql(
inner: PoolConnection<sqlx::MySql>,
metric_callback: Option<crate::metric::Callback>,
record_stmt_in_spans: bool,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
) -> Result<crate::DatabaseTransaction, DbErr> {
Self::begin(
Arc::new(Mutex::new(crate::InnerConnection::MySql(inner))),
crate::DbBackend::MySql,
metric_callback,
record_stmt_in_spans,
isolation_level,
access_mode,
None,
Expand Down
9 changes: 9 additions & 0 deletions sea-orm-sync/src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct SqlxPostgresConnector;
pub struct SqlxPostgresPoolConnection {
pub(crate) pool: PgPool,
metric_callback: Option<crate::metric::Callback>,
pub(crate) record_stmt_in_spans: bool,
}

impl std::fmt::Debug for SqlxPostgresPoolConnection {
Expand All @@ -41,6 +42,7 @@ impl From<PgPool> for SqlxPostgresPoolConnection {
SqlxPostgresPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans: true,
}
}
}
Expand All @@ -60,6 +62,7 @@ impl SqlxPostgresConnector {
/// Add configuration options for the PostgreSQL database
#[instrument(level = "trace")]
pub fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let record_stmt_in_spans = options.get_record_stmt_in_spans();
let mut sqlx_opts = options
.url
.parse::<PgConnectOptions>()
Expand Down Expand Up @@ -134,6 +137,7 @@ impl SqlxPostgresConnector {
DatabaseConnectionType::SqlxPostgresPoolConnection(SqlxPostgresPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans,
})
.into();

Expand All @@ -151,6 +155,7 @@ impl SqlxPostgresConnector {
DatabaseConnectionType::SqlxPostgresPoolConnection(SqlxPostgresPoolConnection {
pool,
metric_callback: None,
record_stmt_in_spans: true,
})
.into()
}
Expand Down Expand Up @@ -241,6 +246,7 @@ impl SqlxPostgresPoolConnection {
DatabaseTransaction::new_postgres(
conn,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
)
Expand All @@ -262,6 +268,7 @@ impl SqlxPostgresPoolConnection {
let transaction = DatabaseTransaction::new_postgres(
conn,
self.metric_callback.clone(),
self.record_stmt_in_spans,
isolation_level,
access_mode,
)
Expand Down Expand Up @@ -372,13 +379,15 @@ impl crate::DatabaseTransaction {
pub(crate) fn new_postgres(
inner: PoolConnection<sqlx::Postgres>,
metric_callback: Option<crate::metric::Callback>,
record_stmt_in_spans: bool,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
) -> Result<crate::DatabaseTransaction, DbErr> {
Self::begin(
Arc::new(Mutex::new(crate::InnerConnection::Postgres(inner))),
crate::DbBackend::Postgres,
metric_callback,
record_stmt_in_spans,
isolation_level,
access_mode,
None,
Expand Down
Loading
Loading