Skip to content

Commit 562646b

Browse files
committed
feat: Added support for disabled clients
1 parent a1137c9 commit 562646b

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/client/real.rs

+34-11
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ use constants::{SDK_INFO, USER_AGENT};
2929
/// [the shim client docs](shim/struct.Client.html).
3030
#[derive(Clone)]
3131
pub struct Client {
32-
dsn: Dsn,
3332
options: ClientOptions,
34-
transport: Arc<Transport>,
33+
transport: Option<Arc<Transport>>,
3534
}
3635

3736
impl fmt::Debug for Client {
3837
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3938
f.debug_struct("Client")
40-
.field("dsn", &self.dsn)
39+
.field("dsn", &self.dsn())
4140
.field("options", &self.options)
4241
.finish()
4342
}
@@ -225,11 +224,25 @@ impl Client {
225224

226225
/// Creates a new sentry client for the given DSN.
227226
pub fn with_dsn_and_options(dsn: Dsn, options: ClientOptions) -> Client {
228-
let transport = Transport::new(&dsn, options.user_agent.to_string());
227+
let transport = Transport::new(dsn, options.user_agent.to_string());
229228
Client {
230-
dsn: dsn,
231229
options: options,
232-
transport: Arc::new(transport),
230+
transport: Some(Arc::new(transport))
231+
}
232+
}
233+
234+
/// Creates a new client that does not send anything.
235+
///
236+
/// This is useful when general sentry handling is wanted but a client cannot be bound
237+
/// yet as the DSN might not be available yet. In that case a disabled client can be
238+
/// bound and later replaced by another one.
239+
///
240+
/// A disabled client can be detected by inspecting the DSN. If the DSN is `None` then
241+
/// the client is disabled.
242+
pub fn disabled() -> Client {
243+
Client {
244+
options: Default::default(),
245+
transport: None,
233246
}
234247
}
235248

@@ -400,14 +413,20 @@ impl Client {
400413
}
401414

402415
/// Returns the DSN that constructed this client.
403-
pub fn dsn(&self) -> &Dsn {
404-
&self.dsn
416+
///
417+
/// If the client is in disabled mode this returns `None`.
418+
pub fn dsn(&self) -> Option<&Dsn> {
419+
self.transport.as_ref().map(|x| x.dsn())
405420
}
406421

407422
/// Captures an event and sends it to sentry.
408423
pub fn capture_event(&self, mut event: Event<'static>, scope: Option<&Scope>) -> Uuid {
409-
self.prepare_event(&mut event, scope);
410-
self.transport.send_event(event)
424+
if let Some(ref transport) = self.transport {
425+
self.prepare_event(&mut event, scope);
426+
transport.send_event(event)
427+
} else {
428+
Default::default()
429+
}
411430
}
412431

413432
/// Drains all pending events up to the current time.
@@ -416,7 +435,11 @@ impl Client {
416435
/// given time or `false` if not (for instance because of a timeout).
417436
/// If no timeout is provided the client will wait forever.
418437
pub fn drain_events(&self, timeout: Option<Duration>) -> bool {
419-
self.transport.drain(timeout)
438+
if let Some(ref transport) = self.transport {
439+
transport.drain(timeout)
440+
} else {
441+
true
442+
}
420443
}
421444
}
422445

src/transport.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use protocol::Event;
1313
/// A transport can send rust events.
1414
#[derive(Debug)]
1515
pub struct Transport {
16+
dsn: Dsn,
1617
sender: Mutex<SyncSender<Option<Event<'static>>>>,
1718
drain_signal: Arc<Condvar>,
1819
queue_size: Arc<Mutex<usize>>,
@@ -81,7 +82,7 @@ fn spawn_http_sender(
8182

8283
impl Transport {
8384
/// Creates a new client.
84-
pub fn new(dsn: &Dsn, user_agent: String) -> Transport {
85+
pub fn new(dsn: Dsn, user_agent: String) -> Transport {
8586
let (sender, receiver) = sync_channel(30);
8687
let drain_signal = Arc::new(Condvar::new());
8788
let queue_size = Arc::new(Mutex::new(0));
@@ -93,6 +94,7 @@ impl Transport {
9394
user_agent,
9495
));
9596
Transport {
97+
dsn: dsn,
9698
sender: Mutex::new(sender),
9799
drain_signal: drain_signal,
98100
queue_size: queue_size,
@@ -118,6 +120,11 @@ impl Transport {
118120
event_id
119121
}
120122

123+
/// Returns the dsn of the transport
124+
pub fn dsn(&self) -> &Dsn {
125+
&self.dsn
126+
}
127+
121128
/// Drains remaining messages in the transport.
122129
///
123130
/// This returns `true` if the queue was successfully drained in the

0 commit comments

Comments
 (0)