class Coach::Notifications

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.

Public Class Methods

instance() click to toggle source
# File lib/coach/notifications.rb, line 26
def self.instance
  @instance ||= new
end
new() click to toggle source
# File lib/coach/notifications.rb, line 63
def initialize
  @benchmarks = {}
  @subscriptions = []
end
subscribe!() click to toggle source

Begin processing/emitting ‘request.coach’s

# File lib/coach/notifications.rb, line 17
def self.subscribe!
  instance.subscribe!
end
unsubscribe!() click to toggle source

Cease to emit ‘request.coach’s

# File lib/coach/notifications.rb, line 22
def self.unsubscribe!
  instance.unsubscribe!
end

Public Instance Methods

active?() click to toggle source
# File lib/coach/notifications.rb, line 55
def active?
  @subscriptions.any?
end
subscribe!() click to toggle source
# 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!() click to toggle source
# File lib/coach/notifications.rb, line 46
def unsubscribe!
  return unless active?

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

Private Instance Methods

broadcast(event, benchmark) click to toggle source

Receives a handler.finish event, with processed benchmark. Publishes to request.coach notification.

# File lib/coach/notifications.rb, line 87
def broadcast(event, benchmark)
  serialized = RequestSerializer.new(event[:request]).serialize.
    merge(benchmark.stats).
    merge(event.slice(:response, :metadata))
  ActiveSupport::Notifications.publish("request.coach", serialized)
end
log_handler_finish(event, start, finish) click to toggle source
# File lib/coach/notifications.rb, line 79
def log_handler_finish(event, start, finish)
  benchmark = @benchmarks.delete(event[:request].uuid)
  benchmark.complete(start, finish)
  broadcast(event, benchmark)
end
log_middleware_finish(event, start, finish) click to toggle source
# File lib/coach/notifications.rb, line 72
def log_middleware_finish(event, start, finish)
  benchmark_for_request = @benchmarks[event[:request].uuid]
  return unless benchmark_for_request.present?

  benchmark_for_request.notify(event[:middleware], start, finish)
end
subscribe(event, &block) click to toggle source
# File lib/coach/notifications.rb, line 68
def subscribe(event, &block)
  ActiveSupport::Notifications.subscribe("#{event}.coach", &block)
end