0

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.

Michael
  • 683
  • 9
  • 21

1 Answers1

0

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.

trh
  • 7,186
  • 2
  • 29
  • 41
  • This is great, but I need to access the ContentLength inside the app, so that I store it someplace (or log it). Do you know how to make this happen so that an event can fire after ContentLength is determined (or, even better, after the client connection is closed)? – Michael Sep 01 '13 at 15:48