DEV Community 👩‍💻👨‍💻

radin reth
radin reth

Posted on

Rails active support instrumentation

Today I am gonna have a brief introduction about active support instrumentation. We will start from what it really is and how to use it.

Instrumentation provide a HOOK when certain events happen inside rails. Moreover, You can publish your own custom event if you want :)

How it works

It's really easy. All you need to do it listen to event that rails publish.

Below is the activity logs feature that I implement in my current project

  1. I subscribe to process_action.action_controller (rails specific event)

  2. I capture some information and pass to sidekiq's worker to perform some task

# config/initializers/my_event.rb

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
  args = data.slice(:controller, :action, :format, :method, :path, :status)
  args[:payload] = data[:params].except(:action, :controller)
  ActivityLogsWorker.perform_async args
end
Enter fullscreen mode Exit fullscreen mode

Notice that
data is a hash with the following keys
image

It's simple as that.
for more detail read more on rails official website

Top comments (0)

👋 Every week new members join DEV and share a bit about them in our Welcome Thread

 
Welcome them to DEV and share a bit about yourself