Class: Goliath::Chimp::Rack::ServerMetrics

Inherits:
Object
  • Object
show all
Includes:
EnvExtractor, Rack::AsyncMiddleware
Defined in:
lib/goliath/chimp/rack/server_metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnvExtractor

#extract_from_env

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, options = {})
  @app     = app
  @path    = options[:path]    || '/metrics'
  @env_key = options[:env_key]
  @default = options[:default] || '/*'
end

Instance Attribute Details

#defaultObject (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_keyObject (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

#pathObject (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