Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Understanding the Instrumentation API in Rails

The Instrumentation Api in ActiveSupport serves a dual purpose. You can use it to implement the observer (pub-sub) pattern, as well as benchmark how long it took to execute some action. In this post, we'll learn almost everything you need to know about the Rails Instrumentation API.The observer design pattern (also called pub-sub) allows one or more subscribers to register with and receive notifications from a publisher. It's great for scenarios that need push-based notifications, instead of continuous polling. A real-world example is this blog. Whenever a new post is published, it sends an email to all the subscribers of the blog. As a result, the readers don't have to constantly refresh the blog to see if a new post was published.The pattern defines a publisher (also known as a provider or an observable) and zero or more subscribers (also known as observers or listeners). Subscribers register with the publisher, and whenever a predefined condition, event, or state change happens (e.g. a new post is published), the publisher automatically notifies all observers, passing any additional data to provide current state information to observers.The Instrumentation (or Notification) API is a simple implementation of the observer (pub-sub) pattern in Rails. It allows you to subscribe and listen to various events that occur within your Rails application or even the Rails framework. In addition, you can also use it to benchmark a piece of code. This article explains how to use the instrumentation API and some practical examples of how Rails (the framework) uses it internally.Events are a great way to decouple various aspects of your application. A single event can have multiple listeners in unrelated parts of the codebase, and a subscriber can listen to multiple events. The instrumentation API in Rails lets you create and publish such events to subscribers.The Rails framework provides several of these events itself. For example, To publish an event that other parts of the code can listen to, call the ActiveSupport::Notifications.instrument method, passing the event's name, payload (a hash containing information about the event), and an optional block. The payload is used to pass additional data to the event's subscribers. Hence, the instrumentation API serves a dual purpose. You can use it to benchmark how long it took to execute some action, as well as to notify other parts of the codebase that a certain event occurred.To listen to any custom events that you created, or the ones triggered by the Rails framework, call the ActiveSupport::Notifications.subscribe method, providing the name of the event you're subscribing to and passing a block that will be called whenever this event occurs. Too many arguments? Receive an event instead...If you don't want to type all those arguments each time you subscribe to an event, don't worry. Passing a block with a single argument will ensure it receives an ActiveSupport::Notifications::Event object.To continue our earlier example, you can subscribe to the event publish.post using the following code:Multiple subscribers can listen to a single event, allowing you to decouple your code. For example, you'd like to publish a newsletter as well as take a backup when you publish a post. You can accomplish this by dispatching an event after publishing a post. Instead of the event name, you can pass a regular expression to the subscribe method above. This lets you subscribe to multiple events at once.Here's the code to subscribe to all the events from ActiveRecord library. Alternatively, the subscriber can simply listen to each event separately. Sometimes, you're only interested in measuring how long it took to execute a code block. The duration property on the event object received by the subscriber returns the difference in milliseconds between when the execution of the event started and when it ended.This provides a simple way to figure out the parts of the codebase that are slowing the application down and optimize them. Let's say you would like to measure how long it took to render an action. In your Rails controller, you'd wrap the call to the render method in the instrument block as follows:Rails will first execute the provided block by calling render, and then notify all the subscribers. You can listen to this event by registering a subscriber.This lets you measure how long it took to render the view as well as get notified whenever rendering happens. That's a wrap. I hope you found this article helpful and you learned something new.As always, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. I reply to all emails I get from developers, and I look forward to hearing from you.If you'd like to receive future articles directly in your email, please subscribe to my blog. If you're already a subscriber, thank you.I'm a software developer living in beautiful Victoria, BC. This blog is my attempt to share my learnings and to spread the joy of programming in Ruby with the world.Subscribe to get the new posts via email. Your email address is never sold or shared.Processing your application...Success! Please check your inbox and click the link to confirm your subscription.Sorry, something went wrong. Please try again. © 2023 - Akshay's Blog



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Understanding the Instrumentation API in Rails

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×