From 18e1b5b8c5d7c5fd4a73a9f896f59e1ab03781bf Mon Sep 17 00:00:00 2001 From: adman65 Date: Thu, 15 Mar 2012 10:27:04 +0100 Subject: [PATCH] [ci skip] Add examples of subscribing & creating ActiveSupport::Notifications --- .../active_support_instrumentation.textile | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/active_support_instrumentation.textile b/railties/guides/source/active_support_instrumentation.textile index 18500949e0..dbf701acee 100644 --- a/railties/guides/source/active_support_instrumentation.textile +++ b/railties/guides/source/active_support_instrumentation.textile @@ -361,7 +361,6 @@ h4. cache_exist?.active_support } - h3. Rails h4. deprecation.rails @@ -372,5 +371,78 @@ h4. deprecation.rails h3. Subscribing to an event +Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to +listen to any notification. + +The block receives the following arguments: + +# The name of the event +# Time when is started +# Time when it finished +# An unique ID for this event +# The payload (described in previous sections) + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |name, started, finished, unique_id, data| + # your own custom stuff + Rails.logger.info "#{name} Received!" +end + + +Defining all those block arguments each time can be tedious. You can easily create an +ActiveSupport::Notifications::Event+ +from block args like this: + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| + event = ActiveSupport::Notification::Event.new args + + event.name # => "process_action.action_controller" + event.duration # => 10 (in milliseconds) + event.payload # => { :extra => :information } + + Rails.logger.info "#{event} Received!" +end + + +Most times you only care about the data itself. Here is a shortuct to just get the data. + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| + data = args.extract_options! + data # { :extra => :information } + + +You may also subscribe to events matching a regular expresssion. This enables you to subscribe to +multiple events at once. Here's you could subscribe to everything from +ActionController+. + + +ActiveSupport::Notifications.subscribe /action_controller/ do |*args| + # inspect all ActionController events +end + + h3. Creating custom events +Adding your own events is easy as well. +ActiveSupport::Notifications+ will take care of +all the heavy lifting for you. Simply call +instrument+ with a +name+, +payload+ and a block. +The notification will be sent after the block returns. +ActiveSupport+ will generate the start and end times +as well as the unique ID. All data passed into the +insturment+ call will make it into the payload. + +Here's an example: + + +ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do + # do your custom stuff here +end + + +Now you can listen to this event with: + + +ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data| + puts data.inspect # { :this => :data } +end + + +You should follow Rails conventions when defining your own events. The format is: +event.library+. +If you application is sending Tweets, you should create an event named +tweet.twitter+.