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

131 lines
3 KiB
Ruby
Raw Normal View History

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.
EVENT_SERIES = 'events'
attr_reader :tags, :values, :method, :metrics
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 = {}
@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
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
yield
ensure
@memory_after = System.memory_usage
2016-08-24 12:49:21 +05:30
@finished_at = System.monotonic_time
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-09-13 17:45:13 +05:30
method
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|
hash = metric.to_hash
2016-09-13 17:45:13 +05:30
hash[:tags][:action] ||= @action if @action && !metric.event?
hash
end
2016-06-22 15:30:34 +05:30
Metrics.submit_metrics(submit_hashes)
end
end
end
end