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
Source
# File lib/coach/notifications.rb, line 26 def self.instance @instance ||= new end
Source
# File lib/coach/notifications.rb, line 61 def initialize @benchmarks = {} @subscriptions = [] end
Source
# File lib/coach/notifications.rb, line 17 def self.subscribe! instance.subscribe! end
Begin processing/emitting ‘request.coach’s
Source
# File lib/coach/notifications.rb, line 22 def self.unsubscribe! instance.unsubscribe! end
Cease to emit ‘request.coach’s
Public Instance Methods
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
Source
# File lib/coach/notifications.rb, line 46 def unsubscribe! return unless active? ActiveSupport::Notifications.unsubscribe(@subscriptions.pop) while @subscriptions.any? true end
Private Instance Methods
Source
# File lib/coach/notifications.rb, line 85 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
Receives a handler.finish event, with processed benchmark. Publishes to request.coach notification.
Source
# File lib/coach/notifications.rb, line 77 def log_handler_finish(event, start, finish) benchmark = @benchmarks.delete(event[:request].uuid) benchmark.complete(start, finish) broadcast(event, benchmark) end
Source
# File lib/coach/notifications.rb, line 70 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
Source
# File lib/coach/notifications.rb, line 66 def subscribe(event, &block) ActiveSupport::Notifications.subscribe("#{event}.coach", &block) end