Class: Rack::Informant
- Inherits:
-
Object
- Object
- Rack::Informant
- Defined in:
- lib/rack/informant.rb
Overview
Middleware that “informs on” an application, reporting everything it does to its secret employer. Other middlewares may become hostile if they find out.
After each request is processed, calls the provided block with a hash containing:
* :method - HTTP request method, e.g. 'GET', 'POST'
* :path - path segment of the request (no host, port or querystring),
e.g. '/info'
* :status - HTTP response status code, e.g. 200, 404
* :runtime - total request processing time (by the server's reckoning) in
*seconds*, as a float. e.g. 0.1 == 100 ms.
This middleware is async-safe (i.e. should work correctly for both synchronous requests and asynchronous requests).
Example usage:
require 'rack/informant'
use Rack::Informant do |request|
# poor man's request logging (use Rack::CommonLogger instead)
puts "#{request[:method]} #{request[:path]}: #{request[:status]} #{request[:runtime]}"
end
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, &block) ⇒ Informant
constructor
A new instance of Informant.
Constructor Details
#initialize(app, &block) ⇒ Informant
Returns a new instance of Informant.
27 28 29 30 |
# File 'lib/rack/informant.rb', line 27 def initialize(app, &block) @app = app @callback = block || lambda {|*_|} end |
Instance Method Details
#call(env) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rack/informant.rb', line 32 def call(env) start = Time.now if original_callback = env['async.callback'] env['async.callback'] = proc do |response| status, headers, body = response inform!(env, start, status, headers, body) original_callback.call(response) end end catch :async do status, headers, body = @app.call(env) # if we got this far, then @app.call didn't throw :async return [status, headers, body] if status == -1 # alternative async API # if we got *this* far, @app.call is definitely synchronous inform!(env, start, status, headers, body) return [status, headers, body] end # we only end up here if @app.call threw :async, so just throw it on. throw :async end |