Skip to content

Commit d7c15e9

Browse files
authored
feat: Added some debug logs (#81)
1 parent 5454ff8 commit d7c15e9

File tree

6 files changed

+94
-9
lines changed

6 files changed

+94
-9
lines changed

examples/before-send.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
});
1616
Some(event)
1717
}))),
18+
debug: true,
1819
..Default::default()
1920
});
2021

src/client.rs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub struct ClientOptions {
9292
pub https_proxy: Option<Cow<'static, str>>,
9393
/// The timeout on client drop for draining events on shutdown.
9494
pub shutdown_timeout: Duration,
95+
/// Enables debug mode.
96+
pub debug: bool,
9597
/// Attaches stacktraces to messages.
9698
pub attach_stacktrace: bool,
9799
/// If turned on some default PII informat is attached.
@@ -127,6 +129,7 @@ impl fmt::Debug for ClientOptions {
127129
.field("http_proxy", &self.http_proxy)
128130
.field("https_proxy", &self.https_proxy)
129131
.field("shutdown_timeout", &self.shutdown_timeout)
132+
.field("debug", &self.debug)
130133
.field("attach_stacktrace", &self.attach_stacktrace)
131134
.field("send_default_pii", &self.send_default_pii)
132135
.field("before_send", &BeforeSendSet(self.before_send.is_some()))
@@ -157,6 +160,7 @@ impl Clone for ClientOptions {
157160
http_proxy: self.http_proxy.clone(),
158161
https_proxy: self.https_proxy.clone(),
159162
shutdown_timeout: self.shutdown_timeout,
163+
debug: self.debug,
160164
attach_stacktrace: self.attach_stacktrace,
161165
send_default_pii: self.send_default_pii,
162166
before_send: self.before_send.clone(),
@@ -195,6 +199,7 @@ impl Default for ClientOptions {
195199
.or_else(|| env::var("HTTPS_PROXY").ok().map(Cow::Owned))
196200
.or_else(|| env::var("http_proxy").ok().map(Cow::Owned)),
197201
shutdown_timeout: Duration::from_secs(2),
202+
debug: false,
198203
attach_stacktrace: false,
199204
send_default_pii: false,
200205
before_send: None,
@@ -506,7 +511,12 @@ impl Client {
506511
}
507512

508513
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+
})
510520
} else {
511521
Some(event)
512522
}
@@ -518,12 +528,15 @@ impl Client {
518528
}
519529

520530
/// Returns the DSN that constructed this client.
521-
///
522-
/// If the client is in disabled mode this returns `None`.
523531
pub fn dsn(&self) -> Option<&Dsn> {
524532
self.options.dsn.as_ref()
525533
}
526534

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+
527540
/// Captures an event and sends it to sentry.
528541
pub fn capture_event(&self, event: Event<'static>, scope: Option<&Scope>) -> Uuid {
529542
if let Some(ref transport) = *self.transport.read().unwrap() {
@@ -547,8 +560,10 @@ impl Client {
547560
/// `shutdown_timeout` in the client options.
548561
pub fn close(&self, timeout: Option<Duration>) -> bool {
549562
if let Some(transport) = self.transport.write().unwrap().take() {
563+
sentry_debug!("client close; request transport to shut down");
550564
transport.shutdown(timeout.unwrap_or(self.options.shutdown_timeout))
551565
} else {
566+
sentry_debug!("client close; no transport to shut down");
552567
true
553568
}
554569
}
@@ -566,20 +581,37 @@ impl Client {
566581
/// Helper struct that is returned from `init`.
567582
///
568583
/// 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."]
569586
pub struct ClientInitGuard(Arc<Client>);
570587

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+
571595
impl Drop for ClientInitGuard {
572596
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+
}
573602
self.0.close(None);
574603
}
575604
}
576605

577606
/// Creates the Sentry client for a given client config and binds it.
578607
///
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.
583615
///
584616
/// # Examples
585617
///
@@ -589,6 +621,33 @@ impl Drop for ClientInitGuard {
589621
/// }
590622
/// ```
591623
///
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+
///
592651
/// This behaves similar to creating a client by calling `Client::from_config`
593652
/// and to then bind it to the hub except it's also possible to directly pass
594653
/// a client. For more information about the formats accepted see
@@ -597,6 +656,11 @@ impl Drop for ClientInitGuard {
597656
pub fn init<C: Into<Client>>(cfg: C) -> ClientInitGuard {
598657
let client = Arc::new(cfg.into());
599658
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+
}
600664
ClientInitGuard(client)
601665
}
602666

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! The `sentry::init` function returns a guard that when dropped will flush
2222
//! Events that were not yet sent to the sentry service. It has a two second
2323
//! deadline for this so shutdown of applications might slightly delay as a result
24-
//! of this.
24+
//! of this. Keep the guard around or sending events will not work.
2525
//!
2626
//! ```
2727
//! extern crate sentry;

src/macros.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ macro_rules! with_client_impl {
3636
};
3737
}
3838

39+
#[allow(unused_macros)]
40+
macro_rules! sentry_debug {
41+
($($arg:tt)*) => {
42+
with_client_impl! {{
43+
$crate::Hub::with(|hub| {
44+
if hub.client().map_or(false, |c| c.options().debug) {
45+
eprint!("[sentry] ");
46+
eprintln!($($arg)*);
47+
}
48+
});
49+
}}
50+
}
51+
}
52+
3953
#[allow(unused_macros)]
4054
macro_rules! minimal_unreachable {
4155
() => {

src/scope/real.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,13 @@ impl Scope {
299299
}
300300

301301
for processor in &self.event_processors {
302+
let id = event.id;
302303
event = match processor(event) {
303304
Some(event) => event,
304-
None => return None,
305+
None => {
306+
sentry_debug!("event processor dropped event {:?}", id);
307+
return None;
308+
}
305309
}
306310
}
307311

src/transport.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl Transport for HttpTransport {
230230
}
231231

232232
fn shutdown(&self, timeout: Duration) -> bool {
233+
sentry_debug!("shutting down http transport");
233234
let guard = self.queue_size.lock().unwrap();
234235
if *guard == 0 {
235236
true
@@ -244,6 +245,7 @@ impl Transport for HttpTransport {
244245

245246
impl Drop for HttpTransport {
246247
fn drop(&mut self) {
248+
sentry_debug!("dropping http transport");
247249
self.shutdown_immediately.store(true, Ordering::SeqCst);
248250
if let Ok(sender) = self.sender.lock() {
249251
sender.send(None).ok();

0 commit comments

Comments
 (0)