Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rack_timeout_observer.rb « cluster « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2bc006b80110ef8f9dfdbe88b7f03a04247a87dc (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
46
47
48
# frozen_string_literal: true

module Gitlab
  module Cluster
    class RackTimeoutObserver
      def initialize
        @counter = Gitlab::Metrics.counter(:rack_state_total, 'Number of requests in a given rack state')
      end

      # returns the Proc to be used as the observer callback block
      def callback
        method(:log_timeout_exception)
      end

      private

      def log_timeout_exception(env)
        info = env[::Rack::Timeout::ENV_INFO_KEY]
        return unless info

        @counter.increment(labels(info, env))
      end

      def labels(info, env)
        params = controller_params(env) || grape_params(env) || {}

        {
          controller: params['controller'],
          action: params['action'],
          route: params['route'],
          state: info.state
        }
      end

      def controller_params(env)
        env['action_dispatch.request.parameters']
      end

      def grape_params(env)
        endpoint = env[Grape::Env::API_ENDPOINT]
        route = endpoint&.route&.pattern&.origin
        return unless route

        { 'route' => route }
      end
    end
  end
end