2021-09-21 14:11:18 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
2022-05-17 11:09:01 -04:00
|
|
|
module Sli
|
2021-09-21 14:11:18 -04:00
|
|
|
COUNTER_PREFIX = 'gitlab_sli'
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
module ClassMethods
|
2021-09-21 14:11:18 -04:00
|
|
|
INITIALIZATION_MUTEX = Mutex.new
|
|
|
|
|
|
|
|
def [](name)
|
|
|
|
known_slis[name] || initialize_sli(name, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_sli(name, possible_label_combinations)
|
|
|
|
INITIALIZATION_MUTEX.synchronize do
|
2022-05-17 11:09:01 -04:00
|
|
|
next known_slis[name] if initialized?(name)
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
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
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
def self.included(mod)
|
|
|
|
mod.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
@initialized_with_combinations = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_counters(possible_label_combinations)
|
2022-05-17 11:09:01 -04:00
|
|
|
# This module is effectively an abstract class
|
|
|
|
@initialized_with_combinations = possible_label_combinations.any? # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2021-09-21 14:11:18 -04:00
|
|
|
possible_label_combinations.each do |label_combination|
|
2022-06-28 11:09:17 -04:00
|
|
|
legacy_total_counter.get(label_combination)
|
|
|
|
legacy_numerator_counter.get(label_combination)
|
|
|
|
|
|
|
|
if ::Feature.enabled?(:gitlab_sli_new_counters)
|
|
|
|
total_counter.get(label_combination)
|
|
|
|
numerator_counter.get(label_combination)
|
|
|
|
end
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
def increment(labels:, increment_numerator:)
|
2022-06-28 11:09:17 -04:00
|
|
|
legacy_total_counter.increment(labels)
|
|
|
|
legacy_numerator_counter.increment(labels) if increment_numerator
|
|
|
|
|
|
|
|
if ::Feature.enabled?(:gitlab_sli_new_counters)
|
|
|
|
total_counter.increment(labels)
|
|
|
|
numerator_counter.increment(labels) if increment_numerator
|
|
|
|
end
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialized?
|
|
|
|
@initialized_with_combinations
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def total_counter
|
2022-06-28 11:09:17 -04:00
|
|
|
prometheus.counter(counter_name('total', '_'), "Total number of measurements for #{name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_total_counter
|
|
|
|
prometheus.counter(counter_name('total', ':'), "Total number of measurements for #{name}")
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
def prometheus
|
|
|
|
Gitlab::Metrics
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
class Apdex
|
|
|
|
include Sli
|
|
|
|
|
|
|
|
def increment(labels:, success:)
|
|
|
|
super(labels: labels, increment_numerator: success)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-28 11:09:17 -04:00
|
|
|
def counter_name(suffix, separator)
|
|
|
|
[COUNTER_PREFIX, "#{name}_apdex", suffix].join(separator).to_sym
|
2022-06-10 08:09:36 -04:00
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
def numerator_counter
|
2022-06-28 11:09:17 -04:00
|
|
|
prometheus.counter(counter_name('success_total', '_'), "Number of successful measurements for #{name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_numerator_counter
|
|
|
|
prometheus.counter(counter_name('success_total', ':'), "Legacy number of successful measurements for #{name}")
|
2022-05-17 11:09:01 -04:00
|
|
|
end
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
class ErrorRate
|
|
|
|
include Sli
|
|
|
|
|
|
|
|
def increment(labels:, error:)
|
|
|
|
super(labels: labels, increment_numerator: error)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-28 11:09:17 -04:00
|
|
|
def counter_name(suffix, separator)
|
|
|
|
[COUNTER_PREFIX, name, suffix].join(separator).to_sym
|
2022-06-10 08:09:36 -04:00
|
|
|
end
|
|
|
|
|
2022-05-17 11:09:01 -04:00
|
|
|
def numerator_counter
|
2022-06-28 11:09:17 -04:00
|
|
|
prometheus.counter(counter_name('error_total', '_'), "Number of error measurements for #{name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_numerator_counter
|
|
|
|
prometheus.counter(counter_name('error_total', ':'), "Number of error measurements for #{name}")
|
2022-05-17 11:09:01 -04:00
|
|
|
end
|
2021-09-21 14:11:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|