Permalink
Browse files

Merge pull request #22 from librato/feature/capture_request_body

Include request body in output for failed requests.
  • Loading branch information...
nextmat committed Apr 6, 2012
2 parents aabed18 + 86326bb commit baeea8a3bc5930e1d6562b251f0f59b5faa2e4d5
@@ -1,6 +1,7 @@
require 'faraday'
require 'metrics/middleware/count_requests'
require 'metrics/middleware/expects_status'
+require 'metrics/middleware/request_body'
require 'metrics/middleware/retry'
module Librato
@@ -29,10 +30,13 @@ def transport
raise NoClientProvided unless @client
@transport ||= Faraday::Connection.new(:url => api_endpoint + "/v1/") do |f|
#f.use FaradayMiddleware::EncodeJson
+ f.use Librato::Metrics::Middleware::RequestBody
f.use Librato::Metrics::Middleware::Retry
- f.use Librato::Metrics::Middleware::CountRequests
+ f.use Librato::Metrics::Middleware::CountRequests
+
f.use Librato::Metrics::Middleware::ExpectsStatus
#f.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
+
f.adapter Faraday.default_adapter
end.tap do |transport|
transport.headers[:user_agent] = user_agent
@@ -5,8 +5,7 @@ module Middleware
class ExpectsStatus < Faraday::Response::Middleware
def on_complete(env)
- # TODO: clean up exception output
- # TODO: catch specific status codes by request
+ # TODO: make exception output prettier
case env[:status]
when 401
raise Unauthorized, env.to_s
@@ -0,0 +1,18 @@
+module Librato
+ module Metrics
+ module Middleware
+
+ class RequestBody < Faraday::Response::Middleware
+
+ def call(env)
+ # duplicate request body so it is preserved through request
+ # in case we need it for exception output
+ env[:request_body] = env[:body]
+ @app.call(env)
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit baeea8a

Please sign in to comment.