Created
August 9, 2015 17:30
-
-
Save SafeAF/8d5d87ed61e06eb20dd4 to your computer and use it in GitHub Desktop.
active support instrumentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### start_processing.action_controller | |
| Key | Value | | |
| ------------- | --------------------------------------------------------- | | |
| `:controller` | The controller name | | |
| `:action` | The action | | |
| `:params` | Hash of request parameters without any filtered parameter | | |
| `:format` | html/js/json/xml etc | | |
| `:method` | HTTP request verb | | |
| `:path` | Request path | | |
```ruby | |
{ | |
controller: "PostsController", | |
action: "new", | |
params: { "action" => "new", "controller" => "posts" }, | |
format: :html, | |
method: "GET", | |
path: "/posts/new" | |
} | |
``` | |
### process_action.action_controller | |
| Key | Value | | |
| --------------- | --------------------------------------------------------- | | |
| `:controller` | The controller name | | |
| `:action` | The action | | |
| `:params` | Hash of request parameters without any filtered parameter | | |
| `:format` | html/js/json/xml etc | | |
| `:method` | HTTP request verb | | |
| `:path` | Request path | | |
| `:view_runtime` | Amount spent in view in ms | | |
```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 | |
} | |
``` | |
### send_file.action_controller | |
| Key | Value | | |
| ------- | ------------------------- | | |
| `:path` | Complete path to the file | | |
INFO. Additional keys may be added by the caller. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 it started | |
* Time when it finished | |
* An unique ID for this event | |
* The payload (described in previous sections) | |
```ruby | |
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 arguments like this: | |
```ruby | |
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| | |
event = ActiveSupport::Notifications::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 shortcut to just get the data. | |
```ruby | |
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 expression. This enables you to subscribe to | |
multiple events at once. Here's you could subscribe to everything from `ActionController`. | |
```ruby | |
ActiveSupport::Notifications.subscribe /action_controller/ do |*args| | |
# inspect all ActionController events | |
end | |
``` | |
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 `instrument` call will make it into the payload. | |
Here's an example: | |
```ruby | |
ActiveSupport::Notifications.instrument "my.custom.event", this: :data do | |
# do your custom stuff here | |
end | |
``` | |
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} | |
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`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment