# frozen_string_literal: true module PlainApm module Hooks class ActiveJob < ActiveSupportSubscriber NOTIFICATION_PATTERN = /\A[^!]\w+\.active_job\Z/.freeze private def notification_pattern NOTIFICATION_PATTERN end def payload(event) name, base = attributes_from_notification(event) payload = event.payload job = payload[:job] base.tap do |o| o[:queue_name] = job.queue_name o[:job_id] = job.job_id o[:job_class] = job.class.name o[:job_arguments] = job.arguments o[:executions] = job.executions o[:enqueued_at] = enqueued_at(job) o[:dequeued_at] = dequeued_at(job) o[:scheduled_at] = job.scheduled_at o[:adapter] = payload[:adapter].class.name o[:aborted] = payload[:aborted] end case name when "enqueue", "enqueue_at", "perform" base when "enqueue_retry" base.tap do |o| o[:error] = payload[:error] o[:wait] = payload[:wait] end when "retry_stopped", "discard" base.tap do |o| o[:error] = payload[:error] end when "perform_start" nil else nil end end private def enqueued_at(job) if job.enqueued_at if job.enqueued_at.is_a?(Time) job.enqueued_at.iso8601(9) else Time.parse(job.enqueued_at).iso8601(9) end end end def dequeued_at(job) if job.respond_to?(:dequeued_at) && job.dequeued_at Time.parse(job.dequeued_at).iso8601(9) end end end end end