debian-mirror-gitlab/lib/gitlab/metrics/transaction.rb

179 lines
5.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Metrics
# Class for storing metrics information of a single transaction.
class Transaction
2018-03-17 18:26:18 +05:30
include Gitlab::Metrics::Methods
# base labels shared among all transactions
BASE_LABELS = { controller: nil, action: nil }.freeze
2019-05-18 00:54:41 +05:30
# labels that potentially contain sensitive information and will be filtered
FILTERED_LABELS = [:branch, :path].freeze
2018-03-17 18:26:18 +05:30
THREAD_KEY = :_gitlab_metrics_transaction
2016-09-13 17:45:13 +05:30
# The series to store events (e.g. Git pushes) in.
2017-08-17 22:00:37 +05:30
EVENT_SERIES = 'events'.freeze
2016-09-13 17:45:13 +05:30
attr_reader :tags, :values, :method, :metrics
def self.current
Thread.current[THREAD_KEY]
end
2018-03-17 18:26:18 +05:30
def initialize
@metrics = []
2016-06-22 15:30:34 +05:30
@methods = {}
2018-03-17 18:26:18 +05:30
@started_at = nil
@finished_at = nil
@values = Hash.new(0)
2018-03-17 18:26:18 +05:30
@tags = {}
@memory_before = 0
2018-03-17 18:26:18 +05:30
@memory_after = 0
end
def duration
2016-08-24 12:49:21 +05:30
@finished_at ? (@finished_at - @started_at) : 0.0
end
2018-03-17 18:26:18 +05:30
def duration_milliseconds
duration.in_milliseconds.to_i
end
def allocated_memory
@memory_after - @memory_before
end
def run
Thread.current[THREAD_KEY] = self
@memory_before = System.memory_usage
2018-03-17 18:26:18 +05:30
@started_at = System.monotonic_time
yield
ensure
@memory_after = System.memory_usage
2018-03-17 18:26:18 +05:30
@finished_at = System.monotonic_time
self.class.gitlab_transaction_duration_seconds.observe(labels, duration)
self.class.gitlab_transaction_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0)
Thread.current[THREAD_KEY] = nil
end
def add_metric(series, values, tags = {})
2019-05-18 00:54:41 +05:30
@metrics << Metric.new("#{::Gitlab::Metrics.series_prefix}#{series}", values, filter_tags(tags))
2016-06-22 15:30:34 +05:30
end
2016-09-13 17:45:13 +05:30
# Tracks a business level event
2016-06-22 15:30:34 +05:30
#
2016-09-13 17:45:13 +05:30
# Business level events including events such as Git pushes, Emails being
# sent, etc.
2016-06-22 15:30:34 +05:30
#
2016-09-13 17:45:13 +05:30
# 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 = {})
2019-05-18 00:54:41 +05:30
filtered_tags = filter_tags(tags)
self.class.transaction_metric(event_name, :counter, prefix: 'event_', use_feature_flag: true, tags: filtered_tags).increment(filtered_tags.merge(labels))
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, filtered_tags.merge(event: event_name), :event)
2016-09-13 17:45:13 +05:30
end
2016-06-22 15:30:34 +05:30
2016-09-13 17:45:13 +05:30
# Returns a MethodCall object for the given name.
2018-03-17 18:26:18 +05:30
def method_call_for(name, module_name, method_name)
2016-09-13 17:45:13 +05:30
unless method = @methods[name]
2018-03-17 18:26:18 +05:30
@methods[name] = method = MethodCall.new(name, module_name, method_name, self)
2016-06-22 15:30:34 +05:30
end
2016-09-13 17:45:13 +05:30
method
end
2018-03-17 18:26:18 +05:30
def increment(name, value, use_prometheus = true)
self.class.transaction_metric(name, :counter).increment(labels, value) if use_prometheus
@values[name] += value
end
2018-03-17 18:26:18 +05:30
def set(name, value, use_prometheus = true)
self.class.transaction_metric(name, :gauge).set(labels, value) if use_prometheus
@values[name] = value
end
def finish
track_self
submit
end
def track_self
2018-03-17 18:26:18 +05:30
values = { duration: duration_milliseconds, allocated_memory: allocated_memory }
@values.each do |name, value|
values[name] = value
end
add_metric('transactions', values, @tags)
end
def submit
2016-06-22 15:30:34 +05:30
submit = @metrics.dup
@methods.each do |name, method|
submit << method.to_metric if method.above_threshold?
end
submit_hashes = submit.map do |metric|
hash = metric.to_hash
2018-03-17 18:26:18 +05:30
hash[:tags][:action] ||= action if action && !metric.event?
hash
end
2019-05-18 00:54:41 +05:30
::Gitlab::Metrics.submit_metrics(submit_hashes)
end
2018-03-17 18:26:18 +05:30
def labels
BASE_LABELS
end
# returns string describing the action performed, usually the class plus method name.
def action
"#{labels[:controller]}##{labels[:action]}" if labels && !labels.empty?
end
define_histogram :gitlab_transaction_duration_seconds do
docstring 'Transaction duration'
base_labels BASE_LABELS
2018-11-08 19:23:39 +05:30
buckets [0.1, 0.25, 0.5, 1.0, 2.5, 5.0]
2018-03-17 18:26:18 +05:30
end
define_histogram :gitlab_transaction_allocated_memory_bytes do
docstring 'Transaction allocated memory bytes'
base_labels BASE_LABELS
buckets [100, 1000, 10000, 100000, 1000000, 10000000]
with_feature :prometheus_metrics_transaction_allocated_memory
end
def self.transaction_metric(name, type, prefix: nil, use_feature_flag: false, tags: {})
metric_name = "gitlab_transaction_#{prefix}#{name}_total".to_sym
fetch_metric(type, metric_name) do
docstring "Transaction #{prefix}#{name} #{type}"
base_labels tags.merge(BASE_LABELS)
with_feature "prometheus_transaction_#{prefix}#{name}_total".to_sym if use_feature_flag
if type == :gauge
multiprocess_mode :livesum
end
end
end
2019-05-18 00:54:41 +05:30
private
def filter_tags(tags)
tags.without(*FILTERED_LABELS)
end
end
end
end