Class: Gitlab::Metrics::RequestsRackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/metrics/requests_rack_middleware.rb

Constant Summary collapse

HTTP_METHODS =
{
  "delete" => %w(200 202 204 303 400 401 403 404 410 422 500 503),
  "get" => %w(200 204 301 302 303 304 307 400 401 403 404 410 412 422 429 500 503),
  "head" => %w(200 204 301 302 303 304 400 401 403 404 410 429 500 503),
  "options" => %w(200 404),
  "patch" => %w(200 202 204 400 403 404 409 416 422 500),
  "post" => %w(200 201 202 204 301 302 303 304 400 401 403 404 406 409 410 412 413 415 422 429 500 503),
  "propfind" => %w(404),
  "put" => %w(200 202 204 400 401 403 404 405 406 409 410 415 422 500),
  "report" =>  %w(404)
}.freeze
HEALTH_ENDPOINT =
/^\/-\/(liveness|readiness|health|metrics)\/?$/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestsRackMiddleware

Returns a new instance of RequestsRackMiddleware.


20
21
22
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 20

def initialize(app)
  @app = app
end

Class Method Details

.http_health_requests_totalObject


37
38
39
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 37

def self.http_health_requests_total
  @http_health_requests_total ||= ::Gitlab::Metrics.counter(:http_health_requests_total, 'Health endpoint request count')
end

.http_request_duration_secondsObject


32
33
34
35
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 32

def self.http_request_duration_seconds
  @http_request_duration_seconds ||= ::Gitlab::Metrics.histogram(:http_request_duration_seconds, 'Request handling execution time',
                                                     {}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 2.5, 5, 10, 25])
end

.http_request_totalObject


24
25
26
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 24

def self.http_request_total
  @http_request_total ||= ::Gitlab::Metrics.counter(:http_requests_total, 'Request count')
end

.initialize_http_request_duration_secondsObject


41
42
43
44
45
46
47
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 41

def self.initialize_http_request_duration_seconds
  HTTP_METHODS.each do |method, statuses|
    statuses.each do |status|
      http_request_duration_seconds.get({ method: method, status: status.to_s })
    end
  end
end

.rack_uncaught_errors_countObject


28
29
30
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 28

def self.rack_uncaught_errors_count
  @rack_uncaught_errors_count ||= ::Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Request handling uncaught errors count')
end

Instance Method Details

#call(env) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 49

def call(env)
  method = env['REQUEST_METHOD'].downcase
  started = Time.now.to_f

  begin
    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)

    elapsed = Time.now.to_f - started
    RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method, status: status.to_s }, elapsed)

    [status, headers, body]
  rescue
    RequestsRackMiddleware.rack_uncaught_errors_count.increment
    raise
  end
end

#health_endpoint?(path) ⇒ Boolean

Returns:

  • (Boolean)

72
73
74
75
76
# File 'lib/gitlab/metrics/requests_rack_middleware.rb', line 72

def health_endpoint?(path)
  return false if path.blank?

  HEALTH_ENDPOINT.match?(CGI.unescape(path))
end