# 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 = common_attributes(event) payload = event.payload job = payload[:job] base.merge!({ "queue_name" => job.queue_name, "job_id" => job.job_id, "job_class" => job.class.name, "job_arguments" => job.arguments, "executions" => job.executions, "enqueued_at" => enqueued_at(job), "dequeued_at" => dequeued_at(job), "scheduled_at" => job.scheduled_at, "adapter" => payload[:adapter].class.name, "aborted" => payload[:aborted] }) case name when "enqueue", "enqueue_at", "perform" base when "enqueue_retry" base.merge({ "error" => payload[:error], "wait" => payload[:wait] }) when "retry_stopped", "discard" base.merge({ "error" => payload[:error] }) when "perform_start" nil end end private def enqueued_at(job) Time.parse(job.enqueued_at).to_f if job.enqueued_at end def dequeued_at(job) Time.parse(job.dequeued_at).to_f if job.respond_to?(:dequeued_at) && job.dequeued_at end end end end