sea_orm_tracing/
config.rs

1//! Configuration for tracing behavior.
2
3use std::time::Duration;
4
5/// Configuration options for database tracing.
6///
7/// # Example
8///
9/// ```rust
10/// use sea_orm_tracing::TracingConfig;
11/// use std::time::Duration;
12///
13/// let config = TracingConfig::default()
14///     .with_statement_logging(true)
15///     .with_slow_query_threshold(Duration::from_millis(100));
16/// ```
17#[derive(Debug, Clone)]
18pub struct TracingConfig {
19    /// Whether to include the SQL statement in spans.
20    /// Default: `false` (for security - prevents accidental credential logging)
21    pub log_statements: bool,
22
23    /// Whether to include query parameters in spans.
24    /// Default: `false` (parameters may contain sensitive data)
25    pub log_parameters: bool,
26
27    /// Threshold for logging slow queries at WARN level.
28    /// Queries exceeding this duration will be logged with additional context.
29    /// Default: 500ms
30    pub slow_query_threshold: Duration,
31
32    /// Whether to record the number of rows affected/returned.
33    /// Default: `true`
34    pub record_row_counts: bool,
35
36    /// Target name for tracing events.
37    /// Default: "sea_orm_tracing"
38    pub target: &'static str,
39
40    /// Custom database name to include in spans (useful for multi-database setups).
41    /// Default: `None`
42    pub database_name: Option<String>,
43
44    /// Server address (hostname) for the database.
45    /// Used by X-Ray and other APM tools to show the database as a separate node on the service map.
46    /// Default: `None`
47    pub server_address: Option<String>,
48
49    /// Server port for the database.
50    /// Default: `None`
51    pub server_port: Option<u16>,
52
53    /// Peer service name for the database.
54    /// This is used by X-Ray to display the database as a named node on the trace map.
55    /// If not set, X-Ray will use server_address if available.
56    /// Default: `None`
57    pub peer_service: Option<String>,
58}
59
60impl Default for TracingConfig {
61    fn default() -> Self {
62        Self {
63            log_statements: false,
64            log_parameters: false,
65            slow_query_threshold: Duration::from_millis(500),
66            record_row_counts: true,
67            target: "sea_orm_tracing",
68            database_name: None,
69            server_address: None,
70            server_port: None,
71            peer_service: None,
72        }
73    }
74}
75
76impl TracingConfig {
77    /// Create a new configuration with default values.
78    pub fn new() -> Self {
79        Self::default()
80    }
81
82    /// Enable or disable SQL statement logging in spans.
83    ///
84    /// **Security Warning**: Enabling this may expose sensitive data in your traces
85    /// if your queries contain credentials or PII in the SQL text itself.
86    pub fn with_statement_logging(mut self, enabled: bool) -> Self {
87        self.log_statements = enabled;
88        self
89    }
90
91    /// Enable or disable parameter logging in spans.
92    ///
93    /// **Security Warning**: Query parameters often contain user input and
94    /// potentially sensitive data. Only enable in development or controlled environments.
95    pub fn with_parameter_logging(mut self, enabled: bool) -> Self {
96        self.log_parameters = enabled;
97        self
98    }
99
100    /// Set the threshold for slow query warnings.
101    ///
102    /// Queries taking longer than this duration will be logged at WARN level
103    /// with the `slow_query` field set to `true`.
104    pub fn with_slow_query_threshold(mut self, threshold: Duration) -> Self {
105        self.slow_query_threshold = threshold;
106        self
107    }
108
109    /// Enable or disable row count recording.
110    pub fn with_row_count_recording(mut self, enabled: bool) -> Self {
111        self.record_row_counts = enabled;
112        self
113    }
114
115    /// Set a custom tracing target name.
116    pub fn with_target(mut self, target: &'static str) -> Self {
117        self.target = target;
118        self
119    }
120
121    /// Set a database name to include in spans.
122    ///
123    /// Useful when your application connects to multiple databases.
124    pub fn with_database_name(mut self, name: impl Into<String>) -> Self {
125        self.database_name = Some(name.into());
126        self
127    }
128
129    /// Set the server address (hostname) for the database.
130    ///
131    /// This enables APM tools like AWS X-Ray to show the database as a separate
132    /// node on the service/trace map.
133    pub fn with_server_address(mut self, address: impl Into<String>) -> Self {
134        self.server_address = Some(address.into());
135        self
136    }
137
138    /// Set the server port for the database.
139    pub fn with_server_port(mut self, port: u16) -> Self {
140        self.server_port = Some(port);
141        self
142    }
143
144    /// Set the peer service name for the database.
145    ///
146    /// This is used by AWS X-Ray and other APM tools to display the database
147    /// as a named node on the trace map. Common values: "postgresql", "mysql",
148    /// or a custom name like "users-db".
149    pub fn with_peer_service(mut self, name: impl Into<String>) -> Self {
150        self.peer_service = Some(name.into());
151        self
152    }
153
154    /// Create a development-friendly configuration with full logging enabled.
155    ///
156    /// **Warning**: Do not use in production as it logs all SQL and parameters.
157    pub fn development() -> Self {
158        Self {
159            log_statements: true,
160            log_parameters: true,
161            slow_query_threshold: Duration::from_millis(100),
162            record_row_counts: true,
163            target: "sea_orm_tracing",
164            database_name: None,
165            server_address: None,
166            server_port: None,
167            peer_service: None,
168        }
169    }
170
171    /// Create a production-safe configuration with minimal overhead.
172    pub fn production() -> Self {
173        Self {
174            log_statements: false,
175            log_parameters: false,
176            slow_query_threshold: Duration::from_secs(1),
177            record_row_counts: true,
178            target: "sea_orm_tracing",
179            database_name: None,
180            server_address: None,
181            server_port: None,
182            peer_service: None,
183        }
184    }
185}