2016-01-14 18:37:52 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Class for storing metrics information of a single transaction.
|
|
|
|
class Transaction
|
|
|
|
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
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
attr_accessor :action
|
|
|
|
|
|
|
|
def self.current
|
|
|
|
Thread.current[THREAD_KEY]
|
|
|
|
end
|
|
|
|
|
|
|
|
# action - A String describing the action performed, usually the class
|
|
|
|
# plus method name.
|
|
|
|
def initialize(action = nil)
|
|
|
|
@metrics = []
|
2016-06-22 15:30:34 +05:30
|
|
|
@methods = {}
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
@started_at = nil
|
|
|
|
@finished_at = nil
|
|
|
|
|
|
|
|
@values = Hash.new(0)
|
|
|
|
@tags = {}
|
|
|
|
@action = action
|
|
|
|
|
|
|
|
@memory_before = 0
|
|
|
|
@memory_after = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def duration
|
2016-08-24 12:49:21 +05:30
|
|
|
@finished_at ? (@finished_at - @started_at) : 0.0
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def allocated_memory
|
|
|
|
@memory_after - @memory_before
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
Thread.current[THREAD_KEY] = self
|
|
|
|
|
|
|
|
@memory_before = System.memory_usage
|
2016-08-24 12:49:21 +05:30
|
|
|
@started_at = System.monotonic_time
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@memory_after = System.memory_usage
|
2016-08-24 12:49:21 +05:30
|
|
|
@finished_at = System.monotonic_time
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
Thread.current[THREAD_KEY] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_metric(series, values, tags = {})
|
2016-09-13 17:45:13 +05:30
|
|
|
@metrics << Metric.new("#{Metrics.series_prefix}#{series}", values, 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 = {})
|
|
|
|
@metrics << Metric.new(EVENT_SERIES,
|
|
|
|
{ count: 1 },
|
|
|
|
{ event: event_name }.merge(tags),
|
|
|
|
:event)
|
|
|
|
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.
|
|
|
|
def method_call_for(name)
|
|
|
|
unless method = @methods[name]
|
|
|
|
@methods[name] = method = MethodCall.new(name, Instrumentation.series)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
method
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def increment(name, value)
|
|
|
|
@values[name] += value
|
|
|
|
end
|
|
|
|
|
|
|
|
def set(name, value)
|
|
|
|
@values[name] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_tag(key, value)
|
|
|
|
@tags[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
|
|
|
track_self
|
|
|
|
submit
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_self
|
|
|
|
values = { duration: duration, 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|
|
2016-01-14 18:37:52 +05:30
|
|
|
hash = metric.to_hash
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
hash[:tags][:action] ||= @action if @action && !metric.event?
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
Metrics.submit_metrics(submit_hashes)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|