Commit d65e7aa9 authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Merge branch 'add-action-to-all-metrics' into 'master'

See merge request !2370
parents 701e9ee4 35b501f3
......@@ -39,10 +39,8 @@ def transaction_from_env(env)
end
def tag_controller(trans, env)
controller = env[CONTROLLER_KEY]
label = "#{controller.class.name}##{controller.action_name}"
trans.add_tag(:action, label)
controller = env[CONTROLLER_KEY]
trans.action = "#{controller.class.name}##{controller.action_name}"
end
end
end
......
......@@ -5,19 +5,14 @@ module Metrics
# This middleware is intended to be used as a server-side middleware.
class SidekiqMiddleware
def call(worker, message, queue)
trans = Transaction.new
trans = Transaction.new("#{worker.class.name}#perform")
begin
trans.run { yield }
ensure
tag_worker(trans, worker)
trans.finish
end
end
def tag_worker(trans, worker)
trans.add_tag(:action, "#{worker.class.name}#perform")
end
end
end
end
......@@ -6,11 +6,15 @@ class Transaction
attr_reader :tags, :values
attr_accessor :action
def self.current
Thread.current[THREAD_KEY]
end
def initialize
# action - A String describing the action performed, usually the class
# plus method name.
def initialize(action = nil)
@metrics = []
@started_at = nil
......@@ -18,6 +22,7 @@ def initialize
@values = Hash.new(0)
@tags = {}
@action = action
end
def duration
......@@ -70,7 +75,15 @@ def track_self
end
def submit
Metrics.submit_metrics(@metrics.map(&:to_hash))
metrics = @metrics.map do |metric|
hash = metric.to_hash
hash[:tags][:action] ||= @action if @action
hash
end
Metrics.submit_metrics(metrics)
end
def sidekiq?
......
......@@ -57,7 +57,7 @@
middleware.tag_controller(transaction, env)
expect(transaction.tags[:action]).to eq('TestController#show')
expect(transaction.action).to eq('TestController#show')
end
end
end
......@@ -5,22 +5,15 @@
describe '#call' do
it 'tracks the transaction' do
worker = Class.new.new
worker = double(:worker, class: double(:class, name: 'TestWorker'))
expect(Gitlab::Metrics::Transaction).to receive(:new).
with('TestWorker#perform').
and_call_original
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
middleware.call(worker, 'test', :test) { nil }
end
end
describe '#tag_worker' do
it 'adds the worker class and action to the transaction' do
trans = Gitlab::Metrics::Transaction.new
worker = double(:worker, class: double(:class, name: 'TestWorker'))
expect(trans).to receive(:add_tag).with(:action, 'TestWorker#perform')
middleware.tag_worker(trans, worker)
end
end
end
......@@ -96,5 +96,22 @@
transaction.submit
end
it 'adds the action as a tag for every metric' do
transaction.action = 'Foo#bar'
transaction.track_self
hash = {
series: 'rails_transactions',
tags: { action: 'Foo#bar' },
values: { duration: 0.0 },
timestamp: an_instance_of(Fixnum)
}
expect(Gitlab::Metrics).to receive(:submit_metrics).
with([hash])
transaction.submit
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment