@@ -92,6 +92,8 @@ pub struct ClientOptions {
92
92
pub https_proxy : Option < Cow < ' static , str > > ,
93
93
/// The timeout on client drop for draining events on shutdown.
94
94
pub shutdown_timeout : Duration ,
95
+ /// Enables debug mode.
96
+ pub debug : bool ,
95
97
/// Attaches stacktraces to messages.
96
98
pub attach_stacktrace : bool ,
97
99
/// If turned on some default PII informat is attached.
@@ -127,6 +129,7 @@ impl fmt::Debug for ClientOptions {
127
129
. field ( "http_proxy" , & self . http_proxy )
128
130
. field ( "https_proxy" , & self . https_proxy )
129
131
. field ( "shutdown_timeout" , & self . shutdown_timeout )
132
+ . field ( "debug" , & self . debug )
130
133
. field ( "attach_stacktrace" , & self . attach_stacktrace )
131
134
. field ( "send_default_pii" , & self . send_default_pii )
132
135
. field ( "before_send" , & BeforeSendSet ( self . before_send . is_some ( ) ) )
@@ -157,6 +160,7 @@ impl Clone for ClientOptions {
157
160
http_proxy : self . http_proxy . clone ( ) ,
158
161
https_proxy : self . https_proxy . clone ( ) ,
159
162
shutdown_timeout : self . shutdown_timeout ,
163
+ debug : self . debug ,
160
164
attach_stacktrace : self . attach_stacktrace ,
161
165
send_default_pii : self . send_default_pii ,
162
166
before_send : self . before_send . clone ( ) ,
@@ -195,6 +199,7 @@ impl Default for ClientOptions {
195
199
. or_else ( || env:: var ( "HTTPS_PROXY" ) . ok ( ) . map ( Cow :: Owned ) )
196
200
. or_else ( || env:: var ( "http_proxy" ) . ok ( ) . map ( Cow :: Owned ) ) ,
197
201
shutdown_timeout : Duration :: from_secs ( 2 ) ,
202
+ debug : false ,
198
203
attach_stacktrace : false ,
199
204
send_default_pii : false ,
200
205
before_send : None ,
@@ -506,7 +511,12 @@ impl Client {
506
511
}
507
512
508
513
if let Some ( ref func) = self . options . before_send {
509
- func ( event)
514
+ sentry_debug ! ( "invoking before_send callback" ) ;
515
+ let id = event. id ;
516
+ func ( event) . or_else ( move || {
517
+ sentry_debug ! ( "before_send dropped event {:?}" , id) ;
518
+ None
519
+ } )
510
520
} else {
511
521
Some ( event)
512
522
}
@@ -518,12 +528,15 @@ impl Client {
518
528
}
519
529
520
530
/// Returns the DSN that constructed this client.
521
- ///
522
- /// If the client is in disabled mode this returns `None`.
523
531
pub fn dsn ( & self ) -> Option < & Dsn > {
524
532
self . options . dsn . as_ref ( )
525
533
}
526
534
535
+ /// Quick check to see if the client is enabled.
536
+ pub fn is_enabled ( & self ) -> bool {
537
+ self . options . dsn . is_some ( ) && self . transport . read ( ) . unwrap ( ) . is_some ( )
538
+ }
539
+
527
540
/// Captures an event and sends it to sentry.
528
541
pub fn capture_event ( & self , event : Event < ' static > , scope : Option < & Scope > ) -> Uuid {
529
542
if let Some ( ref transport) = * self . transport . read ( ) . unwrap ( ) {
@@ -547,8 +560,10 @@ impl Client {
547
560
/// `shutdown_timeout` in the client options.
548
561
pub fn close ( & self , timeout : Option < Duration > ) -> bool {
549
562
if let Some ( transport) = self . transport . write ( ) . unwrap ( ) . take ( ) {
563
+ sentry_debug ! ( "client close; request transport to shut down" ) ;
550
564
transport. shutdown ( timeout. unwrap_or ( self . options . shutdown_timeout ) )
551
565
} else {
566
+ sentry_debug ! ( "client close; no transport to shut down" ) ;
552
567
true
553
568
}
554
569
}
@@ -566,20 +581,37 @@ impl Client {
566
581
/// Helper struct that is returned from `init`.
567
582
///
568
583
/// When this is dropped events are drained with a 1 second timeout.
584
+ #[ must_use = "when the init guard is dropped the transport will be shut down and no further \
585
+ events can be sent. If you do want to ignore this use mem::forget on it."]
569
586
pub struct ClientInitGuard ( Arc < Client > ) ;
570
587
588
+ impl ClientInitGuard {
589
+ /// Quick check if the client is enabled.
590
+ pub fn is_enabled ( & self ) -> bool {
591
+ self . 0 . is_enabled ( )
592
+ }
593
+ }
594
+
571
595
impl Drop for ClientInitGuard {
572
596
fn drop ( & mut self ) {
597
+ if self . is_enabled ( ) {
598
+ sentry_debug ! ( "dropping client guard -> disposing client" ) ;
599
+ } else {
600
+ sentry_debug ! ( "dropping client guard (no client to dispose)" ) ;
601
+ }
573
602
self . 0 . close ( None ) ;
574
603
}
575
604
}
576
605
577
606
/// Creates the Sentry client for a given client config and binds it.
578
607
///
579
- /// This returns a client init guard that if kept in scope will help the
580
- /// client send events before the application closes by calling drain on
581
- /// the generated client. If the scope guard is immediately dropped then
582
- /// no draining will take place so ensure it's bound to a variable.
608
+ /// This returns a client init guard that must kept in scope will help the
609
+ /// client send events before the application closes. When the guard is
610
+ /// dropped then the transport that was initialized shuts down and no
611
+ /// further events can be set on it.
612
+ ///
613
+ /// If you don't want (or can) keep the guard around it's permissible to
614
+ /// call `mem::forget` on it.
583
615
///
584
616
/// # Examples
585
617
///
@@ -589,6 +621,33 @@ impl Drop for ClientInitGuard {
589
621
/// }
590
622
/// ```
591
623
///
624
+ /// Of if draining on shutdown should be ignored:
625
+ ///
626
+ /// ```rust
627
+ /// use std::mem;
628
+ ///
629
+ /// fn main() {
630
+ /// mem::forget(sentry::init("https://[email protected] /1234"));
631
+ /// }
632
+ /// ```
633
+ ///
634
+ /// The guard returned can also be inspected to see if a client has been
635
+ /// created to enable further configuration:
636
+ ///
637
+ /// ```rust
638
+ /// use sentry::integrations::panic::register_panic_handler;
639
+ ///
640
+ /// fn main() {
641
+ /// let sentry = sentry::init(sentry::ClientOptions {
642
+ /// release: Some("[email protected] ".into()),
643
+ /// ..Default::default()
644
+ /// });
645
+ /// if sentry.is_enabled() {
646
+ /// register_panic_handler();
647
+ /// }
648
+ /// }
649
+ /// ```
650
+ ///
592
651
/// This behaves similar to creating a client by calling `Client::from_config`
593
652
/// and to then bind it to the hub except it's also possible to directly pass
594
653
/// a client. For more information about the formats accepted see
@@ -597,6 +656,11 @@ impl Drop for ClientInitGuard {
597
656
pub fn init < C : Into < Client > > ( cfg : C ) -> ClientInitGuard {
598
657
let client = Arc :: new ( cfg. into ( ) ) ;
599
658
Hub :: with ( |hub| hub. bind_client ( Some ( client. clone ( ) ) ) ) ;
659
+ if let Some ( dsn) = client. dsn ( ) {
660
+ sentry_debug ! ( "enabled sentry client for DSN {}" , dsn) ;
661
+ } else {
662
+ sentry_debug ! ( "initialized disabled sentry client due to disabled or invalid DSN" ) ;
663
+ }
600
664
ClientInitGuard ( client)
601
665
}
602
666
0 commit comments