sentry-ruby icon indicating copy to clipboard operation
sentry-ruby copied to clipboard

Allow custom backtrace in `capture_message`

Open kirbycool opened this issue 5 years ago • 1 comments

Describe the idea Add an option to add a custom backtrace to capture_message. This was supported in the deprecated sentry-raven sdk using the backtrace option, as described in the doc https://docs.sentry.io/clients/ruby/usage/. Since it existed in the previous version, it would be nice to also support this feature in the new sentry-ruby sdk, or explicitly call out its removal with some justification in the migration guide.

Why do you think it's beneficial to most of the users I've needed this feature when writing code that uses backtrace capturing error objects rather than exceptions for error handling (think go style errors or monadic Result types). There would be a single handler that sends events to sentry. In this case, the backtrace where the event is sent is not important; instead, we'd like to send the backtrace captured by the error object.

Here's some rough pseudocode to illustrate the use case:

class BacktraceError
  attr_reader :backtrace, :message

  def initialize(message)
    @message = message
    @backtrace = Kernel.caller
  end
end

class RequestController
  def handle_request
    result, error = do_failing_thing
    if error
      # How can we send the error backtrace?
      Sentry.capture_message(error.message)
      # ... return some error
    end

    # ... do something with result
  end

  def do_failing_thing
    [nil, BacktraceError.new('it broke!')]
  end
end

There may also be some other use cases that I haven't thought of.

Possible implementation Currently you can hack together this feature by manually building/modifying a Sentry::Event and sending that.

event = Sentry.get_current_hub.current_client.event_from_message(error.message)
event.add_threads_interface(backtrace: error.backtrace)
Sentry.capture_event(event)

This emulates some internal behavior https://github.com/getsentry/sentry-ruby/blob/36f9bed7b7fb13b08c5b2a6f235979d03e15fbce/sentry-ruby/lib/sentry/hub.rb#L111 https://github.com/getsentry/sentry-ruby/blob/36f9bed7b7fb13b08c5b2a6f235979d03e15fbce/sentry-ruby/lib/sentry/client.rb#L56

So while it's possible to do, it's uncomfortable using some non public apis that may break in future releases.

I think adding this feature could be as easy as adding an optional backtrace option to event_from_message and passing it down from capture_message. Here https://github.com/getsentry/sentry-ruby/blob/36f9bed7b7fb13b08c5b2a6f235979d03e15fbce/sentry-ruby/lib/sentry/client.rb#L55

If you think this is a good idea, I'm happy to help out with a pr!

kirbycool avatar Apr 14 '21 20:04 kirbycool

@kirbycool how about initializing a normal exception object manually and do this?

 def handle_request
    result, error = do_failing_thing
    if error
      # How can we send the error backtrace?
      Sentry.capture_exception(error)
      # ... return some error
    end

    # ... do something with result
  end
  
  def do_failing_thing
    error = StandardError.new('it broke!')
    error.set_backtrace(caller)
    [nil, error]
  end

st0012 avatar Apr 22 '21 15:04 st0012