lib/sentry/rails/active_job.rb in sentry-rails-5.1.0 vs lib/sentry/rails/active_job.rb in sentry-rails-5.1.1
- old
+ new
@@ -3,79 +3,83 @@
module ActiveJobExtensions
def perform_now
if !Sentry.initialized? || already_supported_by_sentry_integration?
super
else
- Sentry.with_scope do |scope|
- capture_and_reraise_with_sentry(scope) do
- super
- end
+ SentryReporter.record(self) do
+ super
end
end
end
- def capture_and_reraise_with_sentry(scope, &block)
- scope.set_transaction_name(self.class.name)
- transaction =
- if is_a?(::Sentry::SendEventJob)
- nil
- else
- Sentry.start_transaction(name: scope.transaction_name, op: "active_job")
- end
+ def already_supported_by_sentry_integration?
+ Sentry.configuration.rails.skippable_job_adapters.include?(self.class.queue_adapter.class.to_s)
+ end
- scope.set_span(transaction) if transaction
+ class SentryReporter
+ class << self
+ def record(job, &block)
+ Sentry.with_scope do |scope|
+ begin
+ scope.set_transaction_name(job.class.name)
+ transaction =
+ if job.is_a?(::Sentry::SendEventJob)
+ nil
+ else
+ Sentry.start_transaction(name: scope.transaction_name, op: "active_job")
+ end
- return_value = block.call
+ scope.set_span(transaction) if transaction
- finish_sentry_transaction(transaction, 200)
+ yield.tap do
+ finish_sentry_transaction(transaction, 200)
+ end
+ rescue Exception => e # rubocop:disable Lint/RescueException
+ finish_sentry_transaction(transaction, 500)
- return_value
- rescue Exception => e # rubocop:disable Lint/RescueException
- finish_sentry_transaction(transaction, 500)
+ Sentry::Rails.capture_exception(
+ e,
+ extra: sentry_context(job),
+ tags: {
+ job_id: job.job_id,
+ provider_job_id: job.provider_job_id
+ }
+ )
+ raise
+ end
+ end
+ end
- Sentry::Rails.capture_exception(
- e,
- extra: sentry_context,
- tags: {
- job_id: job_id,
- provider_job_id: provider_job_id
- }
- )
- raise e
- end
+ def finish_sentry_transaction(transaction, status)
+ return unless transaction
- def finish_sentry_transaction(transaction, status)
- return unless transaction
+ transaction.set_http_status(status)
+ transaction.finish
+ end
- transaction.set_http_status(status)
- transaction.finish
- end
+ 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
- def already_supported_by_sentry_integration?
- Sentry.configuration.rails.skippable_job_adapters.include?(self.class.queue_adapter.class.to_s)
- end
-
- def sentry_context
- {
- active_job: self.class.name,
- arguments: sentry_serialize_arguments(arguments),
- scheduled_at: scheduled_at,
- job_id: job_id,
- provider_job_id: provider_job_id,
- locale: locale
- }
- end
-
- def sentry_serialize_arguments(argument)
- case argument
- 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
+ def sentry_serialize_arguments(argument)
+ case argument
+ 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
end
end
end
end
end