ckb_sentry_core/
clientoptions.rs

1use std::borrow::Cow;
2use std::fmt;
3use std::sync::Arc;
4use std::time::Duration;
5
6use crate::constants::USER_AGENT;
7use crate::protocol::{Breadcrumb, Event};
8use crate::types::Dsn;
9use crate::{Integration, IntoDsn, TransportFactory};
10
11/// Type alias for before event/breadcrumb handlers.
12pub type BeforeCallback<T> = Arc<dyn Fn(T) -> Option<T> + Send + Sync>;
13
14/// Configuration settings for the client.
15///
16/// These options are explained in more detail in the general
17/// [sentry documentation](https://docs.sentry.io/error-reporting/configuration/?platform=rust).
18///
19/// # Examples
20///
21/// ```
22/// let _options = sentry::ClientOptions {
23///     debug: true,
24///     ..Default::default()
25/// };
26/// ```
27#[derive(Clone)]
28pub struct ClientOptions {
29    // Common options
30    /// The DSN to use.  If not set the client is effectively disabled.
31    pub dsn: Option<Dsn>,
32    /// Enables debug mode.
33    ///
34    /// In debug mode debug information is printed to stderr to help you understand what
35    /// sentry is doing.  When the `log` feature is enabled, Sentry will instead
36    /// log to the `sentry` logger independently of this flag with the `Debug` level.
37    pub debug: bool,
38    /// The release to be sent with events.
39    pub release: Option<Cow<'static, str>>,
40    /// The environment to be sent with events.
41    ///
42    /// Defaults to either `"development"` or `"production"` depending on the
43    /// `debug_assertions` cfg-attribute.
44    pub environment: Option<Cow<'static, str>>,
45    /// The sample rate for event submission. (0.0 - 1.0, defaults to 1.0)
46    pub sample_rate: f32,
47    /// Maximum number of breadcrumbs. (defaults to 100)
48    pub max_breadcrumbs: usize,
49    /// Attaches stacktraces to messages.
50    pub attach_stacktrace: bool,
51    /// If turned on some default PII informat is attached.
52    pub send_default_pii: bool,
53    /// The server name to be reported.
54    pub server_name: Option<Cow<'static, str>>,
55    /// Module prefixes that are always considered "in_app".
56    pub in_app_include: Vec<&'static str>,
57    /// Module prefixes that are never "in_app".
58    pub in_app_exclude: Vec<&'static str>,
59    // Integration options
60    /// A list of integrations to enable.
61    pub integrations: Vec<Arc<dyn Integration>>,
62    /// Whether to add default integrations.
63    pub default_integrations: bool,
64    // Hooks
65    /// Callback that is executed before event sending.
66    pub before_send: Option<BeforeCallback<Event<'static>>>,
67    /// Callback that is executed for each Breadcrumb being added.
68    pub before_breadcrumb: Option<BeforeCallback<Breadcrumb>>,
69    // Transport options
70    /// The transport to use.
71    ///
72    /// This is typically either a boxed function taking the client options by
73    /// reference and returning a `Transport`, a boxed `Arc<Transport>` or
74    /// alternatively the `DefaultTransportFactory`.
75    pub transport: Option<Arc<dyn TransportFactory>>,
76    /// An optional HTTP proxy to use.
77    ///
78    /// This will default to the `http_proxy` environment variable.
79    pub http_proxy: Option<Cow<'static, str>>,
80    /// An optional HTTPS proxy to use.
81    ///
82    /// This will default to the `HTTPS_PROXY` environment variable
83    /// or `http_proxy` if that one exists.
84    pub https_proxy: Option<Cow<'static, str>>,
85    /// The timeout on client drop for draining events on shutdown.
86    pub shutdown_timeout: Duration,
87    // Other options not documented in Unified API
88    /// Enable Release Health Session tracking.
89    ///
90    /// When automatic session tracking is enabled, a new "user-mode" session
91    /// is started at the time of `sentry::init`, and will persist for the
92    /// application lifetime.
93    pub auto_session_tracking: bool,
94    /// Border frames which indicate a border from a backtrace to
95    /// useless internals. Some are automatically included.
96    pub extra_border_frames: Vec<&'static str>,
97    /// Automatically trim backtraces of junk before sending. (defaults to true)
98    pub trim_backtraces: bool,
99    /// The user agent that should be reported.
100    pub user_agent: Cow<'static, str>,
101}
102
103impl ClientOptions {
104    /// Creates new Options.
105    pub fn new() -> Self {
106        Self::default()
107    }
108
109    /// Adds a configured integration to the options.
110    ///
111    /// # Examples
112    ///
113    /// ```
114    /// struct MyIntegration;
115    ///
116    /// impl sentry::Integration for MyIntegration {}
117    ///
118    /// let options = sentry::ClientOptions::new().add_integration(MyIntegration);
119    /// assert_eq!(options.integrations.len(), 1);
120    /// ```
121    pub fn add_integration<I: Integration>(mut self, integration: I) -> Self {
122        self.integrations.push(Arc::new(integration));
123        self
124    }
125}
126
127impl fmt::Debug for ClientOptions {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        #[derive(Debug)]
130        struct BeforeSend;
131        let before_send = self.before_send.as_ref().map(|_| BeforeSend);
132        #[derive(Debug)]
133        struct BeforeBreadcrumb;
134        let before_breadcrumb = self.before_breadcrumb.as_ref().map(|_| BeforeBreadcrumb);
135        #[derive(Debug)]
136        struct TransportFactory;
137
138        let integrations: Vec<_> = self.integrations.iter().map(|i| i.name()).collect();
139
140        f.debug_struct("ClientOptions")
141            .field("dsn", &self.dsn)
142            .field("debug", &self.debug)
143            .field("release", &self.release)
144            .field("environment", &self.environment)
145            .field("sample_rate", &self.sample_rate)
146            .field("max_breadcrumbs", &self.max_breadcrumbs)
147            .field("attach_stacktrace", &self.attach_stacktrace)
148            .field("send_default_pii", &self.send_default_pii)
149            .field("server_name", &self.server_name)
150            .field("in_app_include", &self.in_app_include)
151            .field("in_app_exclude", &self.in_app_exclude)
152            .field("integrations", &integrations)
153            .field("default_integrations", &self.default_integrations)
154            .field("before_send", &before_send)
155            .field("before_breadcrumb", &before_breadcrumb)
156            .field("transport", &TransportFactory)
157            .field("http_proxy", &self.http_proxy)
158            .field("https_proxy", &self.https_proxy)
159            .field("shutdown_timeout", &self.shutdown_timeout)
160            .field("auto_session_tracking", &self.auto_session_tracking)
161            .field("extra_border_frames", &self.extra_border_frames)
162            .field("trim_backtraces", &self.trim_backtraces)
163            .field("user_agent", &self.user_agent)
164            .finish()
165    }
166}
167
168impl Default for ClientOptions {
169    fn default() -> ClientOptions {
170        let env = if cfg!(debug_assertions) {
171            "development"
172        } else {
173            "production"
174        };
175        ClientOptions {
176            dsn: None,
177            debug: false,
178            release: None,
179            environment: Some(env.into()),
180            sample_rate: 1.0,
181            max_breadcrumbs: 100,
182            attach_stacktrace: false,
183            send_default_pii: false,
184            server_name: None,
185            in_app_include: vec![],
186            in_app_exclude: vec![],
187            integrations: vec![],
188            default_integrations: true,
189            before_send: None,
190            before_breadcrumb: None,
191            transport: None,
192            http_proxy: None,
193            https_proxy: None,
194            shutdown_timeout: Duration::from_secs(2),
195            auto_session_tracking: false,
196            extra_border_frames: vec![],
197            trim_backtraces: true,
198            user_agent: Cow::Borrowed(&USER_AGENT),
199        }
200    }
201}
202
203impl<T: IntoDsn> From<(T, ClientOptions)> for ClientOptions {
204    fn from((into_dsn, mut opts): (T, ClientOptions)) -> ClientOptions {
205        opts.dsn = into_dsn.into_dsn().expect("invalid value for DSN");
206        opts
207    }
208}
209
210impl<T: IntoDsn> From<T> for ClientOptions {
211    fn from(into_dsn: T) -> ClientOptions {
212        ClientOptions {
213            dsn: into_dsn.into_dsn().expect("invalid value for DSN"),
214            ..ClientOptions::default()
215        }
216    }
217}