Trace api request logs in Rails

Kavita Jadhav
3 min readDec 2, 2020
Image by Hans Braxmeier from Pixabay

We all know the benefits of logging. It enables developers to quickly troubleshoot issues even before reproducing them. These can be logs generated by the framework and custom logs added by developers.

In the Production environment, we have many app servers running behind the load balancer. Request can be processed by any server and hence to get the request logs you need to know which server has processed the request. It gets even more complex when you have multiple services in place.

In distributed systems, computing logs are stored in a shared drive or sometimes on distributed servers itself. Later these logs are aggregated in tools like Splunk, LogDNA, Logstash.. etc. to make them accessible in a single place and enable users to search/read logs.

Since now all the logs are aggregated in one place it reduces the effort of checking logs on multiple servers but still you have to search for keywords(like api_request, query parameter, custom log statements..etc.) to get expected logs. Using this approach you might miss some important logs which might increase your debug time.

Example: Here I have created an application and added a /health endpoint to it. To check logs am searching for text /health. This is giving me all log statements having the keyword health in it. Also if you look at the timeline section you are getting older logs as well. Hence you have to manually compare time as well to get specific requests.

log search results with keyword

This can be solved in rails by using rails_distributed_tracing gem written by Ajit Singh.

To get started add rails_distributed_tracing gem to your Gemfile and run bundle install command. Then follow the steps mentioned in the README file.

gem 'rails_distributed_tracing'

Now restart the application and send a request from the browser or Postman and monitor network call logs. You will get X-Request-Id attribute in Response Headers as below:

API request network logs

You can use this X-Request-Id to search all logs generated for the API request in all services.

Log search results with request_id

Here you are getting all logs for specific request only. Please note that I am using a different request_id to search logs as I have generated these at different point of time. You can search logs with same request_id available in response headers.

I hope this will help to make debugging a easy task!

Happy coding!!!😇

--

--