Class: Sentry::Resque::SentryReporter

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

Constant Summary collapse

SPAN_ORIGIN =
"auto.queue.resque"

Class Method Summary collapse

Class Method Details

.finish_transaction(transaction, status) ⇒ Object



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

def finish_transaction(transaction, status)
  return unless transaction

  transaction.set_http_status(status)
  transaction.finish
end

.generate_contexts(queue, worker, payload) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sentry/resque.rb', line 52

def generate_contexts(queue, worker, payload)
  context = {}

  if payload["class"] == "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper"
    active_job_payload = payload["args"].first

    context[:"Active-Job"] = {
      job_class: active_job_payload["job_class"],
      job_id: active_job_payload["job_id"],
      arguments: active_job_payload["arguments"],
      executions: active_job_payload["executions"],
      exception_executions: active_job_payload["exception_executions"],
      locale: active_job_payload["locale"],
      enqueued_at: active_job_payload["enqueued_at"],
      queue: queue,
      worker: worker.to_s
    }
  else
    context[:"Resque"] = {
      job_class: payload["class"],
      arguments: payload["args"],
      queue: queue,
      worker: worker.to_s
    }
  end

  context
end

.record(queue, worker, payload, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sentry/resque.rb', line 21

def record(queue, worker, payload, &block)
  Sentry.with_scope do |scope|
    begin
      contexts = generate_contexts(queue, worker, payload)
      scope.set_contexts(**contexts)
      scope.set_tags("resque.queue" => queue)

      name = contexts.dig(:"Active-Job", :job_class) || contexts.dig(:"Resque", :job_class)
      scope.set_transaction_name(name, source: :task)
      transaction = Sentry.start_transaction(
        name: scope.transaction_name,
        source: scope.transaction_source,
        op: "queue.resque",
        origin: SPAN_ORIGIN
      )

      scope.set_span(transaction) if transaction

      yield

      finish_transaction(transaction, 200)
    rescue Exception => exception
      raise_if_retries_remain(payload["class"], exception)

      ::Sentry::Resque.capture_exception(exception, hint: { background: false })
      finish_transaction(transaction, 500)
      raise
    end
  end
end