# Working with the Rails Instrumentation API

*Published on February 08, 2025*

Tags: Rails Internals

---


The observer design pattern (also called&nbsp;**pub-sub**) allows one or more subscribers to register with and receive notifications from a publisher. It's great for scenarios that need push-based notifications, instead of continuous polling. A real-world example is this blog. Whenever a new post is published, it sends an email to all the subscribers of the blog. As a result, the readers don't have to constantly refresh the blog to see if a new post was published.

The pattern defines a&nbsp;**publisher**&nbsp;(also known as a&nbsp;_provider_&nbsp;or an&nbsp;_observable_) and zero or more&nbsp;**subscribers**&nbsp;(also known as&nbsp;_observers_&nbsp;or&nbsp;_listeners_). Subscribers register with the publisher, and whenever a predefined condition, event, or state change happens (e.g. a new post is published), the publisher automatically notifies all observers, passing any additional data to provide current state information to observers.

The&nbsp;[Instrumentation (or Notification) API](https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html)&nbsp;is a simple implementation of the observer (pub-sub) pattern in Rails. It allows you to subscribe and listen to various events that occur within your Rails application or even the Rails framework. In addition, you can also use it to benchmark a piece of code. This article explains how to use the instrumentation API and some practical examples of how Rails (the framework) uses it internally.

### **What we'll learn:**

1. How to publish an event
2. How to subscribe to an event
3. Publishing an event to multiple subscribers
4. Subscribing to multiple events
5. Performance benchmarking
6. Example: Instrumenting Render

* * *

Events are a great way to decouple various aspects of your application. A single event can have multiple listeners in unrelated parts of the codebase, and a subscriber can listen to multiple events. The instrumentation API in Rails lets you create and publish such events to subscribers.

The Rails framework provides several of these events itself. For example,

- Active Record fires an event named&nbsp;`sql.active_record`&nbsp;every time it uses a SQL query on a database. You can subscribe to this event to keep track of the total number of queries made during an action.
- Action controller fires an event&nbsp;`process_action.action_controller`&nbsp;after processing a controller action, allowing you to track how long it took to process that action.

## How to Publish an Event

To publish an event that other parts of the code can listen to, call the&nbsp;`ActiveSupport::Notifications.instrument`&nbsp;method, passing the event's name, payload (a hash containing information about the event), and an optional block. The payload is used to pass additional data to the event's subscribers.

```ruby
ActiveSupport::Notifications.instrument "publish.post", { title: "hello world" } do
  puts "Creating and publishing the post"
end
```

- If you pass a block, ActiveSupport will execute the block and&nbsp;**then**&nbsp;call all the event subscribers with the provided payload and the time taken to execute the block.
- If you don't pass the block, ActiveSupport will simply notify the subscribers.

Hence, the instrumentation API serves a dual purpose. You can use it to benchmark how long it took to execute some action, as well as to notify other parts of the codebase that a certain event occurred.

## How to Subscribe to an Event

To listen to any custom events that you created, or the ones triggered by the Rails framework, call the&nbsp;`ActiveSupport::Notifications.subscribe`&nbsp;method, providing the&nbsp;**name of the event**&nbsp;you're subscribing to and passing&nbsp;**a block that will be called whenever this event occurs**.

```ruby
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, id, data|
  name    # => String, name of the event (such as 'render' from above)
  start   # => Time, when the instrumented block started execution
  finish  # => Time, when the instrumented block ended execution
  id      # => String, unique ID for the instrumenter that fired the
  data    # => Hash, the payload
end
```

**Too many arguments? Receive an event instead...**

If you don't want to type all those arguments each time you subscribe to an event, don't worry. Passing a block with a single argument will ensure it receives an&nbsp;`ActiveSupport::Notifications::Event`&nbsp;object.

```ruby
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |event|
  puts event.class # ActiveSupport::Notifications::Event
  puts event.name  # process_action.action_controller
  puts event.duration  # 0.03 (in ms)
  puts event.payload
end
```

To continue our earlier example, you can subscribe to the event&nbsp;`publish.post`&nbsp;using the following code:

```ruby
ActiveSupport::Notifications.subscribe "publish.post" do |event|
  puts "Published the post"
end
```

💡 The code that subscribes to an event must run before the code that publishes the event. If no one is listening when the event is published, future subscribers won't be notified. For example, if you subscribe to my blog today, you won't receive any articles I published last month. Get it?

## Publishing to Multiple Subscribers
![publish-to-many-subscribers.png](https://writesoftwarewell.com/media/0fkujayitm6cjxf4vg2bs0vmskf8.png "Publish to Many Subscribers")

Multiple subscribers can listen to a single event, allowing you to decouple your code. For example, you'd like to publish a newsletter as well as take a backup when you publish a post. You can accomplish this by dispatching an event after publishing a post.

```ruby
# newsletter service
ActiveSupport::Notifications.subscribe "publish.post" do |event|
  # send newsletter
end

# backup service
ActiveSupport::Notifications.subscribe "publish.post" do |event|
  # take backup
end

# blogging-related code
ActiveSupport::Notifications.instrument "publish.post", { id: post.id } do
  # publish the post
end
```

## Subscribing to Multiple Events

Instead of the event name, you can pass a regular expression to the&nbsp;`subscribe`&nbsp;method above. This lets you subscribe to multiple events at once.

![subscribe-to-multiple-events.png](https://writesoftwarewell.com/media/tgrs74bz57wptymrfqusd6gmwi63.png "Subscribe to Multiple Events")

Here's the code to subscribe to all the events from&nbsp;`ActiveRecord`&nbsp;library.

```ruby
ActiveSupport::Notifications.subscribe /active_record/ do |*args|
  # inspect all ActiveRecord events
end
```

Alternatively, the subscriber can simply listen to each event separately.

## Performance Benchmarking

Sometimes, you're only interested in measuring how long it took to execute a code block. The&nbsp;`duration`&nbsp;property on the event object received by the subscriber returns the difference in milliseconds between when the execution of the event started and when it ended.

```ruby
ActiveSupport::Notifications.subscribe "publish.post" do |event|
  puts "#{event.duration} ms"  # 3001.50 ms
end

ActiveSupport::Notifications.instrument "publish.post" do
  sleep 3
  puts "instrument: publishing the post"
end
```

This provides a simple way to figure out the parts of the codebase that are slowing the application down and optimize them.

## Example: Instrumenting Render

Let's say you would like to measure how long it took to render an action. In your Rails controller, you'd wrap the call to the&nbsp;`render`&nbsp;method in the&nbsp;`instrument`&nbsp;block as follows:

```ruby
ActiveSupport::Notifications.instrument('render', extra: :information) do
  render 'index'
end
```

Rails will first execute the provided block by calling&nbsp;`render`, and then notify all the subscribers.

You can listen to this event by registering a subscriber.

```ruby
ActiveSupport::Notifications.subscribe('render') do |name, start, finish, id, payload|
  # do something with the information provided.
end
```

This lets you measure how long it took to render the view as well as get notified whenever rendering happens.

