Class: OpenTelemetry::Instrumentation::Sidekiq::Middlewares::Server::TracerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb

Overview

TracerMiddleware propagates context and instruments Sidekiq requests by way of its middleware system

Instance Method Summary collapse

Instance Method Details

#call(_worker, msg, _queue) ⇒ Object

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



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/sidekiq/middlewares/server/tracer_middleware.rb', line 15

def call(_worker, msg, _queue) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  attributes = {
    'messaging.system' => 'sidekiq',
    'messaging.sidekiq.job_class' => msg['wrapped']&.to_s || msg['class'],
    'messaging.message_id' => msg['jid'],
    'messaging.destination' => msg['queue'],
    'messaging.destination_kind' => 'queue',
    'messaging.operation' => 'process'
  }
  attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]

  span_name = case instrumentation_config[:span_naming]
              when :job_class then "#{msg['wrapped']&.to_s || msg['class']} process"
              else "#{msg['queue']} process"
              end

  extracted_context = OpenTelemetry.propagation.extract(msg)
  OpenTelemetry::Context.with_current(extracted_context) do
    if instrumentation_config[:propagation_style] == :child
      tracer.in_span(span_name, attributes: attributes, kind: :consumer) do |span|
        span.add_event('created_at', timestamp: msg['created_at'])
        span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
        yield
      end
    else
      links = []
      span_context = OpenTelemetry::Trace.current_span(extracted_context).context
      links << OpenTelemetry::Trace::Link.new(span_context) if instrumentation_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
        span.add_event('created_at', timestamp: msg['created_at'])
        span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
        yield
      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