DEV Community

radin reth
radin reth

Posted on

2 1

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay