Skip to content

Commit a1b6ae1

Browse files
authored
feat: Added debug logs (#90)
1 parent 23bbebc commit a1b6ae1

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ with_backtrace = ["backtrace", "regex"]
2222
with_panic = ["with_backtrace"]
2323
with_failure = ["failure", "with_backtrace"]
2424
with_log = ["log", "with_backtrace"]
25+
with_debug_to_log = ["log"]
2526
with_env_logger = ["with_log", "env_logger"]
2627
with_error_chain = ["error-chain", "with_backtrace"]
2728
with_device_info = ["libc", "hostname", "uname", "with_client_implementation"]

src/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ pub struct ClientOptions {
9292
/// The timeout on client drop for draining events on shutdown.
9393
pub shutdown_timeout: Duration,
9494
/// Enables debug mode.
95+
///
96+
/// In debug mode debug information is printed to stderr to help you understand what
97+
/// sentry is doing. When the `with_debug_to_log` flag is enabled Sentry will instead
98+
/// log to the `sentry` logger independently of this flag with the `Debug` level.
9599
pub debug: bool,
96100
/// Attaches stacktraces to messages.
97101
pub attach_stacktrace: bool,

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
//! * `with_device_info`: enables the device info context
9898
//! * `with_rust_info`: enables the rust compiler info context
9999
//! * `with_debug_meta`: enables debug meta support (permits server side symbolication)
100+
//! * `with_debug_to_log`: when enabled sentry will debug log to a debug log at all times
101+
//! instead of printing to stderr when debug is enabled on the hub.
100102
//!
101103
//! additional features:
102104
//!
@@ -144,7 +146,8 @@ extern crate failure;
144146
#[cfg(feature = "with_error_chain")]
145147
extern crate error_chain;
146148

147-
#[cfg(feature = "with_log")]
149+
#[cfg(any(feature = "with_log", feature = "with_debug_to_log"))]
150+
#[cfg_attr(feature = "with_debug_to_log", macro_use)]
148151
extern crate log;
149152

150153
#[cfg(feature = "with_env_logger")]

src/macros.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ macro_rules! with_client_impl {
4040
macro_rules! sentry_debug {
4141
($($arg:tt)*) => {
4242
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-
});
43+
#[cfg(feature = "with_debug_to_log")] {
44+
debug!(target: "sentry", $($arg)*);
45+
}
46+
#[cfg(not(feature = "with_debug_to_log"))] {
47+
$crate::Hub::with(|hub| {
48+
if hub.client().map_or(false, |c| c.options().debug) {
49+
eprint!("[sentry] ");
50+
eprintln!($($arg)*);
51+
}
52+
});
53+
}
4954
}}
5055
}
5156
}

src/transport.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,36 @@ fn spawn_http_sender(
144144
}
145145

146146
// while we are disabled due to rate limits, skip
147-
if disabled > SystemTime::now() {
147+
let now = SystemTime::now();
148+
if let Ok(time_left) = disabled.duration_since(now) {
149+
sentry_debug!(
150+
"Skipping event send because we're disabled due to rate limits for {}s",
151+
time_left.as_secs()
152+
);
148153
continue;
149154
}
150155

151-
if let Ok(resp) = client
156+
match client
152157
.post(url.as_str())
153158
.json(&event)
154159
.header("X-Sentry-Auth", dsn.to_auth(Some(&user_agent)).to_string())
155160
.send()
156161
{
157-
if resp.status() == 429 {
158-
if let Some(retry_after) = resp
159-
.headers()
160-
.get(RETRY_AFTER)
161-
.and_then(|x| x.to_str().ok())
162-
.and_then(parse_retry_after)
163-
{
164-
disabled = retry_after;
162+
Ok(resp) => {
163+
if resp.status() == 429 {
164+
if let Some(retry_after) = resp
165+
.headers()
166+
.get(RETRY_AFTER)
167+
.and_then(|x| x.to_str().ok())
168+
.and_then(parse_retry_after)
169+
{
170+
disabled = retry_after;
171+
}
165172
}
166173
}
174+
Err(err) => {
175+
sentry_debug!("Failed to send event: {}", err);
176+
}
167177
}
168178

169179
let mut size = queue_size.lock().unwrap();

0 commit comments

Comments
 (0)