diff options
Diffstat (limited to 'guides/source/active_support_instrumentation.md')
-rw-r--r-- | guides/source/active_support_instrumentation.md | 150 |
1 files changed, 76 insertions, 74 deletions
diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md index 6c06cfcc4b..969596f470 100644 --- a/guides/source/active_support_instrumentation.md +++ b/guides/source/active_support_instrumentation.md @@ -3,19 +3,21 @@ Active Support Instrumentation Active Support is a part of core Rails that provides Ruby language extensions, utilities and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired. -In this guide, you will learn how to use the instrumentation API inside of ActiveSupport to measure events inside of Rails and other Ruby code. We cover: +In this guide, you will learn how to use the instrumentation API inside of Active Support to measure events inside of Rails and other Ruby code. -* What instrumentation can provide -* The hooks inside the Rails framework for instrumentation -* Adding a subscriber to a hook -* Building a custom instrumentation implementation +After reading this guide, you will know: + +* What instrumentation can provide. +* The hooks inside the Rails framework for instrumentation. +* Adding a subscriber to a hook. +* Building a custom instrumentation implementation. -------------------------------------------------------------------------------- Introduction to instrumentation ------------------------------- -The instrumentation API provided by ActiveSupport allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework, as described below in <TODO: link to section detailing each hook point>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code. +The instrumentation API provided by Active Support allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework, as described below in <TODO: link to section detailing each hook point>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code. For example, there is a hook provided within Active Record that is called every time Active Record uses an SQL query on a database. This hook could be **subscribed** to, and used to track the number of queries during a certain action. There's another hook around the processing of an action of a controller. This could be used, for instance, to track how long a specific action has taken. @@ -26,8 +28,8 @@ Rails framework hooks Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below. -ActionController ----------------- +Action Controller +----------------- ### write_fragment.action_controller @@ -37,7 +39,7 @@ ActionController ```ruby { - :key => 'posts/1-dasboard-view' + key: 'posts/1-dashboard-view' } ``` @@ -49,7 +51,7 @@ ActionController ```ruby { - :key => 'posts/1-dasboard-view' + key: 'posts/1-dashboard-view' } ``` @@ -61,7 +63,7 @@ ActionController ```ruby { - :key => 'posts/1-dasboard-view' + key: 'posts/1-dashboard-view' } ``` @@ -73,7 +75,7 @@ ActionController ```ruby { - :key => 'posts/1-dasboard-view' + key: 'posts/1-dashboard-view' } ``` @@ -85,7 +87,7 @@ ActionController ```ruby { - :path => '/users/1' + path: '/users/1' } ``` @@ -97,7 +99,7 @@ ActionController ```ruby { - :path => '/users/1' + path: '/users/1' } ``` @@ -114,12 +116,12 @@ ActionController ```ruby { - :controller => "PostsController", - :action => "new", - :params => { "action" => "new", "controller" => "posts" }, - :format => :html, - :method => "GET", - :path => "/posts/new" + controller: "PostsController", + action: "new", + params: { "action" => "new", "controller" => "posts" }, + format: :html, + method: "GET", + path: "/posts/new" } ``` @@ -137,15 +139,15 @@ ActionController ```ruby { - :controller => "PostsController", - :action => "index", - :params => {"action" => "index", "controller" => "posts"}, - :format => :html, - :method => "GET", - :path => "/posts", - :status => 200, - :view_runtime => 46.848, - :db_runtime => 0.157 + controller: "PostsController", + action: "index", + params: {"action" => "index", "controller" => "posts"}, + format: :html, + method: "GET", + path: "/posts", + status: 200, + view_runtime: 46.848, + db_runtime: 0.157 } ``` @@ -170,8 +172,8 @@ INFO. Additional keys may be added by the caller. ```ruby { - :status => 302, - :location => "http://localhost:3000/posts/new" + status: 302, + location: "http://localhost:3000/posts/new" } ``` @@ -183,12 +185,12 @@ INFO. Additional keys may be added by the caller. ```ruby { - :filter => ":halting_filter" + filter: ":halting_filter" } ``` -ActionView ----------- +Action View +----------- ### render_template.action_view @@ -199,8 +201,8 @@ ActionView ```ruby { - :identifier => "/Users/adam/projects/notifications/app/views/posts/index.html.erb", - :layout => "layouts/application" + identifier: "/Users/adam/projects/notifications/app/views/posts/index.html.erb", + layout: "layouts/application" } ``` @@ -212,11 +214,11 @@ ActionView ```ruby { - :identifier => "/Users/adam/projects/notifications/app/views/posts/_form.html.erb", + identifier: "/Users/adam/projects/notifications/app/views/posts/_form.html.erb", } ``` -ActiveRecord +Active Record ------------ ### sql.active_record @@ -231,10 +233,10 @@ INFO. The adapters will add their own data as well. ```ruby { - :sql => "SELECT \"posts\".* FROM \"posts\" ", - :name => "Post Load", - :connection_id => 70307250813140, - :binds => [] + sql: "SELECT \"posts\".* FROM \"posts\" ", + name: "Post Load", + connection_id: 70307250813140, + binds: [] } ``` @@ -246,8 +248,8 @@ INFO. The adapters will add their own data as well. | `:name` | Record's class | | `:connection_id` | `self.object_id` | -ActionMailer ------------- +Action Mailer +------------- ### receive.action_mailer @@ -265,13 +267,13 @@ ActionMailer ```ruby { - :mailer => "Notification", - :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", - :subject => "Rails Guides", - :to => ["users@rails.com", "ddh@rails.com"], - :from => ["me@rails.com"], - :date => Sat, 10 Mar 2012 14:18:09 +0100, - :mail=> "..." # ommitted for beverity + mailer: "Notification", + message_id: "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + subject: "Rails Guides", + to: ["users@rails.com", "ddh@rails.com"], + from: ["me@rails.com"], + date: Sat, 10 Mar 2012 14:18:09 +0100, + mail: "..." # omitted for brevity } ``` @@ -291,13 +293,13 @@ ActionMailer ```ruby { - :mailer => "Notification", - :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", - :subject => "Rails Guides", - :to => ["users@rails.com", "ddh@rails.com"], - :from => ["me@rails.com"], - :date => Sat, 10 Mar 2012 14:18:09 +0100, - :mail=> "..." # ommitted for beverity + mailer: "Notification", + message_id: "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + subject: "Rails Guides", + to: ["users@rails.com", "ddh@rails.com"], + from: ["me@rails.com"], + date: Sat, 10 Mar 2012 14:18:09 +0100, + mail: "..." # omitted for brevity } ``` @@ -312,8 +314,8 @@ ActiveResource | `:request_uri` | Complete URI | | `:result` | HTTP response object | -ActiveSupport -------------- +Active Support +-------------- ### cache_read.active_support @@ -335,7 +337,7 @@ INFO. Options passed to fetch will be merged with the payload when writing to th ```ruby { - :key => 'name-of-complicated-computation' + key: 'name-of-complicated-computation' } ``` @@ -352,7 +354,7 @@ INFO. Options passed to fetch will be merged with the payload. ```ruby { - :key => 'name-of-complicated-computation' + key: 'name-of-complicated-computation' } ``` @@ -366,7 +368,7 @@ INFO. Cache stores my add their own keys ```ruby { - :key => 'name-of-complicated-computation' + key: 'name-of-complicated-computation' } ``` @@ -378,7 +380,7 @@ INFO. Cache stores my add their own keys ```ruby { - :key => 'name-of-complicated-computation' + key: 'name-of-complicated-computation' } ``` @@ -390,7 +392,7 @@ INFO. Cache stores my add their own keys ```ruby { - :key => 'name-of-complicated-computation' + key: 'name-of-complicated-computation' } ``` @@ -426,29 +428,29 @@ end ``` Defining all those block arguments each time can be tedious. You can easily create an `ActiveSupport::Notifications::Event` -from block args like this: +from block arguments like this: ```ruby ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| - event = ActiveSupport::Notification::Event.new args + event = ActiveSupport::Notifications::Event.new *args event.name # => "process_action.action_controller" event.duration # => 10 (in milliseconds) - event.payload # => { :extra => :information } + 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. +Most times you only care about the data itself. Here is a shortcut to just get the data. ```ruby ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| data = args.extract_options! - data # { :extra => :information } + data # { extra: :information } ``` -You may also subscribe to events matching a regular expresssion. This enables you to subscribe to +You may also subscribe to events matching a regular expression. This enables you to subscribe to multiple events at once. Here's you could subscribe to everything from `ActionController`. ```ruby @@ -463,12 +465,12 @@ 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. +as well as the unique ID. All data passed into the `instrument` call will make it into the payload. Here's an example: ```ruby -ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do +ActiveSupport::Notifications.instrument "my.custom.event", this: :data do # do your custom stuff here end ``` @@ -477,7 +479,7 @@ Now you can listen to this event with: ```ruby ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data| - puts data.inspect # { :this => :data } + puts data.inspect # {:this=>:data} end ``` |