Class: Gitlab::Metrics::BackgroundTransaction

Inherits:
Transaction
  • Object
show all
Defined in:
lib/gitlab/metrics/background_transaction.rb

Overview

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.

Constant Summary collapse

THREAD_KEY =
:_gitlab_metrics_background_transaction
BASE_LABEL_KEYS =
%i[queue endpoint_id feature_category].freeze

Constants inherited from Transaction

Transaction::EVENT_SERIES, Transaction::FILTERED_LABEL_KEYS

Instance Attribute Summary

Attributes inherited from Transaction

#method

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Transaction

#add_event, #filter_labels, #increment, #initialize, #method_call_for, #observe, #set

Constructor Details

This class inherits a constructor from Gitlab::Metrics::Transaction

Class Method Details

.currentObject



14
15
16
# File 'lib/gitlab/metrics/background_transaction.rb', line 14

def current
  Thread.current[THREAD_KEY]
end

.prometheus_metric(name, type, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/metrics/background_transaction.rb', line 18

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

Instance Method Details

#labelsObject



38
39
40
41
42
43
44
# File 'lib/gitlab/metrics/background_transaction.rb', line 38

def labels
  @labels ||= {
    endpoint_id: endpoint_id,
    feature_category: feature_category,
    queue: queue
  }
end

#runObject



30
31
32
33
34
35
36
# File 'lib/gitlab/metrics/background_transaction.rb', line 30

def run
  Thread.current[THREAD_KEY] = self

  yield
ensure
  Thread.current[THREAD_KEY] = nil
end