Module: Otel::ActiveSupportSubscriber::Subscriber

Extended by:
Subscriber
Included in:
Subscriber
Defined in:
lib/otel/active_support_subscriber/subscriber.rb

Constant Summary collapse

IGNORED_PAYLOAD_KEYS =
i[request response headers exception exception_object].freeze

Instance Method Summary collapse

Instance Method Details

#add(name, payload, duration) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/otel/active_support_subscriber/subscriber.rb', line 17

def add(name, payload, duration)
  # skip Rails internal events
  return if name.start_with?("!")

  span = OpenTelemetry::Trace.current_span
  return unless span.context.valid?

  if payload.is_a?(Hash)
    # we should only mutate the copy of the payload
    payload = payload.dup
    IGNORED_PAYLOAD_KEYS.each { |key| payload.delete(key) if payload.key?(key) }
  end

  duration = format("%0.3fms", duration * 1000)
  span.add_event(
    name,
    attributes: {
      "context" => JSON.dump(payload),
      "duration" => duration,
    },
  )
end

#subscribe_to_event!(event) ⇒ Object



10
11
12
13
14
15
# File 'lib/otel/active_support_subscriber/subscriber.rb', line 10

def subscribe_to_event!(event)
  ActiveSupport::Notifications
    .monotonic_subscribe(event) do |name, start, finish, _id, payload|
    add(name, payload, finish - start)
  end
end