One of the lesser known parts of Rails core is the ActiveSupport instrumentation framework.
ActiveSupport::Notifications
includes all the things you need to implement pub-sub in Rails.
Pub-Sub is a software architecture where you publish (send) a message without being specific about who should
receive it. Fire and forget.
Receiving a message, and doing something with it, "just" requires you to subscribe to it. Because the publisher doesn't need to know about the subscribers (as they are decoupled), this provides nice opportunities for organization and scale.
Let's explore the joyful shenanigans of this.
There is an instrumentation message emitted from ActionController
that includes interesting data
about the things happening in your controller action. Let's explore this.
If you don't have a Ruby on Rails app at hand, just setup a minimal one with rails new --minimal
Add this code into an initializer
Boot the app (rails server
), visit http://127.0.0.0:3000 and you'll
see the new log lines in your development.log. So what? What's the difference to calling Rails.logger
in
an action or callback inside your controller? Why is ActiveSupport::Notifications
fabulous?
First, as explained in the intro, the main advantage is that the publisher is decoupled from the subscriber. For instance,
you can have more than one subscriber listening to process_action.action_controller
.
You are free to organize this however you want. Decouple publisher/subscriber in different files, chronological or even in different threads.
Second, and you probably already guessed this from the example, what makes this most awesome are the ready made messages that are already published today.
Rails for instance uses ActiveSupport::Notifications
to publish close to 50(!) different instrumentation events
that include data about your application. Data ranging from the HTTP status code of your requests, over which partials
were rendered, to more esoteric measurements like the byte range attempted to be read from your ActiveStorage service.
Check the instrumentation guide
for all the dirty secrets.
Last but not least, you can not only listen to messages others publish, you can publish messages to yourself.
Okay, you're convinced I hope! Now what do people do with ActiveSupport::Notifications
out in the world?
Ever wondered how your metrics get to Sentry, New Relic, Datadog or Skylight? You guessed it, via ActiveSupport::Notifications
.
Now if the main work, publishing tons of messages through a pub-sub architecture, is already done by Rails, surely there are non-SaaS (a.k.a. Software Libre) options to display this data, right? Turns out there are not. While evaluating options for some of my projects (especially Open Build Service) we came across this gap and started to fill it.
How, why and where? Read on in the next part of this series: Measure twice, cut once: App Performance Monitoring with influxdb-rails
Any criticism, remarks or praise about this post? Get in touch, I'm looking forward to your input!