1 of 33

Digging Into

ActiveSupport::Notifications

Suraj Nath

Engineer @ Grafana Labs

x: @electron0zero

2 of 33

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.

3 of 33

Poll: Have you heard of ActiveSupport::Notifications??

4 of 33

Poll: Have you used of ActiveSupport::Notifications??

5 of 33

Do you know that Rails has a Pub/Sub built into it?

🀯

6 of 33

What is ActiveSupport::Notifications?

7 of 33

Way for Rails applications* to publish and subscribe to some events

*can be used outside rails apps as well :)

8 of 33

Where can is use it?

Wherever you want?

However you want?

most folks use it for creating metrics, and logs from events.

9 of 33

Examples:

  • Send User sign up events to analytics (Google Analytics, Mixpanel, Amplitude, etc)
  • Log Slow SQL Queries..
  • Application Monitoring
  • Instrument your own libs for metrics and stats on them?

10 of 33

How do I use it?

11 of 33

Step 1: Publish something

πŸ“€

12 of 33

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

13 of 33

Step 2: Subscribe to it

πŸ“₯

14 of 33

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

15 of 33

16 of 33

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

17 of 33

Step 3: Profit?? 🀷

18 of 33

ActiveSupport::Notifications

From Rails

19 of 33

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

20 of 33

ActiveJob

21 of 33

ActionCable

22 of 33

ActionView

23 of 33

Rails - Deprecation events

24 of 33

Internal / Private Notifications

25 of 33

ActiveSupport::Notifications

From Gems

26 of 33

27 of 33

28 of 33

Q: What’s my overhead? Can I disable this if I am not using it?

29 of 33

Q: Where are these messages stored?

And can I bring my own queue?

30 of 33

31 of 33

32 of 33

Thank you

Slides: suraj.dev/talks

Twitter: @electron0zero

33 of 33