requests_rack_middleware.rb 4.13 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Metrics
    class RequestsRackMiddleware
6 7 8 9 10 11 12 13 14
      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
15

16
      HEALTH_ENDPOINT = %r{^/-/(liveness|readiness|health|metrics)/?$}.freeze
Ryan Cobb's avatar
Ryan Cobb committed
17

18 19
      FEATURE_CATEGORY_DEFAULT = 'unknown'

20 21 22 23 24 25 26 27 28
      # These were the top 5 categories at a point in time, chosen as a
      # reasonable default. If we initialize every category we'll end up
      # with an explosion in unused metric combinations, but we want the
      # most common ones to be always present.
      FEATURE_CATEGORIES_TO_INITIALIZE = ['authentication_and_authorization',
                                          'code_review', 'continuous_integration',
                                          'not_owned', 'source_code_management',
                                          FEATURE_CATEGORY_DEFAULT].freeze

29 30 31 32
      def initialize(app)
        @app = app
      end

33
      def self.http_requests_total
34
        ::Gitlab::Metrics.counter(:http_requests_total, 'Request count')
35 36 37
      end

      def self.rack_uncaught_errors_count
38
        ::Gitlab::Metrics.counter(:rack_uncaught_errors_total, 'Request handling uncaught errors count')
39 40 41
      end

      def self.http_request_duration_seconds
42 43
        ::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])
44 45
      end

Ryan Cobb's avatar
Ryan Cobb committed
46
      def self.http_health_requests_total
47
        ::Gitlab::Metrics.counter(:http_health_requests_total, 'Health endpoint request count')
Ryan Cobb's avatar
Ryan Cobb committed
48 49
      end

50 51 52 53 54 55 56 57
      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.
        HTTP_METHODS.each do |method, statuses|
58
          http_request_duration_seconds.get({ method: method })
59

60
          statuses.product(FEATURE_CATEGORIES_TO_INITIALIZE) do |status, feature_category|
61 62
            http_requests_total.get({ method: method, status: status, feature_category: feature_category })
          end
63 64 65
        end
      end

66 67
      def call(env)
        method = env['REQUEST_METHOD'].downcase
68
        method = 'INVALID' unless HTTP_METHODS.key?(method)
69
        started = Gitlab::Metrics::System.monotonic_time
70
        health_endpoint = health_endpoint?(env['PATH_INFO'])
71
        status = 'undefined'
Ryan Cobb's avatar
Ryan Cobb committed
72

73 74 75
        begin
          status, headers, body = @app.call(env)

76
          elapsed = Gitlab::Metrics::System.monotonic_time - started
77

78
          if !health_endpoint && Gitlab::Metrics.record_duration_for_status?(status)
79
            RequestsRackMiddleware.http_request_duration_seconds.observe({ method: method }, elapsed)
80
          end
81 82

          [status, headers, body]
83
        rescue StandardError
84 85
          RequestsRackMiddleware.rack_uncaught_errors_count.increment
          raise
86 87
        ensure
          if health_endpoint
88
            RequestsRackMiddleware.http_health_requests_total.increment(status: status.to_s, method: method)
89
          else
90 91 92 93 94
            RequestsRackMiddleware.http_requests_total.increment(
              status: status.to_s,
              method: method,
              feature_category: feature_category.presence || FEATURE_CATEGORY_DEFAULT
            )
95
          end
96 97
        end
      end
Ryan Cobb's avatar
Ryan Cobb committed
98 99 100 101 102 103

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

        HEALTH_ENDPOINT.match?(CGI.unescape(path))
      end
104 105 106 107

      def feature_category
        ::Gitlab::ApplicationContext.current_context_attribute(:feature_category)
      end
108 109 110
    end
  end
end