Axios response time — Capture and log
Notice: Make sure to use axios version different then 0.19.0 as it introduced this feature/bug.
Github gist will full source code here.
In this post, we will explore how to capture and/or log HTTP response time with axios. We will be expending on one of my previous posts about Axios interceptors on how to log request and response (body and headers).
Without further ado, the first step is to record time when an HTTP request started, this can be easily done with a request interceptor.
Next, when we receive a response from a server, in the response interceptor, we need to add the following logic.
The above interceptor will log only successful requests, to add support for failed requests, we need to register an additional handler within the same response interceptor.
If for some reason, the consumer needs to access the response time value, let’s say showing it in the UI, that can be easily achieved with a slight modification of the response interceptor.
As you can see, it’s pretty straight forward to add response time logging to axios, this might get useful performance debugging tool if other solutions such APM are not available.
The above-shown way of measuring execution time is not the most precise you can get with NodeJS. If you need a greater precision you should checkout this post.
Full source code here.