Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 47 additions and 1 deletion.
  1. +1 −1 lib/goliath/chimp/rack/server_metrics.rb
  2. +46 −0 spec/rack/server_metrics_spec.rb
View
2 lib/goliath/chimp/rack/server_metrics.rb
@@ -26,7 +26,7 @@ def post_process(env, status, headers, body)
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 = 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
View
46 spec/rack/server_metrics_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Goliath::Chimp::Rack::ServerMetrics do
+
+ let(:app) { double :app }
+ let(:status) { Hash.new }
+ let(:example_time){ Time.new(2014, 1, 1, 3, 15) }
+ let(:env) { { 'status' => status, endpoint: 'images', start_time: example_time.to_f } }
+ subject { described_class.new(app, env_key: 'endpoint') }
+
+ context '#call', 'when /metrics' do
+ it 'does not call the app' do
+ app.should_not_receive(:call)
+ env['PATH_INFO'] = '/metrics'
+ subject.call env
+ end
+
+ it 'returns the current status' do
+ env['PATH_INFO'] = '/metrics'
+ subject.call(env).should eq([200, {}, status])
+ end
+ end
+
+ context '#call', 'when /^version' do
+ it 'does nothing' do
+ app.should_receive(:call)
+ subject.should_receive(:post_process)
+ env['PATH_INFO'] = '/foobar'
+ subject.call env
+ end
+ end
+
+ context '#post_process' do
+ it 'does not alter the response' do
+ env['REQUEST_METHOD'] = 'GET'
+ subject.post_process(env, 200, {}, {}).should eq([200, {}, {}])
+ end
+
+ it 'attaches request metrics to the status' do
+ env['REQUEST_METHOD'] = 'GET'
+ Time.stub(:now).and_return(example_time + 30)
+ subject.post_process(env, 200, {}, {})
+ status.should eq(requests: { 'images' => { get: { count: 1, total_millis: 30000 } } })
+ end
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.