Welcome to mirror list, hosted at ThFree Co, Russian Federation.

background_transaction.rb « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3dda68bf93fb5c8de537f798f0f8933c71c77023 (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    class BackgroundTransaction < Transaction
      # Separate web transaction instance and background transaction instance
      BACKGROUND_THREAD_KEY = :_gitlab_metrics_background_transaction
      BACKGROUND_BASE_LABEL_KEYS = %i(endpoint_id feature_category).freeze

      class << self
        def current
          Thread.current[BACKGROUND_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 BACKGROUND_BASE_LABEL_KEYS | (label_keys - ::Gitlab::Metrics::Transaction::FILTERED_LABEL_KEYS)
          end
        end
      end

      def run
        Thread.current[BACKGROUND_THREAD_KEY] = self

        yield
      ensure
        Thread.current[BACKGROUND_THREAD_KEY] = nil
      end

      def labels
        @labels ||= {
          endpoint_id: current_context&.get_attribute(:caller_id),
          feature_category: current_context&.get_attribute(:feature_category)
        }
      end

      private

      def current_context
        Labkit::Context.current
      end
    end
  end
end