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



58
59
60
61
62
63
64
65
66
67
# File 'lib/sentry/rails/active_job.rb', line 58

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



77
78
79
80
81
82
# File 'lib/sentry/rails/active_job.rb', line 77

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

.finish_sentry_transaction(transaction, status) ⇒ Object



99
100
101
102
103
104
# File 'lib/sentry/rails/active_job.rb', line 99

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])


94
95
96
97
# File 'lib/sentry/rails/active_job.rb', line 94

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
# 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 = Sentry.start_transaction(
        name: scope.transaction_name,
        source: scope.transaction_source,
        op: OP_NAME,
        origin: SPAN_ORIGIN
      )

      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



69
70
71
72
73
74
75
# File 'lib/sentry/rails/active_job.rb', line 69

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



85
86
87
88
89
90
91
92
# File 'lib/sentry/rails/active_job.rb', line 85

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



106
107
108
109
110
111
112
113
114
115
# File 'lib/sentry/rails/active_job.rb', line 106

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sentry/rails/active_job.rb', line 117

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