Class: Gitlab::Metrics::WebTransaction

Inherits:
Transaction show all
Defined in:
lib/gitlab/metrics/web_transaction.rb

Overview

Exclusive transaction-type metrics for web servers (including Web/Api/Git fleet). One instance of this class is created for each request going through the Rack metric middleware. Any metrics dispatched with this instance include metadata such as controller, action, feature category, etc.

Constant Summary collapse

THREAD_KEY =
:_gitlab_metrics_transaction
BASE_LABEL_KEYS =
%i[controller action feature_category].freeze
CONTROLLER_KEY =
'action_controller.instance'
ENDPOINT_KEY =
'api.endpoint'
ALLOWED_SUFFIXES =
Set.new(%w[json js atom rss xml zip])
SMALL_BUCKETS =
[0.1, 0.25, 0.5, 1.0, 2.5, 5.0].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, #method_call_for, #observe, #set

Constructor Details

#initialize(env) ⇒ WebTransaction

Returns a new instance of WebTransaction.



36
37
38
39
# File 'lib/gitlab/metrics/web_transaction.rb', line 36

def initialize(env)
  super()
  @env = env
end

Class Method Details

.currentObject



20
21
22
# File 'lib/gitlab/metrics/web_transaction.rb', line 20

def current
  Thread.current[THREAD_KEY]
end

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/metrics/web_transaction.rb', line 24

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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/metrics/web_transaction.rb', line 57

def labels
  return @labels if @labels

  # memoize transaction labels only source env variables were present
  @labels = if @env[CONTROLLER_KEY]
              labels_from_controller || {}
            elsif @env[ENDPOINT_KEY]
              labels_from_endpoint || {}
            end

  @labels || {}
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/metrics/web_transaction.rb', line 41

def run
  Thread.current[THREAD_KEY] = self

  started_at = System.monotonic_time

  status, _, _ = retval = yield

  finished_at = System.monotonic_time
  duration = finished_at - started_at
  record_duration_if_needed(status, duration)

  retval
ensure
  Thread.current[THREAD_KEY] = nil
end