Skip to content

sentry::capture_error does not capture the backtrace even when the feature is enabled #328

@Tuetuopay

Description

@Tuetuopay

Environment

How do you use Sentry?
Self-hosted 21.5.0 and 9.1.2

Which SDK and version?
Rust 0.18 and 0.22 (because of Tokio 0.2 and 1.0)

Steps to Reproduce

  1. Install sentry integration with backtraces enabled
  2. Call sentry::capture_error
  3. Go to sentry, see the absence of stacktrace
  • Cargo.toml
sentry = { version = "0.22", features = ["backtrace"] }
  • main.rs
fn main() {
    sentry::init(("https://my-dsn", sentry::ClientOptions {
        attach_stacktrace: true,
        ..Default::default()
    });
    println!("Sentry installed");

    let error = "thisisnotanumber".parse::<u32>().unwrap_err();
    sentry::capture_error(&error);
}

Expected Result

The stacktrace would be shown in the web ui.

sentry-backtrace

Actual Result

The web UI shows no stacktrace for the issue.

sentry-nobacktrace

Fix suggestion

The above screenshot with backtrace was obtained by slightly tweaking the condition in the AttachStacktraceIntegration, which checks whether the event contains exceptions before attaching the stacktrace. However, in the case of events made from capture_error, they do not contain a backtrace, resulting in no backtrace being attached to the event. The tweak is as follows:

diff --git a/sentry-backtrace/src/integration.rs b/sentry-backtrace/src/integration.rs
index 49f6826..e6c3a5a 100644
--- a/sentry-backtrace/src/integration.rs
+++ b/sentry-backtrace/src/integration.rs
@@ -73,7 +73,7 @@ impl Integration for AttachStacktraceIntegration {
         mut event: Event<'static>,
         options: &ClientOptions,
     ) -> Option<Event<'static>> {
-        if options.attach_stacktrace && event.exception.is_empty() {
+        if options.attach_stacktrace && event.threads.is_empty() {
             let thread = current_thread(true);
             if thread.stacktrace.is_some() {
                 event.threads.values.push(thread);

While this works, it may not be ideal as I feel like this is not really what the threads field is for. A fix that may be cleaner would be to record the backtrace in sentry_core::error::event_from_error and/or exception_from_error.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions