Class: Coach::Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/coach/notifications.rb

Overview

By default, Coach will trigger ActiveSupport::Notifications at specific times in a request lifecycle.

Notifications is used to coordinate the listening and aggregation of these middleware notifications, while RequestEvent processes the published data.

Once a request has completed, Notifications will emit a ‘request.coach’ event with aggregated request data.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



26
27
28
# File 'lib/coach/notifications.rb', line 26

def self.instance
  @instance ||= new
end

.subscribe!Object

Begin processing/emitting ‘request.coach’s



17
18
19
# File 'lib/coach/notifications.rb', line 17

def self.subscribe!
  instance.subscribe!
end

.unsubscribe!Object

Cease to emit ‘request.coach’s



22
23
24
# File 'lib/coach/notifications.rb', line 22

def self.unsubscribe!
  instance.unsubscribe!
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/coach/notifications.rb', line 55

def active?
  @subscriptions.any?
end

#subscribe!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coach/notifications.rb', line 30

def subscribe!
  return if active?

  @subscriptions << subscribe("start_handler") do |_, event|
    @benchmarks[event[:request].uuid] = RequestBenchmark.new(event[:middleware])
  end

  @subscriptions << subscribe("finish_middleware") do |_name, start, finish, _, event|
    log_middleware_finish(event, start, finish)
  end

  @subscriptions << subscribe("finish_handler") do |_name, start, finish, _, event|
    log_handler_finish(event, start, finish)
  end
end

#unsubscribe!Object



46
47
48
49
50
51
52
53
# File 'lib/coach/notifications.rb', line 46

def unsubscribe!
  return unless active?

  while @subscriptions.any?
    ActiveSupport::Notifications.unsubscribe(@subscriptions.pop)
  end
  true
end