2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Class for storing metrics information of a single transaction.
|
2017-10-19 05:24:15 -04:00
|
|
|
class Transaction
|
2018-01-29 06:33:08 -05:00
|
|
|
include Gitlab::Metrics::Methods
|
2018-01-16 09:47:07 -05:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
# base label keys shared among all transactions
|
|
|
|
BASE_LABEL_KEYS = %i(controller action feature_category).freeze
|
2019-04-04 06:56:12 -04:00
|
|
|
# labels that potentially contain sensitive information and will be filtered
|
2020-07-28 08:09:49 -04:00
|
|
|
FILTERED_LABEL_KEYS = %i(branch path).freeze
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
THREAD_KEY = :_gitlab_metrics_transaction
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
SMALL_BUCKETS = [0.1, 0.25, 0.5, 1.0, 2.5, 5.0].freeze
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
# The series to store events (e.g. Git pushes) in.
|
2019-08-31 15:25:25 -04:00
|
|
|
EVENT_SERIES = 'events'
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2020-06-09 11:08:05 -04:00
|
|
|
attr_reader :method
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
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 - FILTERED_LABEL_KEYS)
|
|
|
|
end
|
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
def initialize
|
2017-10-16 09:57:33 -04:00
|
|
|
@methods = {}
|
|
|
|
|
|
|
|
@started_at = nil
|
|
|
|
@finished_at = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def duration
|
|
|
|
@finished_at ? (@finished_at - @started_at) : 0.0
|
|
|
|
end
|
|
|
|
|
2019-10-16 02:07:06 -04:00
|
|
|
def thread_cpu_duration
|
|
|
|
System.thread_cpu_duration(@thread_cputime_start)
|
|
|
|
end
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
def run
|
|
|
|
Thread.current[THREAD_KEY] = self
|
|
|
|
|
|
|
|
@started_at = System.monotonic_time
|
2019-10-16 02:07:06 -04:00
|
|
|
@thread_cputime_start = System.thread_cpu_time
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@finished_at = System.monotonic_time
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
observe(:gitlab_transaction_cputime_seconds, thread_cpu_duration) do
|
|
|
|
buckets SMALL_BUCKETS
|
|
|
|
end
|
|
|
|
observe(:gitlab_transaction_duration_seconds, duration) do
|
|
|
|
buckets SMALL_BUCKETS
|
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
Thread.current[THREAD_KEY] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tracks a business level event
|
|
|
|
#
|
|
|
|
# Business level events including events such as Git pushes, Emails being
|
|
|
|
# sent, etc.
|
|
|
|
#
|
|
|
|
# event_name - The name of the event (e.g. "git_push").
|
|
|
|
# tags - A set of tags to attach to the event.
|
|
|
|
def add_event(event_name, tags = {})
|
2020-07-28 08:09:49 -04:00
|
|
|
event_name = "gitlab_transaction_event_#{event_name}_total".to_sym
|
|
|
|
metric = self.class.prometheus_metric(event_name, :counter) do
|
|
|
|
label_keys tags.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
metric.increment(filter_labels(tags))
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a MethodCall object for the given name.
|
|
|
|
def method_call_for(name, module_name, method_name)
|
|
|
|
unless method = @methods[name]
|
|
|
|
@methods[name] = method = MethodCall.new(name, module_name, method_name, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
method
|
|
|
|
end
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
# Increment counter metric
|
|
|
|
#
|
|
|
|
# It will initialize the metric if metric is not found
|
|
|
|
#
|
|
|
|
# block - if provided can be used to initialize metric with custom options (docstring, labels, with_feature)
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# ```
|
|
|
|
# transaction.increment(:mestric_name, 1, { docstring: 'Custom title', base_labels: {sane: 'yes'} } ) do
|
|
|
|
#
|
|
|
|
# transaction.increment(:mestric_name, 1) do
|
|
|
|
# docstring 'Custom title'
|
|
|
|
# label_keys %i(sane)
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
def increment(name, value = 1, labels = {}, &block)
|
|
|
|
counter = self.class.prometheus_metric(name, :counter, &block)
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
counter.increment(filter_labels(labels), value)
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
# Set gauge metric
|
|
|
|
#
|
|
|
|
# It will initialize the metric if metric is not found
|
|
|
|
#
|
|
|
|
# block - if provided, it can be used to initialize metric with custom options (docstring, labels, with_feature, multiprocess_mode)
|
|
|
|
# - multiprocess_mode is :all by default
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# ```
|
|
|
|
# transaction.set(:mestric_name, 1) do
|
|
|
|
# multiprocess_mode :livesum
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
def set(name, value, labels = {}, &block)
|
|
|
|
gauge = self.class.prometheus_metric(name, :gauge, &block)
|
2017-10-17 10:06:52 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
gauge.set(filter_labels(labels), value)
|
2019-10-16 02:07:06 -04:00
|
|
|
end
|
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
# Observe histogram metric
|
|
|
|
#
|
|
|
|
# It will initialize the metric if metric is not found
|
|
|
|
#
|
|
|
|
# block - if provided, it can be used to initialize metric with custom options (docstring, labels, with_feature, buckets)
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# ```
|
|
|
|
# transaction.observe(:mestric_name, 1) do
|
|
|
|
# buckets [100, 1000, 10000, 100000, 1000000, 10000000]
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
def observe(name, value, labels = {}, &block)
|
|
|
|
histogram = self.class.prometheus_metric(name, :histogram, &block)
|
2018-01-22 05:55:36 -05:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
histogram.observe(filter_labels(labels), value)
|
2018-01-22 05:55:36 -05:00
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
def labels
|
|
|
|
BASE_LABEL_KEYS.product([nil]).to_h
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
2019-04-04 06:56:12 -04:00
|
|
|
|
2020-07-28 08:09:49 -04:00
|
|
|
def filter_labels(labels)
|
|
|
|
labels.empty? ? self.labels : labels.without(*FILTERED_LABEL_KEYS).merge(self.labels)
|
2019-04-04 06:56:12 -04:00
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|