2009-10-01 22:00:22 +00:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2009-09-16 22:53:49 +00:00
|
|
|
|
|
|
|
module ActiveSupport
|
2009-10-15 21:51:51 +00:00
|
|
|
# Notifications provides an instrumentation API for Ruby. To instrument an
|
|
|
|
# action in Ruby you just need to do:
|
2009-09-16 22:53:49 +00:00
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 22:53:49 +00:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 12:42:42 +00:00
|
|
|
# You can consume those events and the information they provide by registering
|
2010-02-15 14:44:30 +00:00
|
|
|
# a log subscriber. For instance, let's store all instrumented events in an array:
|
2009-09-16 22:53:49 +00:00
|
|
|
#
|
2009-10-06 12:42:42 +00:00
|
|
|
# @events = []
|
|
|
|
#
|
2009-10-28 08:58:33 +00:00
|
|
|
# ActiveSupport::Notifications.subscribe do |*args|
|
|
|
|
# @events << ActiveSupport::Notifications::Event.new(*args)
|
2009-10-06 12:42:42 +00:00
|
|
|
# end
|
2009-09-16 22:53:49 +00:00
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
# ActiveSupport::Notifications.instrument(:render, :extra => :information) do
|
2009-09-16 22:53:49 +00:00
|
|
|
# render :text => "Foo"
|
|
|
|
# end
|
|
|
|
#
|
2009-10-06 12:42:42 +00:00
|
|
|
# event = @events.first
|
2009-09-16 22:53:49 +00:00
|
|
|
# event.name #=> :render
|
|
|
|
# event.duration #=> 10 (in miliseconds)
|
|
|
|
# event.result #=> "Foo"
|
|
|
|
# event.payload #=> { :extra => :information }
|
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
# When subscribing to Notifications, you can pass a pattern, to only consume
|
2009-10-06 12:42:42 +00:00
|
|
|
# events that match the pattern:
|
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
# ActiveSupport::Notifications.subscribe(/render/) do |event|
|
2009-10-06 12:42:42 +00:00
|
|
|
# @render_events << event
|
|
|
|
# end
|
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
# Notifications ships with a queue implementation that consumes and publish events
|
2010-02-15 14:44:30 +00:00
|
|
|
# to log subscribers in a thread. You can use any queue implementation you want.
|
2009-09-16 22:53:49 +00:00
|
|
|
#
|
2009-10-15 21:51:51 +00:00
|
|
|
module Notifications
|
2009-11-28 20:49:07 +00:00
|
|
|
autoload :Instrumenter, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Event, 'active_support/notifications/instrumenter'
|
|
|
|
autoload :Fanout, 'active_support/notifications/fanout'
|
2009-09-30 11:59:15 +00:00
|
|
|
|
2009-10-01 22:00:22 +00:00
|
|
|
class << self
|
2009-11-28 20:49:07 +00:00
|
|
|
attr_writer :notifier
|
2010-03-02 01:33:59 +00:00
|
|
|
delegate :publish, :subscribe, :unsubscribe, :to => :notifier
|
2010-02-04 09:39:55 +00:00
|
|
|
delegate :instrument, :to => :instrumenter
|
2009-10-01 22:00:22 +00:00
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
def notifier
|
|
|
|
@notifier ||= Notifier.new
|
2009-10-01 22:00:22 +00:00
|
|
|
end
|
2010-01-06 21:23:29 +00:00
|
|
|
|
|
|
|
def instrumenter
|
|
|
|
Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
|
|
|
|
end
|
2009-10-01 22:00:22 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
class Notifier
|
|
|
|
def initialize(queue = Fanout.new)
|
2009-10-01 22:00:22 +00:00
|
|
|
@queue = queue
|
|
|
|
end
|
|
|
|
|
2009-10-27 07:07:21 +00:00
|
|
|
def publish(*args)
|
|
|
|
@queue.publish(*args)
|
2009-10-01 22:00:22 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
def subscribe(pattern = nil, &block)
|
|
|
|
@queue.bind(pattern).subscribe(&block)
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-02 00:01:08 +00:00
|
|
|
def unsubscribe(subscriber)
|
|
|
|
@queue.unsubscribe(subscriber)
|
|
|
|
end
|
|
|
|
|
2009-11-25 03:35:01 +00:00
|
|
|
def wait
|
2009-11-28 20:49:07 +00:00
|
|
|
@queue.wait
|
2009-09-30 11:59:15 +00:00
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|