1use alloc::boxed::Box;
2use core::fmt::{Debug, Display};
3use core::num::NonZeroU32;
4use core::ops::DerefMut;
5use downcast_rs::Downcast;
6
7#[cfg(feature = "std")]
8static GLOBAL_INSTRUMENTATION: std::sync::RwLock<fn() -> Option<Box<dyn Instrumentation>>> =
9 std::sync::RwLock::new(|| None);
10
11pub trait DebugQuery: Debug + Display {}
16
17impl<T, DB> DebugQuery for crate::query_builder::DebugQuery<'_, T, DB> where Self: Debug + Display {}
18
19#[diesel_derives::__diesel_public_if(
26 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
27)]
28pub(crate) struct StrQueryHelper<'query> {
29 s: &'query str,
30}
31
32impl<'query> StrQueryHelper<'query> {
33 #[diesel_derives::__diesel_public_if(
35 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
36 )]
37 #[cfg(any(
38 feature = "postgres",
39 feature = "__sqlite-shared",
40 feature = "mysql",
41 feature = "mariadb",
42 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
43 ))]
44 pub(crate) fn new(s: &'query str) -> Self {
45 Self { s }
46 }
47}
48
49impl Debug for StrQueryHelper<'_> {
50 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51 Debug::fmt(self.s, f)
52 }
53}
54
55impl Display for StrQueryHelper<'_> {
56 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57 Display::fmt(&self.s, f)
58 }
59}
60
61impl DebugQuery for StrQueryHelper<'_> {}
62
63#[derive(Debug)]
86#[non_exhaustive]
87pub enum InstrumentationEvent<'a> {
88 #[non_exhaustive]
91 StartEstablishConnection {
92 url: &'a str,
98 },
99 #[non_exhaustive]
102 FinishEstablishConnection {
103 url: &'a str,
109 error: Option<&'a crate::result::ConnectionError>,
111 },
112 #[non_exhaustive]
115 StartQuery {
116 query: &'a dyn DebugQuery,
125 },
126 #[non_exhaustive]
130 CacheQuery {
131 sql: &'a str,
133 },
134 #[non_exhaustive]
137 FinishQuery {
138 query: &'a dyn DebugQuery,
147 error: Option<&'a crate::result::Error>,
149 },
150 #[non_exhaustive]
153 BeginTransaction {
154 depth: NonZeroU32,
157 },
158 #[non_exhaustive]
161 CommitTransaction {
162 depth: NonZeroU32,
165 },
166 #[non_exhaustive]
169 RollbackTransaction {
170 depth: NonZeroU32,
173 },
174}
175
176#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
180impl<'a> InstrumentationEvent<'a> {
181 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
183 pub fn start_establish_connection(url: &'a str) -> Self {
184 Self::StartEstablishConnection { url }
185 }
186
187 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
189 pub fn finish_establish_connection(
190 url: &'a str,
191 error: Option<&'a crate::result::ConnectionError>,
192 ) -> Self {
193 Self::FinishEstablishConnection { url, error }
194 }
195
196 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
198 pub fn start_query(query: &'a dyn DebugQuery) -> Self {
199 Self::StartQuery { query }
200 }
201
202 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
204 pub fn cache_query(sql: &'a str) -> Self {
205 Self::CacheQuery { sql }
206 }
207
208 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
210 pub fn finish_query(
211 query: &'a dyn DebugQuery,
212 error: Option<&'a crate::result::Error>,
213 ) -> Self {
214 Self::FinishQuery { query, error }
215 }
216
217 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
219 pub fn begin_transaction(depth: NonZeroU32) -> Self {
220 Self::BeginTransaction { depth }
221 }
222
223 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
225 pub fn rollback_transaction(depth: NonZeroU32) -> Self {
226 Self::RollbackTransaction { depth }
227 }
228
229 #[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
231 pub fn commit_transaction(depth: NonZeroU32) -> Self {
232 Self::CommitTransaction { depth }
233 }
234}
235
236pub trait Instrumentation: Downcast + Send + 'static {
250 fn on_connection_event(&mut self, event: InstrumentationEvent<'_>);
252}
253downcast_rs::impl_downcast!(Instrumentation);
254
255pub fn get_default_instrumentation() -> Option<Box<dyn Instrumentation>> {
260 #[cfg(feature = "std")]
261 match GLOBAL_INSTRUMENTATION.read() {
262 Ok(f) => (*f)(),
263 Err(_) => None,
264 }
265 #[cfg(not(feature = "std"))]
266 None
267}
268
269#[cfg(feature = "std")]
287pub fn set_default_instrumentation(
288 default: fn() -> Option<Box<dyn Instrumentation>>,
289) -> crate::QueryResult<()> {
290 match GLOBAL_INSTRUMENTATION.write() {
291 Ok(mut l) => {
292 *l = default;
293 Ok(())
294 }
295 Err(e) => Err(crate::result::Error::DatabaseError(
296 crate::result::DatabaseErrorKind::Unknown,
297 Box::new(e.to_string()),
298 )),
299 }
300}
301
302impl<F> Instrumentation for F
303where
304 F: FnMut(InstrumentationEvent<'_>) + Send + 'static,
305{
306 fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
307 (self)(event)
308 }
309}
310
311impl Instrumentation for Box<dyn Instrumentation> {
312 fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
313 self.deref_mut().on_connection_event(event)
314 }
315}
316
317impl<T> Instrumentation for Option<T>
318where
319 T: Instrumentation,
320{
321 fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
322 if let Some(i) = self {
323 i.on_connection_event(event)
324 }
325 }
326}
327
328#[diesel_derives::__diesel_public_if(
329 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
330)]
331#[cfg(any(
332 feature = "postgres",
333 feature = "__sqlite-shared",
334 feature = "mysql",
335 feature = "mariadb",
336 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
337))]
338pub(crate) struct DynInstrumentation {
347 no_instrumentation: NoInstrumentation,
349 inner: Option<Box<dyn Instrumentation>>,
350}
351
352#[cfg(any(
353 feature = "postgres",
354 feature = "__sqlite-shared",
355 feature = "mysql",
356 feature = "mariadb",
357 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
358))]
359impl core::ops::Deref for DynInstrumentation {
360 type Target = dyn Instrumentation;
361
362 fn deref(&self) -> &Self::Target {
363 self.inner.as_deref().unwrap_or(&self.no_instrumentation)
364 }
365}
366
367#[cfg(any(
368 feature = "postgres",
369 feature = "__sqlite-shared",
370 feature = "mysql",
371 feature = "mariadb",
372 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
373))]
374impl DerefMut for DynInstrumentation {
375 fn deref_mut(&mut self) -> &mut Self::Target {
376 self.inner
377 .as_deref_mut()
378 .unwrap_or(&mut self.no_instrumentation)
379 }
380}
381
382#[cfg(any(
383 feature = "postgres",
384 feature = "__sqlite-shared",
385 feature = "mysql",
386 feature = "mariadb",
387 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
388))]
389impl DynInstrumentation {
390 #[diesel_derives::__diesel_public_if(
392 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
393 )]
394 #[cfg(any(
395 feature = "postgres",
396 feature = "__sqlite-shared",
397 feature = "mysql",
398 feature = "mariadb",
399 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
400 ))]
401 pub(crate) fn default_instrumentation() -> Self {
402 Self {
403 inner: get_default_instrumentation(),
404 no_instrumentation: NoInstrumentation,
405 }
406 }
407
408 #[diesel_derives::__diesel_public_if(
410 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
411 )]
412 #[cfg(any(
413 feature = "postgres",
414 feature = "__sqlite-shared",
415 feature = "mysql",
416 feature = "mariadb",
417 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
418 ))]
419 pub(crate) fn none() -> Self {
420 Self {
421 inner: None,
422 no_instrumentation: NoInstrumentation,
423 }
424 }
425
426 #[diesel_derives::__diesel_public_if(
428 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
429 )]
430 #[cfg(any(
431 feature = "postgres",
432 feature = "__sqlite-shared",
433 feature = "mysql",
434 feature = "mariadb",
435 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
436 ))]
437 pub(crate) fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
438 if let Some(inner) = self.inner.as_deref_mut() {
442 inner.on_connection_event(event)
443 }
444 }
445}
446
447#[cfg(any(
448 feature = "postgres",
449 feature = "__sqlite-shared",
450 feature = "mysql",
451 feature = "mariadb",
452 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
453))]
454impl<I: Instrumentation> From<I> for DynInstrumentation {
455 fn from(instrumentation: I) -> Self {
456 Self {
457 inner: Some(unpack_instrumentation(Box::new(instrumentation))),
458 no_instrumentation: NoInstrumentation,
459 }
460 }
461}
462#[cfg(any(
463 feature = "postgres",
464 feature = "__sqlite-shared",
465 feature = "mysql",
466 feature = "mariadb",
467 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
468))]
469struct NoInstrumentation;
470
471#[cfg(any(
472 feature = "postgres",
473 feature = "__sqlite-shared",
474 feature = "mysql",
475 feature = "mariadb",
476 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
477))]
478impl Instrumentation for NoInstrumentation {
479 fn on_connection_event(&mut self, _: InstrumentationEvent<'_>) {}
480}
481
482#[cfg(any(
484 feature = "postgres",
485 feature = "__sqlite-shared",
486 feature = "mysql",
487 feature = "mariadb",
488 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
489))]
490fn unpack_instrumentation(
491 mut instrumentation: Box<dyn Instrumentation>,
492) -> Box<dyn Instrumentation> {
493 loop {
494 match instrumentation.downcast::<Box<dyn Instrumentation>>() {
495 Ok(extra_boxed_instrumentation) => instrumentation = *extra_boxed_instrumentation,
496 Err(not_extra_boxed_instrumentation) => {
497 break not_extra_boxed_instrumentation;
498 }
499 }
500 }
501}