Skip to content

Commit a06702c

Browse files
committed
Improve the mysql instrumentation code
1 parent 64c8d33 commit a06702c

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/mysql/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ pub struct AsyncMysqlConnection {
3535
#[async_trait::async_trait]
3636
impl SimpleAsyncConnection for AsyncMysqlConnection {
3737
async fn batch_execute(&mut self, query: &str) -> diesel::QueryResult<()> {
38-
self.instrumentation
39-
.lock()
40-
.unwrap_or_else(|p| p.into_inner())
38+
self.instrumentation()
4139
.on_connection_event(InstrumentationEvent::start_query(&StrQueryHelper::new(
4240
query,
4341
)));
@@ -47,9 +45,7 @@ impl SimpleAsyncConnection for AsyncMysqlConnection {
4745
.await
4846
.map_err(ErrorHelper)
4947
.map_err(Into::into);
50-
self.instrumentation
51-
.lock()
52-
.unwrap_or_else(|p| p.into_inner())
48+
self.instrumentation()
5349
.on_connection_event(InstrumentationEvent::finish_query(
5450
&StrQueryHelper::new(query),
5551
result.as_ref().err(),
@@ -232,7 +228,9 @@ impl AsyncMysqlConnection {
232228
conn,
233229
stmt_cache: StmtCache::new(),
234230
transaction_manager: AnsiTransactionManager::default(),
235-
instrumentation: std::sync::Mutex::new(None),
231+
instrumentation: std::sync::Mutex::new(
232+
diesel::connection::get_default_instrumentation(),
233+
),
236234
};
237235

238236
for stmt in CONNECTION_SETUP_QUERIES {
@@ -257,9 +255,7 @@ impl AsyncMysqlConnection {
257255
T: QueryFragment<Mysql> + QueryId,
258256
F: Future<Output = QueryResult<R>> + Send,
259257
{
260-
self.instrumentation
261-
.lock()
262-
.unwrap_or_else(|p| p.into_inner())
258+
self.instrumentation()
263259
.on_connection_event(InstrumentationEvent::start_query(&diesel::debug_query(
264260
&query,
265261
)));
@@ -272,7 +268,7 @@ impl AsyncMysqlConnection {
272268
ref mut conn,
273269
ref mut stmt_cache,
274270
ref mut transaction_manager,
275-
ref instrumentation,
271+
ref mut instrumentation,
276272
..
277273
} = self;
278274

@@ -311,7 +307,7 @@ impl AsyncMysqlConnection {
311307
};
312308
let r = update_transaction_manager_status(inner.await, transaction_manager);
313309
instrumentation
314-
.lock()
310+
.get_mut()
315311
.unwrap_or_else(|p| p.into_inner())
316312
.on_connection_event(InstrumentationEvent::finish_query(
317313
&StrQueryHelper::new(&sql),

0 commit comments

Comments
 (0)