Sometimes while developing REST API’s we want to log the response time of our REST APi for the sake of better tuning. We are going to discuss the same case here and will see how to do that in Akka-http .
So sometime before while I was working on akka-http while developing a spark-akka-http-couchbase Lightbend activator template , a weird use case came in-front of me. I wanted to log each and every request that is made to my server along with the response time that my server took to execute it. I searched for it and found that others are also searching for the same thing. I thought that akka must have provided something for implementing this (which they probably don’t) , but I didn’t found anything in the documentation , and hence I have to come up with my own custom solution( just a little hack ! 😉 )
So let’s start with first things first :
I wanted this just to tune my REST APi , which is quite an important task and one can do so many things to tune it on its Local machine but the real thing happens on the production-server , where Logs are our best friend and the enemies too :P. Hence I developed this so that when we deploy our application on Staging Server(replica of production) , we can tune it in the best way by analyzing as to which routes are taking the maximum time, and then as the routine : check the code use Futures etc wherever possible and do the things in the right way.
How to do this in akka-http.?
So here is the code which can be used to log the response time of each and every request.
Here we are using the Debugging directives of akka for logging and also scala-rainbow by Konard Malawski for the colors . This will log the request that are coming to your server on to your console in a colored fashion. If the request takes more than 3 sec(as a standard) it will be RED or else GREEN.
You can find the whole code here.
I hope now you can log your Response time as well ! 🙂
Happy hakking ! 😀