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: 15dd6a59e19fe3006d7798fe20f51a9535c4fc46 (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
49
50
51
52
# frozen_string_literal: true

module Gitlab
  module Cluster
    class RackTimeoutObserver
      include ActionView::Helpers::SanitizeHelper
      TRANSITION_STATES = %i(ready active).freeze

      def initialize
        @counter = Gitlab::Metrics.counter(:rack_requests_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
        return if TRANSITION_STATES.include?(info.state)

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

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

        {
          controller: sanitize(params['controller']),
          action: sanitize(params['action']),
          route: sanitize(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