From 9f2a6dc40b06edabc1f05d9244e218ee3eaa410a Mon Sep 17 00:00:00 2001 From: Marco Napetti <7566389+nappa85@users.noreply.github.com> Date: Thu, 27 Oct 2022 19:00:04 +0200 Subject: [PATCH] Fix transaction metrics (#1165) --- src/database/transaction.rs | 60 ++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/database/transaction.rs b/src/database/transaction.rs index 66ae25e58..953eba220 100644 --- a/src/database/transaction.rs +++ b/src/database/transaction.rs @@ -310,23 +310,29 @@ impl ConnectionTrait for DatabaseTransaction { #[cfg(feature = "sqlx-mysql")] InnerConnection::MySql(conn) => { let query = crate::driver::sqlx_mysql::sqlx_query(&stmt); - Self::map_err_ignore_not_found( - query.fetch_one(conn).await.map(|row| Some(row.into())), - ) + crate::metric::metric!(self.metric_callback, &stmt, { + Self::map_err_ignore_not_found( + query.fetch_one(conn).await.map(|row| Some(row.into())), + ) + }) } #[cfg(feature = "sqlx-postgres")] InnerConnection::Postgres(conn) => { let query = crate::driver::sqlx_postgres::sqlx_query(&stmt); - Self::map_err_ignore_not_found( - query.fetch_one(conn).await.map(|row| Some(row.into())), - ) + crate::metric::metric!(self.metric_callback, &stmt, { + Self::map_err_ignore_not_found( + query.fetch_one(conn).await.map(|row| Some(row.into())), + ) + }) } #[cfg(feature = "sqlx-sqlite")] InnerConnection::Sqlite(conn) => { let query = crate::driver::sqlx_sqlite::sqlx_query(&stmt); - Self::map_err_ignore_not_found( - query.fetch_one(conn).await.map(|row| Some(row.into())), - ) + crate::metric::metric!(self.metric_callback, &stmt, { + Self::map_err_ignore_not_found( + query.fetch_one(conn).await.map(|row| Some(row.into())), + ) + }) } #[cfg(feature = "mock")] InnerConnection::Mock(conn) => return conn.query_one(stmt), @@ -344,29 +350,35 @@ impl ConnectionTrait for DatabaseTransaction { #[cfg(feature = "sqlx-mysql")] InnerConnection::MySql(conn) => { let query = crate::driver::sqlx_mysql::sqlx_query(&stmt); - query - .fetch_all(conn) - .await - .map(|rows| rows.into_iter().map(|r| r.into()).collect()) - .map_err(sqlx_error_to_query_err) + crate::metric::metric!(self.metric_callback, &stmt, { + query + .fetch_all(conn) + .await + .map(|rows| rows.into_iter().map(|r| r.into()).collect()) + .map_err(sqlx_error_to_query_err) + }) } #[cfg(feature = "sqlx-postgres")] InnerConnection::Postgres(conn) => { let query = crate::driver::sqlx_postgres::sqlx_query(&stmt); - query - .fetch_all(conn) - .await - .map(|rows| rows.into_iter().map(|r| r.into()).collect()) - .map_err(sqlx_error_to_query_err) + crate::metric::metric!(self.metric_callback, &stmt, { + query + .fetch_all(conn) + .await + .map(|rows| rows.into_iter().map(|r| r.into()).collect()) + .map_err(sqlx_error_to_query_err) + }) } #[cfg(feature = "sqlx-sqlite")] InnerConnection::Sqlite(conn) => { let query = crate::driver::sqlx_sqlite::sqlx_query(&stmt); - query - .fetch_all(conn) - .await - .map(|rows| rows.into_iter().map(|r| r.into()).collect()) - .map_err(sqlx_error_to_query_err) + crate::metric::metric!(self.metric_callback, &stmt, { + query + .fetch_all(conn) + .await + .map(|rows| rows.into_iter().map(|r| r.into()).collect()) + .map_err(sqlx_error_to_query_err) + }) } #[cfg(feature = "mock")] InnerConnection::Mock(conn) => return conn.query_all(stmt),