2018-11-17 00:37:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-12 22:46:17 +00:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
class RequestsRackMiddleware
|
2020-10-29 03:09:25 +00:00
|
|
|
HTTP_METHODS = {
|
|
|
|
"delete" => %w(200 202 204 303 400 401 403 404 500 503),
|
|
|
|
"get" => %w(200 204 301 302 303 304 307 400 401 403 404 410 422 429 500 503),
|
|
|
|
"head" => %w(200 204 301 302 303 401 403 404 410 500),
|
|
|
|
"options" => %w(200 404),
|
|
|
|
"patch" => %w(200 202 204 400 403 404 409 416 500),
|
|
|
|
"post" => %w(200 201 202 204 301 302 303 304 400 401 403 404 406 409 410 412 422 429 500 503),
|
|
|
|
"put" => %w(200 202 204 400 401 403 404 405 406 409 410 422 500)
|
|
|
|
}.freeze
|
2019-10-02 21:06:22 +00:00
|
|
|
|
2020-04-06 15:10:04 +00:00
|
|
|
HEALTH_ENDPOINT = /^\/-\/(liveness|readiness|health|metrics)\/?$/.freeze
|
|
|
|
|
2020-10-15 09:08:41 +00:00
|
|
|
FEATURE_CATEGORY_HEADER = 'X-Gitlab-Feature-Category'
|
|
|
|
FEATURE_CATEGORY_DEFAULT = 'unknown'
|
|
|
|
|
2017-07-12 22:46:17 +00:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
2020-10-29 03:09:25 +00:00
|
|
|
def self.http_requests_total
|
|
|
|
@http_requests_total ||= ::Gitlab::Metrics.counter(:http_requests_total, 'Request count')
|
2017-07-12 22:46:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.rack_uncaught_errors_count
|
2019-02-14 18:05:35 +00:00
|
|
|
@rack_uncaught_errors_count ||= ::Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Request handling uncaught errors count')
|
2017-07-12 22:46:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.http_request_duration_seconds
|
2019-02-14 18:05:35 +00:00
|
|
|
@http_request_duration_seconds ||= ::Gitlab::Metrics.histogram(:http_request_duration_seconds, 'Request handling execution time',
|
2017-07-12 22:46:17 +00:00
|
|
|
{}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
|
|
|
|
end
|
|
|
|
|
2020-04-06 15:10:04 +00:00
|
|
|
def self.http_health_requests_total
|
|
|
|
@http_health_requests_total ||= ::Gitlab::Metrics.counter(:http_health_requests_total, 'Health endpoint request count')
|
|
|
|
end
|
|
|
|
|
2020-10-29 03:09:25 +00:00
|
|
|
def self.initialize_metrics
|
|
|
|
# This initialization is done to avoid gaps in scraped metrics after
|
|
|
|
# restarts. It makes sure all counters/histograms are available at
|
|
|
|
# process start.
|
|
|
|
#
|
|
|
|
# For example `rate(http_requests_total{status="500"}[1m])` would return
|
|
|
|
# no data until the first 500 error would occur.
|
|
|
|
|
|
|
|
# The list of feature categories is currently not needed by the application
|
|
|
|
# anywhere else. So no need to keep these in memory forever.
|
|
|
|
# Doing it here, means we're only reading the file on boot.
|
|
|
|
feature_categories = YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).map(&:strip).uniq << FEATURE_CATEGORY_DEFAULT
|
|
|
|
|
|
|
|
HTTP_METHODS.each do |method, statuses|
|
2020-10-20 09:08:43 +00:00
|
|
|
http_request_duration_seconds.get({ method: method })
|
2020-10-29 03:09:25 +00:00
|
|
|
|
|
|
|
statuses.product(feature_categories) do |status, feature_category|
|
|
|
|
http_requests_total.get({ method: method, status: status, feature_category: feature_category })
|
|
|
|
end
|
2019-10-02 21:06:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-12 22:46:17 +00:00
|
|
|
def call(env)
|
|
|
|
method = env['REQUEST_METHOD'].downcase
|
2020-10-29 03:09:25 +00:00
|
|
|
method = 'INVALID' unless HTTP_METHODS.key?(method)
|
2017-07-12 22:46:17 +00:00
|
|
|
started = Time.now.to_f
|
2020-09-28 15:09:44 +00:00
|
|
|
health_endpoint = health_endpoint?(env['PATH_INFO'])
|
2020-10-07 18:08:34 +00:00
|
|
|
status = 'undefined'
|
2020-10-15 09:08:41 +00:00
|
|
|
feature_category = nil
|
2020-04-06 15:10:04 +00:00
|
|
|
|
2017-07-12 22:46:17 +00:00
|
|
|
begin
|
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
|
|
|
|
elapsed = Time.now.to_f - started
|
2020-10-15 09:08:41 +00:00
|
|
|
feature_category = headers&.fetch(FEATURE_CATEGORY_HEADER, nil)
|
2020-09-28 15:09:44 +00:00
|
|
|
|
|
|
|
unless health_endpoint
|
2020-10-20 09:08:43 +00:00
|
|
|
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method }, elapsed)
|
2020-09-28 15:09:44 +00:00
|
|
|
end
|
2017-07-12 22:46:17 +00:00
|
|
|
|
|
|
|
[status, headers, body]
|
|
|
|
rescue
|
|
|
|
RequestsRackMiddleware.rack_uncaught_errors_count.increment
|
|
|
|
raise
|
2020-10-07 18:08:34 +00:00
|
|
|
ensure
|
|
|
|
if health_endpoint
|
2020-10-29 03:09:25 +00:00
|
|
|
RequestsRackMiddleware.http_health_requests_total.increment(status: status.to_s, method: method)
|
2020-10-07 18:08:34 +00:00
|
|
|
else
|
2020-10-29 03:09:25 +00:00
|
|
|
RequestsRackMiddleware.http_requests_total.increment(
|
|
|
|
status: status.to_s,
|
|
|
|
method: method,
|
|
|
|
feature_category: feature_category.presence || FEATURE_CATEGORY_DEFAULT
|
|
|
|
)
|
2020-10-07 18:08:34 +00:00
|
|
|
end
|
2017-07-12 22:46:17 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-06 15:10:04 +00:00
|
|
|
|
|
|
|
def health_endpoint?(path)
|
|
|
|
return false if path.blank?
|
|
|
|
|
|
|
|
HEALTH_ENDPOINT.match?(CGI.unescape(path))
|
|
|
|
end
|
2017-07-12 22:46:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|