summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/connection_rack_middleware.rb
blob: b3da360be8f3500aaa3fb28bf6fff5e98ba92baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Gitlab
  module Metrics
    class ConnectionRackMiddleware
      def initialize(app)
        @app = app
      end

      def self.rack_request_count
        @rack_request_count ||= Gitlab::Metrics.counter(:rack_request, 'Rack request count')
      end

      def self.rack_response_count
        @rack_response_count ||= Gitlab::Metrics.counter(:rack_response, 'Rack response count')
      end

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

      def self.rack_execution_time
        @rack_execution_time ||= Gitlab::Metrics.histogram(:rack_execution_time, 'Rack connection handling execution time',
                                                           {}, [0.05, 0.1, 0.25, 0.5, 0.7, 1, 1.5, 2, 2.5, 3, 5, 7, 10])
      end

      def call(env)
        method = env['REQUEST_METHOD'].downcase
        started = Time.now.to_f
        begin
          ConnectionRackMiddleware.rack_request_count.increment(method: method)

          status, headers, body = @app.call(env)

          ConnectionRackMiddleware.rack_response_count.increment(method: method, status: status)
          [status, headers, body]
        rescue
          ConnectionRackMiddleware.rack_uncaught_errors_count.increment
          raise
        ensure
          elapsed = Time.now.to_f - started
          ConnectionRackMiddleware.rack_execution_time.observe({}, elapsed)
        end
      end
    end
  end
end