blob: 333fc63ebef9f9394ca9118569e2ede983c0f23c (
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
|
# frozen_string_literal: true
module Gitlab
module Metrics
module Subscribers
class LoadBalancing < ActiveSupport::Subscriber
attach_to :load_balancing
PROMETHEUS_COUNTER = :gitlab_transaction_caught_up_replica_pick_count_total
LOG_COUNTERS = { true => :caught_up_replica_pick_ok, false => :caught_up_replica_pick_fail }.freeze
def caught_up_replica_pick(event)
return unless Gitlab::SafeRequestStore.active? && ::Gitlab::Database::LoadBalancing.enable?
result = event.payload[:result]
counter_name = counter(result)
increment(counter_name)
end
# we want to update Prometheus counter after the controller/action are set
def web_transaction_completed(_event)
return unless Gitlab::SafeRequestStore.active? && ::Gitlab::Database::LoadBalancing.enable?
LOG_COUNTERS.keys.each { |result| increment_prometheus_for_result_label(result) }
end
def self.load_balancing_payload
return {} unless Gitlab::SafeRequestStore.active? && ::Gitlab::Database::LoadBalancing.enable?
{}.tap do |payload|
LOG_COUNTERS.values.each do |counter|
value = Gitlab::SafeRequestStore[counter]
payload[counter] = value.to_i if value
end
end
end
private
def increment(counter)
Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1
end
def increment_prometheus_for_result_label(label_value)
counter_name = counter(label_value)
return unless (counter_value = Gitlab::SafeRequestStore[counter_name])
increment_prometheus(labels: { result: label_value }, value: counter_value.to_i)
end
def increment_prometheus(labels:, value:)
current_transaction&.increment(PROMETHEUS_COUNTER, value, labels) do
docstring 'Caught up replica pick result'
label_keys labels.keys
end
end
def counter(result)
LOG_COUNTERS[result]
end
def current_transaction
::Gitlab::Metrics::WebTransaction.current
end
end
end
end
end
|