Class: OpenTelemetry::Instrumentation::ActionView::Fanout

Inherits:
ActiveSupport::Notifications::Fanout
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/action_view/fanout.rb

Overview

This is a replacement for the default Fanout notifications queue, which adds special handling around returned context from the SpanSubscriber notification handlers. Used together, it allows us to trace arbitrary ActiveSupport::Notifications safely.

Instance Method Summary collapse

Constructor Details

#initialize(notifier = ::ActiveSupport::Notifications::Fanout.new) ⇒ Fanout

Returns a new instance of Fanout.



16
17
18
# File 'lib/opentelemetry/instrumentation/action_view/fanout.rb', line 16

def initialize(notifier = ::ActiveSupport::Notifications::Fanout.new)
  super(notifier)
end

Instance Method Details

#finish(name, id, payload, listeners = listeners_for(name)) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/opentelemetry/instrumentation/action_view/fanout.rb', line 32

def finish(name, id, payload, listeners = listeners_for(name))
  listeners.each do |(s, arr)|
    span, token = arr
    if span.is_a?(OpenTelemetry::Trace::Span) && token
      s.finish(
        name,
        id,
        payload.merge(
          __opentelemetry_span: span,
          __opentelemetry_ctx_token: token
        )
      )
    else
      s.finish(name, id, payload)
    end
  end
end

#listeners_for(name) ⇒ Object



50
51
52
53
54
55
# File 'lib/opentelemetry/instrumentation/action_view/fanout.rb', line 50

def listeners_for(name)
  listeners = super
  listeners.sort_by do |l|
    l.instance_variable_get(:@delegate).is_a?(SpanSubscriber) ? -1 : 1
  end
end

#start(name, id, payload) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/opentelemetry/instrumentation/action_view/fanout.rb', line 20

def start(name, id, payload)
  listeners_for(name).map do |s|
    result = [s]
    state = s.start(name, id, payload)
    if state.is_a?(Array) && state[0].is_a?(OpenTelemetry::Trace::Span) && state[1] # rubocop:disable Style/IfUnlessModifier
      result << state
    end

    result
  end
end