summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/rails_slis.rb
blob: 69e0c1e9fde7308b36cca62c8dc1c674efd2c120 (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 Metrics
    module RailsSlis
      class << self
        def request_apdex_counters_enabled?
          Feature.enabled?(:request_apdex_counters)
        end

        def initialize_request_slis_if_needed!
          return unless request_apdex_counters_enabled?
          return if Gitlab::Metrics::Sli.initialized?(:rails_request_apdex)

          Gitlab::Metrics::Sli.initialize_sli(:rails_request_apdex, possible_request_labels)
        end

        def request_apdex
          Gitlab::Metrics::Sli[:rails_request_apdex]
        end

        private

        def possible_request_labels
          possible_controller_labels + possible_api_labels
        end

        def possible_api_labels
          Gitlab::RequestEndpoints.all_api_endpoints.map do |route|
            endpoint_id = API::Base.endpoint_id_for_route(route)
            route_class = route.app.options[:for]
            feature_category = route_class.feature_category_for_app(route.app)

            {
              endpoint_id: endpoint_id,
              feature_category: feature_category
            }
          end
        end

        def possible_controller_labels
          Gitlab::RequestEndpoints.all_controller_actions.map do |controller, action|
            {
              endpoint_id: controller.endpoint_id_for_action(action),
              feature_category: controller.feature_category_for_action(action)
            }
          end
        end
      end
    end
  end
end