2009-10-01 18:00:22 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2009-09-16 18:53:49 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
2009-10-15 17:51:51 -04:00
|
|
|
# Notifications provides an instrumentation API for Ruby. To instrument an
|
|
|
|
# action in Ruby you just need to do:
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 18:53:49 -04:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# You can consume those events and the information they provide by registering
|
2010-02-15 09:44:30 -05:00
|
|
|
# a log subscriber. For instance, let's store all instrumented events in an array:
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# @events = []
|
|
|
|
#
|
2009-10-28 04:58:33 -04:00
|
|
|
# ActiveSupport::Notifications.subscribe do |*args|
|
|
|
|
# @events << ActiveSupport::Notifications::Event.new(*args)
|
2009-10-06 08:42:42 -04:00
|
|
|
# end
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 18:53:49 -04:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 08:42:42 -04:00
|
|
|
# event = @events.first
|
2010-07-29 20:30:04 -04:00
|
|
|
# event.name # => :render
|
|
|
|
# event.duration # => 10 (in milliseconds)
|
|
|
|
# event.payload # => { :extra => :information }
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# When subscribing to Notifications, you can pass a pattern, to only consume
|
2009-10-06 08:42:42 -04:00
|
|
|
# events that match the pattern:
|
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# ActiveSupport::Notifications.subscribe(/render/) do |event|
|
2009-10-06 08:42:42 -04:00
|
|
|
# @render_events << event
|
|
|
|
# end
|
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
# Notifications ships with a queue implementation that consumes and publish events
|
2010-02-15 09:44:30 -05:00
|
|
|
# to log subscribers in a thread. You can use any queue implementation you want.
|
2009-09-16 18:53:49 -04:00
|
|
|
#
|
2009-10-15 17:51:51 -04:00
|
|
|
module Notifications
|
2009-11-28 15:49:07 -05:00
|
|
|
autoload :Instrumenter, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Event, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Fanout, 'active_support/notifications/fanout'
|
2009-09-30 07:59:15 -04:00
|
|
|
|
2010-07-21 19:29:26 -04:00
|
|
|
@instrumenters = Hash.new { |h,k| h[k] = notifier.listening?(k) }
|
|
|
|
|
2009-10-01 18:00:22 -04:00
|
|
|
class << self
|
2011-02-09 17:02:38 -05:00
|
|
|
attr_accessor :notifier
|
2011-02-09 16:46:47 -05:00
|
|
|
|
|
|
|
def publish(name, *args)
|
|
|
|
notifier.publish(name, *args)
|
|
|
|
end
|
2010-07-21 19:29:26 -04:00
|
|
|
|
2010-07-25 14:46:42 -04:00
|
|
|
def instrument(name, payload = {})
|
2010-07-21 19:29:26 -04:00
|
|
|
if @instrumenters[name]
|
2010-07-25 14:46:42 -04:00
|
|
|
instrumenter.instrument(name, payload) { yield payload if block_given? }
|
2010-07-21 19:29:26 -04:00
|
|
|
else
|
2010-07-25 14:46:42 -04:00
|
|
|
yield payload if block_given?
|
2010-07-21 19:29:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe(*args, &block)
|
|
|
|
notifier.subscribe(*args, &block).tap do
|
|
|
|
@instrumenters.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-09 17:33:56 -05:00
|
|
|
def unsubscribe(args)
|
|
|
|
notifier.unsubscribe(args)
|
2010-07-21 19:29:26 -04:00
|
|
|
@instrumenters.clear
|
|
|
|
end
|
2009-10-01 18:00:22 -04:00
|
|
|
|
2010-01-06 16:23:29 -05:00
|
|
|
def instrumenter
|
|
|
|
Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
|
|
|
|
end
|
2009-10-01 18:00:22 -04:00
|
|
|
end
|
2011-02-09 17:02:38 -05:00
|
|
|
|
|
|
|
self.notifier = Fanout.new
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
end
|