Class: Goliath::Chimp::Rack::ServerMetrics
- Inherits:
-
Object
- Object
- Goliath::Chimp::Rack::ServerMetrics
- Includes:
- EnvExtractor, Rack::AsyncMiddleware
- Defined in:
- lib/goliath/chimp/rack/server_metrics.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#env_key ⇒ Object
readonly
Returns the value of attribute env_key.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ ServerMetrics
constructor
A new instance of ServerMetrics.
- #post_process(env, status, headers, body) ⇒ Object
Methods included from EnvExtractor
Constructor Details
#initialize(app, options = {}) ⇒ ServerMetrics
Returns a new instance of ServerMetrics.
9 10 11 12 13 14 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 9 def initialize(app, = {}) @app = app @path = [:path] || '/metrics' @env_key = [:env_key] @default = [:default] || '/*' end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
7 8 9 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 7 def default @default end |
#env_key ⇒ Object (readonly)
Returns the value of attribute env_key.
7 8 9 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 7 def env_key @env_key end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 7 def path @path end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 16 def call env if env['PATH_INFO'] == path [ 200, {}, env['status'] ] else super end end |
#post_process(env, status, headers, body) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/goliath/chimp/rack/server_metrics.rb', line 24 def post_process(env, status, headers, body) base_metrics = { count: 0, total_millis: 0 } env['status'][:requests] ||= Hash.new{ |h, k| h[k] = Hash.new{ |h, k| h[k] = base_metrics } } request_key = extract_from_env(env, env_key, default) request_method = env['REQUEST_METHOD'].downcase.to_sym metrics = env['status'][:requests][request_key][request_method] metrics[:count] += 1 elapsed_millis = ((Time.now.to_f - env[:start_time]) * 1000).round metrics[:total_millis] += elapsed_millis [status, headers, body] end |