Track Request Time With the Faraday
I’m frequently find myself in need to measure request time of remote request from my application to an API or service. Previously, i used simple block of ruby code with
end_time. Finally i decided to find out more robust method of doing so. I’m using
faraday gem for remote requests, because it’s easy extendable by middlewares and great overall. You can take a look at the project with collection of middlewares for faraday on github. This project contains
Instrumentation middleware that we will use for tracking time of our request.
Before we start, here is an image of Dr. Faraday from LOST:
By the way github handle of faraday author is @lostisland. Coincidence? I don’t think so :)
To the work! Let’s look inside the instrumentation middleware:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Pretty straightforward, we just instrument event
'request.faraday'. If you are not familiar with the
ActiveSupport::Notifications mechanism you can read about it here.
We should add this middleware to our faraday connection object:
1 2 3 4 5 6 7
Let’s subscribe to the
request.faraday events with the
ActiveSupport::Notifications. You can execute any code inside your subscribe block, save information and time to the file or database for example. I will use rails logger in my example:
1 2 3 4 5 6
That’s all, you are set and ready, whenever your application will send any request with the faraday connection, it will print request time information to your log file.