mysql_async/
tracing_utils.rs

1/// Compile-time tracing level.
2pub trait TracingLevel: Send + Sync + 'static {
3    #[cfg(feature = "tracing")]
4    const LEVEL: tracing::Level;
5}
6
7/// INFO tracing level.
8pub struct LevelInfo;
9
10impl TracingLevel for LevelInfo {
11    #[cfg(feature = "tracing")]
12    const LEVEL: tracing::Level = tracing::Level::INFO;
13}
14
15/// TRACE tracing level.
16pub struct LevelTrace;
17
18impl TracingLevel for LevelTrace {
19    #[cfg(feature = "tracing")]
20    const LEVEL: tracing::Level = tracing::Level::TRACE;
21}
22
23#[cfg(feature = "tracing")]
24macro_rules! create_span {
25    ($s:expr, $($field:tt)*) => {
26        if $s == tracing::Level::TRACE {
27            tracing::trace_span!($($field)*)
28        } else if $s == tracing::Level::DEBUG {
29            tracing::debug_span!($($field)*)
30        } else if $s == tracing::Level::INFO {
31            tracing::info_span!($($field)*)
32        } else if $s == tracing::Level::WARN {
33            tracing::warn_span!($($field)*)
34        } else if $s == tracing::Level::ERROR {
35            tracing::error_span!($($field)*)
36        } else {
37            unreachable!();
38        }
39    }
40}
41
42#[cfg(feature = "tracing")]
43macro_rules! instrument_result {
44    ($fut:expr, $span:expr) => {{
45        let fut = async {
46            $fut.await.or_else(|e| {
47                match &e {
48                    $crate::error::Error::Server(server_error) => {
49                        match server_error.code {
50                            // Duplicated entry for key
51                            1062 => {
52                                tracing::warn!(error = %e)
53                            }
54                            // Foreign key violation
55                            1451 => {
56                                tracing::warn!(error = %e)
57                            }
58                            // User defined exception condition
59                            1644 => {
60                                tracing::warn!(error = %e);
61                            }
62                            _ => tracing::error!(error = %e),
63                        }
64                    },
65                    e => {
66                        tracing::error!(error = %e);
67                    }
68                }
69                Err(e)
70            })
71        };
72        <_ as tracing::Instrument>::instrument(fut, $span)
73    }};
74}