Class: Sentry::Rails::LogSubscribers::ActiveJobSubscriber

Inherits:
Sentry::Rails::LogSubscriber show all
Defined in:
lib/sentry/rails/log_subscribers/active_job_subscriber.rb

Overview

LogSubscriber for ActiveJob events that captures background job execution and logs them using Sentry's structured logging system.

This subscriber captures various ActiveJob events including job execution, enqueueing, retries, and failures with relevant job information.

Examples:

Usage

# Enable structured logging for ActiveJob
Sentry.init do |config|
  config.rails.structured_logging = true
  config.rails.structured_logging.subscribers = { active_job: Sentry::Rails::LogSubscribers::ActiveJobSubscriber }
end

Constant Summary

Constants inherited from Sentry::Rails::LogSubscriber

Sentry::Rails::LogSubscriber::ORIGIN

Instance Method Summary collapse

Methods inherited from Sentry::Rails::LogSubscriber

#detach_from

Instance Method Details

#discard(event) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sentry/rails/log_subscribers/active_job_subscriber.rb', line 115

def discard(event)
  return unless Sentry.initialized?

  job = event.payload[:job]
  error = event.payload[:error]

  attributes = {
    job_class: job.class.name,
    job_id: job.job_id,
    queue_name: job.queue_name,
    executions: job.executions
  }

  attributes[:error_class] = error.class.name if error
  attributes[:error_message] = error.message if error

  message = "Job discarded: #{job.class.name}"

  log_structured_event(
    message: message,
    level: :warn,
    attributes: attributes
  )
end

#enqueue(event) ⇒ Object

Handle enqueue.active_job events

Parameters:

  • event (ActiveSupport::Notifications::Event)

    The job enqueue event



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sentry/rails/log_subscribers/active_job_subscriber.rb', line 63

def enqueue(event)
  return unless Sentry.initialized?

  job = event.payload[:job]

  attributes = {
    job_class: job.class.name,
    job_id: job.job_id,
    queue_name: job.queue_name,
    priority: job.priority
  }

  attributes[:adapter] = job.class.queue_adapter.class.name if job.class.respond_to?(:queue_adapter)

  if job.scheduled_at
    attributes[:scheduled_at] = job.scheduled_at.iso8601
    attributes[:delay_seconds] = (job.scheduled_at - Time.current).round(2)
  end

  message = "Job enqueued: #{job.class.name}"

  log_structured_event(
    message: message,
    level: :info,
    attributes: attributes
  )
end

#perform(event) ⇒ Object

Handle perform.active_job events

Parameters:

  • event (ActiveSupport::Notifications::Event)

    The job performance event



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
51
52
53
54
55
56
57
58
# File 'lib/sentry/rails/log_subscribers/active_job_subscriber.rb', line 24

def perform(event)
  return unless Sentry.initialized?

  job = event.payload[:job]
  duration = duration_ms(event)

  attributes = {
    job_class: job.class.name,
    job_id: job.job_id,
    queue_name: job.queue_name,
    duration_ms: duration,
    executions: job.executions,
    priority: job.priority
  }

  attributes[:adapter] = job.class.queue_adapter.class.name

  if job.scheduled_at
    attributes[:scheduled_at] = job.scheduled_at.iso8601
    attributes[:delay_ms] = ((Time.current - job.scheduled_at) * 1000).round(2)
  end

  if Sentry.configuration.data_collection.queues && job.arguments.present?
    filtered_args = filter_sensitive_arguments(job.arguments)
    attributes[:arguments] = filtered_args unless filtered_args.empty?
  end

  message = "Job performed: #{job.class.name}"

  log_structured_event(
    message: message,
    level: :info,
    attributes: attributes
  )
end

#retry_stopped(event) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sentry/rails/log_subscribers/active_job_subscriber.rb', line 91

def retry_stopped(event)
  return unless Sentry.initialized?

  job = event.payload[:job]
  error = event.payload[:error]

  attributes = {
    job_class: job.class.name,
    job_id: job.job_id,
    queue_name: job.queue_name,
    executions: job.executions,
    error_class: error.class.name,
    error_message: error.message
  }

  message = "Job retry stopped: #{job.class.name}"

  log_structured_event(
    message: message,
    level: :error,
    attributes: attributes
  )
end