getsentry / sentry-ruby

Sentry SDK for Ruby
https://sentry.io/for/ruby
MIT License
933 stars 494 forks source link

add hint support to Sentry::Rails::ErrorSubscriber #2228

Closed modosc closed 9 months ago

modosc commented 10 months ago

if i want to explicitly send an exception to sentry that would otherwise be ignored because of Sentry.configuration.excluded_exceptions i can do this:

Sentry.capture_exception(ignored_exception, hint: { ignore_exclusions: true }) 

however, as far as i can tell this isn't possible if i'm using the rails error reporting interface.

it would be useful to expose this there.

i think it could be achieved the same way tags are handled here:

https://github.com/getsentry/sentry-ruby/blob/master/sentry-rails/lib/sentry/rails/error_subscriber.rb#L17C38-L20

so you could just provide a hint key in context::

Rails.error.report exception, e, handled: true, severity: :error, context: { hint: { ignore_exclusions: true } }

and you could then pass it in via this (untested) implementation:

      def report(error, handled:, severity:, context:, source: nil)
        tags = { handled: handled }

        if source
          return if SKIP_SOURCES.match?(source)
          tags[:source] = source
        end

        if context[:tags].is_a?(Hash)
          context = context.dup
          tags.merge!(context.delete(:tags))
        end

        hint = {}
        if context[:hint].is_a?(Hash)
          context = context.dup
          hint.merge!(context.delete(:hint)
       end

        Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => context }, tags: tags, hint: hint)
      end
sl0thentr0py commented 9 months ago

@modosc yep this makes sense to have, will add it when I have some time or feel free to PR!

modosc commented 9 months ago

@sl0thentr0py i opened https://github.com/getsentry/sentry-ruby/pull/2235 - i just copied the code/spec pattern from the existing tags: implementation.