gitlab-org--gitlab-foss/lib/gitlab/metrics/transaction.rb
Yorick Peterse 35b501f30a Tag all transaction metrics with an "action" tag
Without this it's impossible to find out what methods/views/queries are
executed by a certain controller or Sidekiq worker. While this will
increase the total number of series it should stay within reasonable
limits due to the amount of "actions" being small enough.
2016-01-11 16:51:01 +01:00

94 lines
1.8 KiB
Ruby

module Gitlab
module Metrics
# Class for storing metrics information of a single transaction.
class Transaction
THREAD_KEY = :_gitlab_metrics_transaction
attr_reader :tags, :values
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 = []
@started_at = nil
@finished_at = nil
@values = Hash.new(0)
@tags = {}
@action = action
end
def duration
@finished_at ? (@finished_at - @started_at) * 1000.0 : 0.0
end
def run
Thread.current[THREAD_KEY] = self
@started_at = Time.now
yield
ensure
@finished_at = Time.now
Thread.current[THREAD_KEY] = nil
end
def add_metric(series, values, tags = {})
prefix = sidekiq? ? 'sidekiq_' : 'rails_'
@metrics << Metric.new("#{prefix}#{series}", values, tags)
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 }
@values.each do |name, value|
values[name] = value
end
add_metric('transactions', values, @tags)
end
def submit
metrics = @metrics.map do |metric|
hash = metric.to_hash
hash[:tags][:action] ||= @action if @action
hash
end
Metrics.submit_metrics(metrics)
end
def sidekiq?
Sidekiq.server?
end
end
end
end