summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/sli.rb
blob: fcd893b675fac2682e3375bc8419c88cb0dcefc1 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Sli
      COUNTER_PREFIX = 'gitlab_sli'

      module ClassMethods
        INITIALIZATION_MUTEX = Mutex.new

        def [](name)
          known_slis[name] || initialize_sli(name, [])
        end

        def initialize_sli(name, possible_label_combinations)
          INITIALIZATION_MUTEX.synchronize do
            next known_slis[name] if initialized?(name)

            sli = new(name)
            sli.initialize_counters(possible_label_combinations)
            known_slis[name] = sli
          end
        end

        def initialized?(name)
          known_slis.key?(name) && known_slis[name].initialized?
        end

        private

        def known_slis
          @known_slis ||= {}
        end
      end

      def self.included(mod)
        mod.extend(ClassMethods)
      end

      attr_reader :name

      def initialize(name)
        @name = name
        @initialized_with_combinations = false
      end

      def initialize_counters(possible_label_combinations)
        # This module is effectively an abstract class
        @initialized_with_combinations = possible_label_combinations.any? # rubocop:disable Gitlab/ModuleWithInstanceVariables
        possible_label_combinations.each do |label_combination|
          total_counter.get(label_combination)
          numerator_counter.get(label_combination)
        end
      end

      def increment(labels:, increment_numerator:)
        total_counter.increment(labels)
        numerator_counter.increment(labels) if increment_numerator
      end

      def initialized?
        @initialized_with_combinations
      end

      private

      def total_counter
        prometheus.counter(counter_name('total'), "Total number of measurements for #{name}")
      end

      def counter_name(suffix)
        :"#{COUNTER_PREFIX}:#{name}_#{self.class.name.demodulize.underscore}:#{suffix}"
      end

      def prometheus
        Gitlab::Metrics
      end

      class Apdex
        include Sli

        def increment(labels:, success:)
          super(labels: labels, increment_numerator: success)
        end

        private

        def numerator_counter
          prometheus.counter(counter_name('success_total'), "Number of successful measurements for #{name}")
        end
      end

      class ErrorRate
        include Sli

        def increment(labels:, error:)
          super(labels: labels, increment_numerator: error)
        end

        private

        def numerator_counter
          prometheus.counter(counter_name('error_total'), "Number of error measurements for #{name}")
        end
      end
    end
  end
end