Module: JCW::Subscriber

Extended by:
Subscriber
Included in:
Subscriber
Defined in:
lib/jcw/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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jcw/subscriber.rb', line 15

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

  span = OpenTracing.scope_manager.active&.span
  return if span.blank?

  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.log_kv(message: name, context: JSON.dump(payload), duration: duration)
end

#subscribe_to_event!(event) ⇒ Object



9
10
11
12
13
# File 'lib/jcw/subscriber.rb', line 9

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