Class: RackMiddlewareMetrics::Reporter
- Inherits:
-
Object
- Object
- RackMiddlewareMetrics::Reporter
- Defined in:
- lib/rack_middleware_metrics/reporter.rb
Overview
The metrics reporter middleware.
Instance Method Summary collapse
-
#append_data(data) ⇒ void
Run the request–call next step in middleware/server chain with added timing.
-
#call(env) ⇒ Array<objects>
Main middleware entry and exit point.
-
#initialize(app, *args) ⇒ Reporter
constructor
Initialize the reporter middleware.
-
#params_from(env) ⇒ String
Converts data from rack env object to semicolon delimited parameters.
-
#timed_call(env) ⇒ Array<Array<Objects>>
Run the request–call next step in middleware/server chain with added timing.
-
#uri_from(env) ⇒ String
Converts data from rack env object to uri.
Constructor Details
#initialize(app, *args) ⇒ Reporter
Initialize the reporter middleware.
22 23 24 25 26 27 28 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 22 def initialize app, *args @app = app # Rack `use` only allows *args. Use either logpath passed via hash or default path. = args.last.is_a?(Hash) ? args.last : {} @logpath = Pathname.new(.fetch(:logpath, 'rack_metrics.csv')) end |
Instance Method Details
#append_data(data) ⇒ void
This method returns an undefined value.
Run the request–call next step in middleware/server chain with added timing.
69 70 71 72 73 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 69 def append_data data CSV.open(@logpath, 'a') do |csv| csv << data end end |
#call(env) ⇒ Array<objects>
Main middleware entry and exit point. Logs metrics as specified.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 79 def call env status, headers, body, timing = timed_call(env) query_params = params_from(env) uri = uri_from(env) thread_id = Thread.current.object_id process_id = Process.pid md5 = MD5Gen.compute(body.to_s) append_data([*timing, uri, query_params, thread_id, process_id, md5]) [status, headers, body] end |
#params_from(env) ⇒ String
Converts data from rack env object to semicolon delimited parameters. Original semicolons escaped with %3B the ASCII equivalent. See www.december.com/html/spec/esccodes.html for more details.
45 46 47 48 49 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 45 def params_from env params = Hash[URI.decode_www_form(env['QUERY_STRING'])] params = params.map { |key, val| "#{ key }=#{ val }" } params.map { |param| param.gsub(';', '%3B') }.join(';') end |
#timed_call(env) ⇒ Array<Array<Objects>>
Run the request–call next step in middleware/server chain with added timing.
57 58 59 60 61 62 63 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 57 def timed_call env start_time = Time.now response = @app.call(env) end_time = Time.now duration = end_time - start_time [*response, [start_time, end_time, duration]] end |
#uri_from(env) ⇒ String
Converts data from rack env object to uri.
34 35 36 37 |
# File 'lib/rack_middleware_metrics/reporter.rb', line 34 def uri_from env "#{ env['rack.url_scheme'] }://#{ env['SERVER_NAME'] }"\ ":#{ env['SERVER_PORT'] }#{ env['PATH_INFO'] }" end |