Module: NatsWork::Instrumentation
Defined Under Namespace
Modules: ActiveSupportCompatibility
Classes: Subscription
Instance Method Summary
collapse
Instance Method Details
#clear_subscribers! ⇒ Object
69
70
71
|
# File 'lib/natswork/instrumentation.rb', line 69
def clear_subscribers!
@subscribers.clear
end
|
#disable! ⇒ Object
61
62
63
|
# File 'lib/natswork/instrumentation.rb', line 61
def disable!
@enabled = false
end
|
#enable! ⇒ Object
57
58
59
|
# File 'lib/natswork/instrumentation.rb', line 57
def enable!
@enabled = true
end
|
#enabled? ⇒ Boolean
65
66
67
|
# File 'lib/natswork/instrumentation.rb', line 65
def enabled?
@enabled
end
|
#instrument(event, payload = {}) ⇒ Object
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
|
# File 'lib/natswork/instrumentation.rb', line 25
def instrument(event, payload = {})
return yield if block_given? && !@enabled
event_name = event.to_s
payload = payload.dup
payload[:event] = event_name
payload[:started_at] = Time.now
begin
if block_given?
result = yield
payload[:finished_at] = Time.now
payload[:duration] = (payload[:finished_at] - payload[:started_at]) * 1000
payload[:result] = result
notify_subscribers(event_name, payload)
result
else
notify_subscribers(event_name, payload)
end
rescue StandardError => e
payload[:finished_at] = Time.now
payload[:duration] = (payload[:finished_at] - payload[:started_at]) * 1000
payload[:error] = e
payload[:exception] = [e.class.name, e.message]
notify_subscribers("#{event_name}.error", payload)
notify_subscribers(event_name, payload)
raise
end
end
|
#subscribe(event, &block) ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/natswork/instrumentation.rb', line 13
def subscribe(event, &block)
return unless block_given?
subscription = Subscription.new(event, block)
@subscribers[event.to_s] << subscription
subscription
end
|
#unsubscribe(subscription) ⇒ Object
21
22
23
|
# File 'lib/natswork/instrumentation.rb', line 21
def unsubscribe(subscription)
@subscribers.each_value { |subs| subs.delete(subscription) }
end
|