class OpenTelemetry::Instrumentation::DelayedJob::Plugins::TracerPlugin

Delayed Job plugin that instruments invoke_job and other hooks

Public Class Methods

instrument_enqueue(job) { |job| ... } click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 16
def instrument_enqueue(job, &block)
  return block.call(job) unless enabled?

  attributes = build_attributes(job)
  attributes['messaging.operation'] = 'send'
  tracer.in_span("#{job_queue(job)} send", attributes: attributes, kind: :producer) do |span|
    yield job
    span.set_attribute('messaging.message_id', job.id.to_s)
    add_events(span, job)
  end
end
instrument_invoke(job) { |job| ... } click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 28
def instrument_invoke(job, &block)
  return block.call(job) unless enabled?

  attributes = build_attributes(job)
  attributes['messaging.delayed_job.attempts'] = job.attempts if job.attempts
  attributes['messaging.delayed_job.locked_by'] = job.locked_by if job.locked_by
  attributes['messaging.operation'] = 'process'
  attributes['messaging.message_id'] = job.id.to_s
  tracer.in_span("#{job_queue(job)} process", attributes: attributes, kind: :consumer) do |span|
    add_events(span, job)
    yield job
  end
end

Protected Class Methods

add_events(span, job) click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 54
def add_events(span, job)
  span.add_event('created_at', timestamp: job.created_at)
  span.add_event('run_at', timestamp: job.run_at) if job.run_at
  span.add_event('locked_at', timestamp: job.locked_at) if job.locked_at
end
build_attributes(job) click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 44
def build_attributes(job)
  {
    'messaging.system' => 'delayed_job',
    'messaging.destination' => job_queue(job),
    'messaging.destination_kind' => 'queue',
    'messaging.delayed_job.name' => job_name(job),
    'messaging.delayed_job.priority' => job.priority
  }
end
enabled?() click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 60
def enabled?
  DelayedJob::Instrumentation.instance.enabled?
end
job_name(job) click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 68
def job_name(job)
  # If Delayed Job is used via ActiveJob then get the job name from the payload
  if job.payload_object.respond_to?(:job_data)
    job.payload_object.job_data['job_class']
  else
    job.name
  end
end
job_queue(job) click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 77
def job_queue(job)
  job.queue || 'default'
end
tracer() click to toggle source
# File lib/opentelemetry/instrumentation/delayed_job/plugins/tracer_plugin.rb, line 64
def tracer
  DelayedJob::Instrumentation.instance.tracer
end