Module: ChatSDK::Instrumentation

Defined in:
lib/chat_sdk/instrumentation.rb

Class Method Summary collapse

Class Method Details

.instrument(event_name, payload = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chat_sdk/instrumentation.rb', line 18

def instrument(event_name, payload = {})
  listeners = @mutex.synchronize { @subscribers[event_name]&.dup }
  unless listeners&.any?
    return yield if block_given?

    return
  end

  start = monotonic_now
  result = yield if block_given?
  payload[:duration] = monotonic_now - start
  fire(listeners, event_name, payload)
  result
rescue => e
  payload[:duration] = monotonic_now - start if start
  payload[:error] = e
  fire(listeners, event_name, payload) if listeners&.any?
  raise
end

.reset!Object



38
39
40
# File 'lib/chat_sdk/instrumentation.rb', line 38

def reset!
  @mutex.synchronize { @subscribers.clear }
end

.subscribe(event_name, &block) ⇒ Object



9
10
11
12
# File 'lib/chat_sdk/instrumentation.rb', line 9

def subscribe(event_name, &block)
  @mutex.synchronize { @subscribers[event_name] << block }
  block
end

.unsubscribe(event_name, block) ⇒ Object



14
15
16
# File 'lib/chat_sdk/instrumentation.rb', line 14

def unsubscribe(event_name, block)
  @mutex.synchronize { @subscribers[event_name].delete(block) }
end