Class: Sentry::Rails::ActiveJobExtensions::SentryReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/rails/active_job.rb

Constant Summary collapse

OP_NAME =
"queue.active_job"
SPAN_ORIGIN =
"auto.queue.active_job"
EVENT_HANDLERS =
{
  "enqueue_retry.active_job" => :retry_handler
}

Class Method Summary collapse

Class Method Details

.capture_exception(job, e) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/sentry/rails/active_job.rb', line 62

def capture_exception(job, e)
  Sentry::Rails.capture_exception(
    e,
    extra: sentry_context(job),
    tags: {
      job_id: job.job_id,
      provider_job_id: job.provider_job_id
    }
  )
end

.detach_event_handlersObject



81
82
83
84
85
86
# File 'lib/sentry/rails/active_job.rb', line 81

def detach_event_handlers
  subscribers.each do |subscriber|
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end
  subscribers.clear
end

.finish_sentry_transaction(transaction, status) ⇒ Object



103
104
105
106
107
108
# File 'lib/sentry/rails/active_job.rb', line 103

def finish_sentry_transaction(transaction, status)
  return unless transaction

  transaction.set_http_status(status)
  transaction.finish
end

.handle_error_event(*args) {|event.payload[:job], event.payload[:error]| ... } ⇒ Object

Yields:

  • (event.payload[:job], event.payload[:error])


98
99
100
101
# File 'lib/sentry/rails/active_job.rb', line 98

def handle_error_event(*args)
  event = ActiveSupport::Notifications::Event.new(*args)
  yield(event.payload[:job], event.payload[:error])
end

.record(job, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sentry/rails/active_job.rb', line 31

def record(job, &block)
  Sentry.with_scope do |scope|
    begin
      scope.set_transaction_name(job.class.name, source: :task)
      transaction =
        if job.is_a?(::Sentry::SendEventJob)
          nil
        else
          Sentry.start_transaction(
            name: scope.transaction_name,
            source: scope.transaction_source,
            op: OP_NAME,
            origin: SPAN_ORIGIN
          )
        end

      scope.set_span(transaction) if transaction

      yield.tap do
        finish_sentry_transaction(transaction, 200)
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      finish_sentry_transaction(transaction, 500)

      capture_exception(job, e)

      raise
    end
  end
end

.register_event_handlersObject



73
74
75
76
77
78
79
# File 'lib/sentry/rails/active_job.rb', line 73

def register_event_handlers
  EVENT_HANDLERS.each do |name, handler|
    subscribers << ActiveSupport::Notifications.subscribe(name) do |*args|
      public_send(handler, *args)
    end
  end
end

.retry_handler(*args) ⇒ Object

This handler does not capture error unless ‘active_job_report_on_retry_error` is true



89
90
91
92
93
94
95
96
# File 'lib/sentry/rails/active_job.rb', line 89

def retry_handler(*args)
  handle_error_event(*args) do |job, error|
    return if !Sentry.initialized? || job.already_supported_by_sentry_integration?
    return unless Sentry.configuration.rails.active_job_report_on_retry_error

    capture_exception(job, error)
  end
end

.sentry_context(job) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/sentry/rails/active_job.rb', line 110

def sentry_context(job)
  {
    active_job: job.class.name,
    arguments: sentry_serialize_arguments(job.arguments),
    scheduled_at: job.scheduled_at,
    job_id: job.job_id,
    provider_job_id: job.provider_job_id,
    locale: job.locale
  }
end

.sentry_serialize_arguments(argument) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sentry/rails/active_job.rb', line 121

def sentry_serialize_arguments(argument)
  case argument
  when Range
    if (argument.begin || argument.end).is_a?(ActiveSupport::TimeWithZone)
      argument.to_s
    else
      argument.map { |v| sentry_serialize_arguments(v) }
    end
  when Hash
    argument.transform_values { |v| sentry_serialize_arguments(v) }
  when Array, Enumerable
    argument.map { |v| sentry_serialize_arguments(v) }
  when ->(v) { v.respond_to?(:to_global_id) }
    argument.to_global_id.to_s rescue argument
  else
    argument
  end
end