Module: OpenTelemetry::Instrumentation::Resque::Patches::ResqueJob

Defined in:
lib/opentelemetry/instrumentation/resque/patches/resque_job.rb

Overview

Module to prepend to Resque::Job for instrumentation

Instance Method Summary collapse

Instance Method Details

#performObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/instrumentation/resque/patches/resque_job.rb', line 13

def perform # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  job_args = args || []

  # Check if the job is being wrapped by ActiveJob
  # before retrieving the job class name
  job_class = if payload_class_name == 'ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper' && job_args[0]&.is_a?(Hash)
                job_args[0]['job_class']
              else
                payload_class_name
              end

  attributes = {
    'messaging.system' => 'resque',
    'messaging.destination' => queue.to_s,
    'messaging.destination_kind' => 'queue',
    'messaging.resque.job_class' => job_class
  }

  span_name = case config[:span_naming]
              when :job_class then "#{job_class} process"
              else "#{queue} process"
              end

  extracted_context = OpenTelemetry.propagation.extract(@payload)

  OpenTelemetry::Context.with_current(extracted_context) do
    if config[:propagation_style] == :child
      tracer.in_span(span_name, attributes: attributes, kind: :consumer) { super }
    else
      links = []
      span_context = OpenTelemetry::Trace.current_span(extracted_context).context
      links << OpenTelemetry::Trace::Link.new(span_context) if config[:propagation_style] == :link && span_context.valid?
      span = tracer.start_root_span(span_name, attributes: attributes, links: links, kind: :consumer)
      OpenTelemetry::Trace.with_span(span) do
        super
      rescue Exception => e # rubocop:disable Lint/RescueException
        span.record_exception(e)
        span.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{e.class}")
        raise e
      ensure
        span.finish
      end
    end
  end
end