Skip to main content

sentry_core/transport/
options.rs

1//! Includes the [`TransportOptions`] struct.
2
3use std::borrow::Cow;
4use std::num::NonZeroUsize;
5
6use sentry_types::Dsn;
7
8#[cfg(feature = "client")]
9use crate::client_report::Recorder as ClientReportRecorder;
10use crate::ClientOptions;
11
12/// Options for a transport.
13#[derive(Debug)]
14#[must_use]
15#[non_exhaustive]
16pub struct TransportOptions {
17    /// The transport's Sentry DSN.
18    pub dsn: Dsn,
19    /// The user agent sent with transport requests.
20    pub user_agent: Cow<'static, str>,
21    /// An optional HTTP proxy to use.
22    pub http_proxy: Option<Cow<'static, str>>,
23    /// An optional HTTPS proxy to use.
24    pub https_proxy: Option<Cow<'static, str>>,
25    /// Whether TLS certificate validation should be disabled.
26    pub accept_invalid_certs: bool,
27    /// A handle for recording lost Sentry data.
28    #[cfg(feature = "client")]
29    pub client_report_recorder: ClientReportRecorder,
30    /// The maximum number of commands the transport channel can queue.
31    ///
32    /// The channel primarily carries envelopes; control commands, such as flushing and shutdown,
33    /// also count against this capacity.
34    ///
35    /// If `None`, the transport uses its own default. The current default is `30` for all
36    /// built-in transports, but this is subject to change.
37    pub transport_channel_capacity: Option<NonZeroUsize>,
38}
39
40impl TransportOptions {
41    /// Try to convert a [`&ClientOptions`](ClientOptions) to a [`TransportOptions`] by extracting
42    /// the relevant fields from the `ClientOptions`.
43    ///
44    /// This method is provided so that code which expects [`TransportOptions`] can be
45    /// backwards-compatible with older code, which provides `ClientOptions`.
46    ///
47    /// Returns [`None`] if `options.dsn` is `None`, `Some(_)` otherwise.
48    pub fn try_from_client_options(options: &ClientOptions) -> Option<Self> {
49        let ClientOptions {
50            dsn,
51            http_proxy,
52            https_proxy,
53            accept_invalid_certs,
54            user_agent,
55            transport_channel_capacity,
56            ..
57        } = options;
58
59        dsn.as_ref().cloned().map(|dsn| Self {
60            dsn,
61            user_agent: user_agent.clone(),
62            http_proxy: http_proxy.clone(),
63            https_proxy: https_proxy.clone(),
64            accept_invalid_certs: *accept_invalid_certs,
65            #[cfg(feature = "client")]
66            client_report_recorder: ClientReportRecorder::new_no_op(),
67            transport_channel_capacity: *transport_channel_capacity,
68        })
69    }
70
71    /// Converts these [`TransportOptions`] into [`ClientOptions`].
72    ///
73    /// This method is provided for backwards-compatibility with custom transports which cannot
74    /// be contructed from [`TransportOptions`] because they expect [`ClientOptions`].
75    ///
76    /// Any fields on [`ClientOptions`] which are not present in [`TransportOptions`] will be
77    /// set to their default values.
78    pub(crate) fn into_client_options(self) -> ClientOptions {
79        let Self {
80            dsn,
81            user_agent,
82            http_proxy,
83            https_proxy,
84            accept_invalid_certs,
85            #[cfg(feature = "client")]
86                client_report_recorder: _,
87            transport_channel_capacity,
88        } = self;
89
90        let dsn = Some(dsn);
91
92        ClientOptions {
93            dsn,
94            user_agent,
95            http_proxy,
96            https_proxy,
97            accept_invalid_certs,
98            transport_channel_capacity,
99            ..Default::default()
100        }
101    }
102}