2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
2021-12-11 22:18:48 +05:30
|
|
|
# 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.
|
2021-04-17 20:07:23 +05:30
|
|
|
class BackgroundTransaction < Transaction
|
2021-12-11 22:18:48 +05:30
|
|
|
THREAD_KEY = :_gitlab_metrics_background_transaction
|
|
|
|
BASE_LABEL_KEYS = %i(queue endpoint_id feature_category).freeze
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
class << self
|
|
|
|
def current
|
2021-12-11 22:18:48 +05:30
|
|
|
Thread.current[THREAD_KEY]
|
2021-04-17 20:07:23 +05:30
|
|
|
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
|
2021-12-11 22:18:48 +05:30
|
|
|
label_keys BASE_LABEL_KEYS | (label_keys - ::Gitlab::Metrics::Transaction::FILTERED_LABEL_KEYS)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2021-12-11 22:18:48 +05:30
|
|
|
Thread.current[THREAD_KEY] = self
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
2021-12-11 22:18:48 +05:30
|
|
|
Thread.current[THREAD_KEY] = nil
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
|
|
|
@labels ||= {
|
2021-04-29 21:17:54 +05:30
|
|
|
endpoint_id: endpoint_id,
|
|
|
|
feature_category: feature_category,
|
|
|
|
queue: queue
|
2021-04-17 20:07:23 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_context
|
|
|
|
Labkit::Context.current
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
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
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|