blob: 7ffec7b01415ea4e7394964a28ef30b938910a9b (
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
|
# frozen_string_literal: true
module Gitlab
module Metrics
# Exclusive transaction-type metrics for background jobs (Sidekiq). One
# instance of this class is created for each job going through the Sidekiq
# metric middleware. Any metrics dispatched with this instance include
# metadata such as endpoint_id, queue, and feature category.
class BackgroundTransaction < Transaction
THREAD_KEY = :_gitlab_metrics_background_transaction
BASE_LABEL_KEYS = %i[queue endpoint_id feature_category].freeze
class << self
def current
Thread.current[THREAD_KEY]
end
def prometheus_metric(name, type, &block)
fetch_metric(type, name) do
# set default metric options
docstring "#{name.to_s.humanize} #{type}"
evaluate(&block)
# always filter sensitive labels and merge with base ones
label_keys BASE_LABEL_KEYS | (label_keys - ::Gitlab::Metrics::Transaction::FILTERED_LABEL_KEYS)
end
end
end
def run
Thread.current[THREAD_KEY] = self
yield
ensure
Thread.current[THREAD_KEY] = nil
end
def labels
@labels ||= {
endpoint_id: endpoint_id,
feature_category: feature_category,
queue: queue
}
end
private
def current_context
Labkit::Context.current
end
def feature_category
current_context&.get_attribute(:feature_category)
end
def endpoint_id
current_context&.get_attribute(:caller_id)
end
def queue
worker_class = endpoint_id.to_s.safe_constantize
return if worker_class.blank? || !worker_class.respond_to?(:queue)
worker_class.queue.to_s
end
end
end
end
|