@@ -29,15 +29,14 @@ use constants::{SDK_INFO, USER_AGENT};
2929/// [the shim client docs](shim/struct.Client.html).
3030#[ derive( Clone ) ]
3131pub struct Client {
32- dsn : Dsn ,
3332 options : ClientOptions ,
34- transport : Arc < Transport > ,
33+ transport : Option < Arc < Transport > > ,
3534}
3635
3736impl 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
0 commit comments