debian-mirror-gitlab/lib/gitlab/metrics/requests_rack_middleware.rb

79 lines
2.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module Metrics
class RequestsRackMiddleware
2021-01-03 14:25:43 +05:30
HTTP_METHODS = %w(delete get head options patch post put).to_set.freeze
2019-12-21 20:55:43 +05:30
2020-04-22 19:07:51 +05:30
HEALTH_ENDPOINT = /^\/-\/(liveness|readiness|health|metrics)\/?$/.freeze
2021-01-03 14:25:43 +05:30
FEATURE_CATEGORY_HEADER = 'X-Gitlab-Feature-Category'
FEATURE_CATEGORY_DEFAULT = 'unknown'
2017-09-10 17:25:29 +05:30
def initialize(app)
@app = app
end
def self.http_request_total
2019-07-07 11:18:12 +05:30
@http_request_total ||= ::Gitlab::Metrics.counter(:http_requests_total, 'Request count')
2017-09-10 17:25:29 +05:30
end
def self.rack_uncaught_errors_count
2019-07-07 11:18:12 +05:30
@rack_uncaught_errors_count ||= ::Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Request handling uncaught errors count')
2017-09-10 17:25:29 +05:30
end
def self.http_request_duration_seconds
2019-07-07 11:18:12 +05:30
@http_request_duration_seconds ||= ::Gitlab::Metrics.histogram(:http_request_duration_seconds, 'Request handling execution time',
2017-09-10 17:25:29 +05:30
{}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
end
2020-04-22 19:07:51 +05:30
def self.http_health_requests_total
@http_health_requests_total ||= ::Gitlab::Metrics.counter(:http_health_requests_total, 'Health endpoint request count')
end
2019-12-21 20:55:43 +05:30
def self.initialize_http_request_duration_seconds
2021-01-03 14:25:43 +05:30
HTTP_METHODS.each do |method|
http_request_duration_seconds.get({ method: method })
2019-12-21 20:55:43 +05:30
end
end
2017-09-10 17:25:29 +05:30
def call(env)
method = env['REQUEST_METHOD'].downcase
2021-01-03 14:25:43 +05:30
method = 'INVALID' unless HTTP_METHODS.include?(method)
2017-09-10 17:25:29 +05:30
started = Time.now.to_f
2021-01-03 14:25:43 +05:30
health_endpoint = health_endpoint?(env['PATH_INFO'])
status = 'undefined'
feature_category = nil
2020-04-22 19:07:51 +05:30
2017-09-10 17:25:29 +05:30
begin
status, headers, body = @app.call(env)
elapsed = Time.now.to_f - started
2021-01-03 14:25:43 +05:30
feature_category = headers&.fetch(FEATURE_CATEGORY_HEADER, nil)
unless health_endpoint
RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method }, elapsed)
end
2017-09-10 17:25:29 +05:30
[status, headers, body]
rescue
RequestsRackMiddleware.rack_uncaught_errors_count.increment
raise
2021-01-03 14:25:43 +05:30
ensure
if health_endpoint
RequestsRackMiddleware.http_health_requests_total.increment(status: status, method: method)
else
RequestsRackMiddleware.http_request_total.increment(status: status, method: method, feature_category: feature_category || FEATURE_CATEGORY_DEFAULT)
end
2017-09-10 17:25:29 +05:30
end
end
2020-04-22 19:07:51 +05:30
def health_endpoint?(path)
return false if path.blank?
HEALTH_ENDPOINT.match?(CGI.unescape(path))
end
2017-09-10 17:25:29 +05:30
end
end
end