Digging Into
ActiveSupport::Notifications
About me:
Work @ Grafana Labs on Distributed Tracing.
Used to build rails apps, but I still <3 Ruby & Rails
Hosted few BRUG at Clarisights office in pre-covid era.
Poll: Have you heard of ActiveSupport::Notifications??
Poll: Have you used of ActiveSupport::Notifications??
Do you know that Rails has a Pub/Sub built into it?
π€―
What is ActiveSupport::Notifications?
Way for Rails applications* to publish and subscribe to some events
*can be used outside rails apps as well :)
Where can is use it?
Wherever you want?
However you want?
most folks use it for creating metrics, and logs from events.
Examples:
How do I use it?
Step 1: Publish something
π€
ActiveSupport::Notifications.instrument "my.custom.event", this: :data do
# do your work
end
ActiveSupport::Notifications.instrument "my.custom.event", this: :data
OR just send the event with your data
Step 2: Subscribe to it
π₯
ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data|
puts data.inspect # {:this=>:data}
end
ActiveSupport::Notifications.subscribe /action_controller/ do |*args|
# event = ActiveSupport::Notifications::Event.new(*args)
# event.duration?
# inspect all ActionController events
# case on event.name?? To do different things
end
or subscribe with a regex
Please follow Rails conventions when defining your own events. The format is: event.library
If you have an application is sending Tweets, you should create an event named tweet.twitter
Step 3: Profit?? π€·
ActiveSupport::Notifications
From Rails
Action Controller
Action Controller β Caching
Action Dispatch
Action View
Active Record
Action Mailer
Active Support β Caching
Active Job
Action Cable
Active Storage
Active Storage β Storage Service
Railties
Rails
ActiveJob
ActionCable
ActionView
Rails - Deprecation events
Internal / Private Notifications
ActiveSupport::Notifications
From Gems
Q: Whatβs my overhead? Can I disable this if I am not using it?
Q: Where are these messages stored?
And can I bring my own queue?