getsentry / sentry-rust

Official Sentry SDK for Rust
https://sentry.io/
Apache License 2.0
620 stars 153 forks source link

Can I get a backtrace from an `Event`? #626

Closed hwittenborn closed 1 year ago

hwittenborn commented 1 year ago

In my application, I'd like to do the following:

  1. Catch any panics with sentry::init
  2. Present the panic to the user (through a graphical user interface)
  3. If the user accepts, upload the backtrace to sentry

The way I have this (sort of) set up right now is to use before_send to invoke the GUI:

    let _guard = sentry::init(("https://my-dsn", ClientOptions {
        before_send: Some(Arc::new(|mut event| {
            // How can I obtain the Rust backtrace from here?
            ...
            my_gui_code...
        })),
        ..Default::default()
    }));

But as shown in that spot in the example, I'm not quite sure how I'd get the recorded stack trace through there.

Is there any way I'd be able to do that?

hwittenborn commented 1 year ago

I managed to solve this in my application by disabling the Sentry panic handler, and then getting the backtrace with std::backtrace::Backtrace in my code's panic_handler, and then just calling PanicIntegration::new().event_from_panic_info(panic_info) manually and uploading the result.

I should be good with this issue now.