Commit 072ba99e authored by Bob Van Landuyt's avatar Bob Van Landuyt

This adds a status label to http_requests_total

This adds a status label to the http_requests_total metric. To do so,
we've had to move the recording to an ensure block after the request
is performed.

In practise our @app.call never fails. But there's a spec covering the
recording of the request count even if it fails. That's why the
recording happens inside an ensure.
parent 9cfb0b2b
...@@ -49,14 +49,9 @@ module Gitlab ...@@ -49,14 +49,9 @@ module Gitlab
method = 'INVALID' unless HTTP_METHODS.key?(method) method = 'INVALID' unless HTTP_METHODS.key?(method)
started = Time.now.to_f started = Time.now.to_f
health_endpoint = health_endpoint?(env['PATH_INFO']) health_endpoint = health_endpoint?(env['PATH_INFO'])
status = 'undefined'
begin begin
if health_endpoint
RequestsRackMiddleware.http_health_requests_total.increment(method: method)
else
RequestsRackMiddleware.http_request_total.increment(method: method)
end
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
elapsed = Time.now.to_f - started elapsed = Time.now.to_f - started
...@@ -69,6 +64,12 @@ module Gitlab ...@@ -69,6 +64,12 @@ module Gitlab
rescue rescue
RequestsRackMiddleware.rack_uncaught_errors_count.increment RequestsRackMiddleware.rack_uncaught_errors_count.increment
raise raise
ensure
if health_endpoint
RequestsRackMiddleware.http_health_requests_total.increment(method: method, status: status)
else
RequestsRackMiddleware.http_request_total.increment(method: method, status: status)
end
end end
end end
......
...@@ -22,7 +22,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -22,7 +22,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
end end
it 'increments requests count' do it 'increments requests count' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 200)
subject.call(env) subject.call(env)
end end
...@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
end end
it 'increments health endpoint counter rather than overall counter' do it 'increments health endpoint counter rather than overall counter' do
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get', status: 200)
expect(described_class).not_to receive(:http_request_total) expect(described_class).not_to receive(:http_request_total)
subject.call(env) subject.call(env)
...@@ -68,7 +68,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -68,7 +68,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
end end
it 'increments overall counter rather than health endpoint counter' do it 'increments overall counter rather than health endpoint counter' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 200)
expect(described_class).not_to receive(:http_health_requests_total) expect(described_class).not_to receive(:http_health_requests_total)
subject.call(env) subject.call(env)
...@@ -101,7 +101,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -101,7 +101,7 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
end end
it 'increments requests count' do it 'increments requests count' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get') expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 'undefined')
expect { subject.call(env) }.to raise_error(StandardError) expect { subject.call(env) }.to raise_error(StandardError)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment