@@ -29,15 +29,14 @@ use constants::{SDK_INFO, USER_AGENT};
29
29
/// [the shim client docs](shim/struct.Client.html).
30
30
#[ derive( Clone ) ]
31
31
pub struct Client {
32
- dsn : Dsn ,
33
32
options : ClientOptions ,
34
- transport : Arc < Transport > ,
33
+ transport : Option < Arc < Transport > > ,
35
34
}
36
35
37
36
impl fmt:: Debug for Client {
38
37
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
39
38
f. debug_struct ( "Client" )
40
- . field ( "dsn" , & self . dsn )
39
+ . field ( "dsn" , & self . dsn ( ) )
41
40
. field ( "options" , & self . options )
42
41
. finish ( )
43
42
}
@@ -225,11 +224,25 @@ impl Client {
225
224
226
225
/// Creates a new sentry client for the given DSN.
227
226
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 ( ) ) ;
229
228
Client {
230
- dsn : dsn,
231
229
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 ,
233
246
}
234
247
}
235
248
@@ -400,14 +413,20 @@ impl Client {
400
413
}
401
414
402
415
/// 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 ( ) )
405
420
}
406
421
407
422
/// Captures an event and sends it to sentry.
408
423
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
+ }
411
430
}
412
431
413
432
/// Drains all pending events up to the current time.
@@ -416,7 +435,11 @@ impl Client {
416
435
/// given time or `false` if not (for instance because of a timeout).
417
436
/// If no timeout is provided the client will wait forever.
418
437
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
+ }
420
443
}
421
444
}
422
445
0 commit comments