Commit c5ad359d authored by James Lopez's avatar James Lopez

Merge branch 'remove-health-endpoints-from-rack-metrics' into 'master'

Exclude health checks from http_request_duration_seconds bucket

See merge request gitlab-org/gitlab!43409
parents 9eea5285 879e02e0
...@@ -48,9 +48,10 @@ module Gitlab ...@@ -48,9 +48,10 @@ module Gitlab
method = env['REQUEST_METHOD'].downcase method = env['REQUEST_METHOD'].downcase
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'])
begin begin
if health_endpoint?(env['PATH_INFO']) if health_endpoint
RequestsRackMiddleware.http_health_requests_total.increment(method: method) RequestsRackMiddleware.http_health_requests_total.increment(method: method)
else else
RequestsRackMiddleware.http_request_total.increment(method: method) RequestsRackMiddleware.http_request_total.increment(method: method)
...@@ -59,7 +60,10 @@ module Gitlab ...@@ -59,7 +60,10 @@ module Gitlab
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
elapsed = Time.now.to_f - started elapsed = Time.now.to_f - started
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method, status: status.to_s }, elapsed)
unless health_endpoint
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method, status: status.to_s }, elapsed)
end
[status, headers, body] [status, headers, body]
rescue rescue
......
...@@ -38,69 +38,49 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -38,69 +38,49 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
end end
context 'request is a health check endpoint' do context 'request is a health check endpoint' do
it 'increments health endpoint counter' do ['/-/liveness', '/-/liveness/', '/-/%6D%65%74%72%69%63%73'].each do |path|
env['PATH_INFO'] = '/-/liveness' context "when path is #{path}" do
before do
env['PATH_INFO'] = path
end
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get') 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).not_to receive(:http_request_total)
subject.call(env) subject.call(env)
end end
context 'with trailing slash' do
before do
env['PATH_INFO'] = '/-/liveness/'
end
it 'increments health endpoint counter' do it 'does not record the request duration' do
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get') expect(described_class).not_to receive(:http_request_duration_seconds)
subject.call(env) subject.call(env)
end end
end
context 'with percent encoded values' do
before do
env['PATH_INFO'] = '/-/%6D%65%74%72%69%63%73' # /-/metrics
end
it 'increments health endpoint counter' do
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get')
subject.call(env)
end end
end end
end end
context 'request is not a health check endpoint' do context 'request is not a health check endpoint' do
it 'does not increment health endpoint counter' do ['/-/ordinary-requests', '/-/', '/-/health/subpath'].each do |path|
env['PATH_INFO'] = '/-/ordinary-requests' context "when path is #{path}" do
before do
expect(described_class).not_to receive(:http_health_requests_total) env['PATH_INFO'] = path
end
subject.call(env)
end it 'increments overall counter rather than health endpoint counter' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get')
context 'path info is a root path' do expect(described_class).not_to receive(:http_health_requests_total)
before do
env['PATH_INFO'] = '/-/' subject.call(env)
end end
it 'does not increment health endpoint counter' do it 'records the request duration' do
expect(described_class).not_to receive(:http_health_requests_total) expect(described_class)
.to receive_message_chain(:http_request_duration_seconds, :observe)
subject.call(env) .with({ method: 'get', status: '200' }, a_positive_execution_time)
end
end subject.call(env)
end
context 'path info is a subpath' do
before do
env['PATH_INFO'] = '/-/health/subpath'
end
it 'does not increment health endpoint counter' do
expect(described_class).not_to receive(:http_health_requests_total)
subject.call(env)
end end
end end
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