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
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
# base labels shared among all transactions
|
|
|
|
BASE_LABELS = { controller: nil, action: nil }.freeze
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
THREAD_KEY = :_gitlab_metrics_transaction
|
|
|
|
|
|
|
|
# The series to store events (e.g. Git pushes) in.
|
|
|
|
EVENT_SERIES = 'events'.freeze
|
|
|
|
|
|
|
|
attr_reader :tags, :values, :method, :metrics
|
|
|
|
|
|
|
|
def self.current
|
|
|
|
Thread.current[THREAD_KEY]
|
|
|
|
end
|
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
def initialize
|
2017-10-16 09:57:33 -04:00
|
|
|
@metrics = []
|
|
|
|
@methods = {}
|
|
|
|
|
|
|
|
@started_at = nil
|
|
|
|
@finished_at = nil
|
|
|
|
|
|
|
|
@values = Hash.new(0)
|
|
|
|
@tags = {}
|
|
|
|
|
|
|
|
@memory_before = 0
|
|
|
|
@memory_after = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def duration
|
|
|
|
@finished_at ? (@finished_at - @started_at) : 0.0
|
|
|
|
end
|
|
|
|
|
2017-12-12 12:12:49 -05:00
|
|
|
def duration_milliseconds
|
2017-12-19 13:40:43 -05:00
|
|
|
duration.in_milliseconds.to_i
|
2017-12-12 12:12:49 -05:00
|
|
|
end
|
|
|
|
|
2017-10-16 09:57:33 -04:00
|
|
|
def allocated_memory
|
|
|
|
@memory_after - @memory_before
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
Thread.current[THREAD_KEY] = self
|
|
|
|
|
|
|
|
@memory_before = System.memory_usage
|
|
|
|
@started_at = System.monotonic_time
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@memory_after = System.memory_usage
|
|
|
|
@finished_at = System.monotonic_time
|
|
|
|
|
2018-01-16 02:50:42 -05:00
|
|
|
self.class.gitlab_transaction_duration_seconds.observe(labels, duration)
|
|
|
|
self.class.gitlab_transaction_allocated_memory_bytes.observe(labels, allocated_memory * 1024.0)
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
Thread.current[THREAD_KEY] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_metric(series, values, tags = {})
|
|
|
|
@metrics << Metric.new("#{Metrics.series_prefix}#{series}", values, tags)
|
|
|
|
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 = {})
|
2018-01-16 02:50:42 -05:00
|
|
|
self.class.transaction_metric(event_name, :counter, prefix: 'event_', tags: tags).increment(tags.merge(labels))
|
2017-10-16 09:57:33 -04:00
|
|
|
@metrics << Metric.new(EVENT_SERIES, { count: 1 }, tags.merge(event: event_name), :event)
|
|
|
|
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
|
|
|
|
|
|
|
|
def increment(name, value, use_prometheus = true)
|
2018-01-16 02:50:42 -05:00
|
|
|
self.class.transaction_metric(name, :counter).increment(labels, value) if use_prometheus
|
2017-10-16 09:57:33 -04:00
|
|
|
@values[name] += value
|
|
|
|
end
|
|
|
|
|
|
|
|
def set(name, value, use_prometheus = true)
|
2018-01-16 02:50:42 -05:00
|
|
|
self.class.transaction_metric(name, :gauge).set(labels, value) if use_prometheus
|
2017-10-16 09:57:33 -04:00
|
|
|
@values[name] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
|
|
|
track_self
|
|
|
|
submit
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_self
|
2017-12-12 12:12:49 -05:00
|
|
|
values = { duration: duration_milliseconds, allocated_memory: allocated_memory }
|
2017-10-16 09:57:33 -04:00
|
|
|
|
|
|
|
@values.each do |name, value|
|
|
|
|
values[name] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_metric('transactions', values, @tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
def submit
|
|
|
|
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
|
|
|
|
hash[:tags][:action] ||= action if action && !metric.event?
|
|
|
|
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
Metrics.submit_metrics(submit_hashes)
|
|
|
|
end
|
|
|
|
|
2017-10-17 10:06:52 -04:00
|
|
|
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
|
|
|
|
|
2018-01-22 05:55:36 -05:00
|
|
|
define_histogram :gitlab_transaction_duration_seconds do
|
|
|
|
docstring 'Transaction duration'
|
|
|
|
base_labels BASE_LABELS
|
2018-01-22 10:43:29 -05:00
|
|
|
buckets [0.001, 0.01, 0.1, 1.0, 10.0]
|
2018-01-22 05:55:36 -05:00
|
|
|
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
|
2017-10-16 09:57:33 -04:00
|
|
|
|
2018-01-16 02:50:42 -05:00
|
|
|
def self.transaction_metric(name, type, prefix: nil, tags: {})
|
2018-01-16 09:47:07 -05:00
|
|
|
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)
|
2018-01-16 20:55:52 -05:00
|
|
|
|
2018-01-16 09:47:07 -05:00
|
|
|
if type == :gauge
|
|
|
|
multiprocess_mode :livesum
|
|
|
|
end
|
2017-10-24 03:20:49 -04:00
|
|
|
end
|
2017-10-16 09:57:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|