# frozen_string_literal: true module PlainApm module Extensions module TraceId module ActiveJob attr_accessor :trace_id def initialize(*arguments) super(*arguments) # Either from request headers / a previous job, or a new trace. @trace_id = PlainApm::Extensions::TraceId.current || SecureRandom.uuid end def serialize super.update("trace_id" => trace_id) end def deserialize(job) PlainApm::Extensions::TraceId.current = job["trace_id"] super(job) end end ## # Allow tracing request ID through jobs ActiveSupport.on_load(:active_job, run_once: true) do |klass| klass.prepend(ActiveJob) end end end end