How to capture size of an API response in Rails?

725 Views Asked by At

I want to capture the size of responses being made by the API of my Rails 3 app.

The idea is that if a client application requests a resource, the full size of the JSON response (in KB/MB, after RABL renders it) can be captured and stored, so that I can see how much data the client application is using later. Because they are exceptionally large, some of these API JSON responses are cached (locally for now, on Redis in the near future), so the solution needs to be able to capture the size of cached responses, not just rendered ones.

What is the best method and place for accomplishing this in Rails? On the Rack level? In the controller (the controllers use ActionController::Metal)? Obviously, the less overhead this puts on response times, the better.

1

There are 1 best solutions below

1
On

You can turn on ContentLength in Rack by adding this to your application.rb

config.middleware.use Rack::ContentLength

Then content length will be sent in the header with every request.