Commit eff9631a authored by Ryan Cobb's avatar Ryan Cobb Committed by James Lopez

Filter health endpoint metrics

parent abce316f
---
title: Filter health endpoint metrics
merge_request: 27847
author:
type: added
...@@ -15,6 +15,8 @@ module Gitlab ...@@ -15,6 +15,8 @@ module Gitlab
"report" => %w(404) "report" => %w(404)
}.freeze }.freeze
HEALTH_ENDPOINT = /^\/-\/(liveness|readiness|health|metrics)\/?$/.freeze
def initialize(app) def initialize(app)
@app = app @app = app
end end
...@@ -32,6 +34,10 @@ module Gitlab ...@@ -32,6 +34,10 @@ module Gitlab
{}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25]) {}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
end end
def self.http_health_requests_total
@http_health_requests_total ||= ::Gitlab::Metrics.counter(:http_health_requests_total, 'Health endpoint request count')
end
def self.initialize_http_request_duration_seconds def self.initialize_http_request_duration_seconds
HTTP_METHODS.each do |method, statuses| HTTP_METHODS.each do |method, statuses|
statuses.each do |status| statuses.each do |status|
...@@ -43,8 +49,13 @@ module Gitlab ...@@ -43,8 +49,13 @@ module Gitlab
def call(env) def call(env)
method = env['REQUEST_METHOD'].downcase method = env['REQUEST_METHOD'].downcase
started = Time.now.to_f started = Time.now.to_f
begin begin
RequestsRackMiddleware.http_request_total.increment(method: method) if health_endpoint?(env['PATH_INFO'])
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)
...@@ -57,6 +68,12 @@ module Gitlab ...@@ -57,6 +68,12 @@ module Gitlab
raise raise
end end
end end
def health_endpoint?(path)
return false if path.blank?
HEALTH_ENDPOINT.match?(CGI.unescape(path))
end
end end
end end
end end
...@@ -36,6 +36,74 @@ describe Gitlab::Metrics::RequestsRackMiddleware do ...@@ -36,6 +36,74 @@ describe Gitlab::Metrics::RequestsRackMiddleware do
Timecop.scale(3600) { subject.call(env) } Timecop.scale(3600) { subject.call(env) }
end end
context 'request is a health check endpoint' do
it 'increments health endpoint counter' do
env['PATH_INFO'] = '/-/liveness'
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get')
subject.call(env)
end
context 'with trailing slash' do
before do
env['PATH_INFO'] = '/-/liveness/'
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
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
context 'request is not a health check endpoint' do
it 'does not increment health endpoint counter' do
env['PATH_INFO'] = '/-/ordinary-requests'
expect(described_class).not_to receive(:http_health_requests_total)
subject.call(env)
end
context 'path info is a root path' do
before do
env['PATH_INFO'] = '/-/'
end
it 'does not increment health endpoint counter' do
expect(described_class).not_to receive(:http_health_requests_total)
subject.call(env)
end
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
context '@app.call throws exception' do context '@app.call throws exception' do
......
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