Module: Sashiko::Rails::TracedJob

Defined in:
lib/sashiko/rails.rb

Overview

Include in ActiveJob classes (typically ApplicationJob) to propagate the OTel trace context across the queue boundary. Works against any ActiveJob backend without backend-specific code: the carrier rides as an extra key on the job’s serialized hash, so it survives whatever backend the job lands in.

class ApplicationJob < ActiveJob::Base
  include Sashiko::Rails::TracedJob
end

On ‘serialize`, the current `Sashiko::Context.carrier` is attached. On `deserialize`, it’s pulled off into an ivar; an ‘around_perform` callback then attaches the context before invoking the job body. Spans emitted inside `perform` become children of the trace that enqueued the job.

Defined Under Namespace

Modules: Serialization

Constant Summary collapse

CARRIER_KEY =
"_sashiko_trace_carrier"

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sashiko/rails.rb', line 110

def self.included(base)
  unless defined?(::ActiveJob::Base) && base <= ::ActiveJob::Base
    raise "Sashiko::Rails::TracedJob must be included in an ActiveJob::Base subclass"
  end
  base.prepend(Serialization)
  base.around_perform do |_job, block|
    carrier = instance_variable_defined?(:@__sashiko_trace_carrier) ? @__sashiko_trace_carrier : nil
    if carrier && !carrier.empty?
      Sashiko::Context.attach(carrier) { block.call }
    else
      block.call
    end
  end
end