Commit 2ee8f555 authored by Yorick Peterse's avatar Yorick Peterse

Automatically prefix transaction series names

This ensures Rails and Sidekiq transactions are split into the series
"rails_transactions" and "sidekiq_transactions" respectively.
parent 2ea464bb
......@@ -4,8 +4,6 @@ module Gitlab
class RackMiddleware
CONTROLLER_KEY = 'action_controller.instance'
SERIES = 'rails_transactions'
def initialize(app)
@app = app
end
......@@ -32,7 +30,7 @@ module Gitlab
end
def transaction_from_env(env)
trans = Transaction.new(SERIES)
trans = Transaction.new
trans.add_tag(:request_method, env['REQUEST_METHOD'])
trans.add_tag(:request_uri, env['REQUEST_URI'])
......
......@@ -4,10 +4,8 @@ module Gitlab
#
# This middleware is intended to be used as a server-side middleware.
class SidekiqMiddleware
SERIES = 'sidekiq_transactions'
def call(worker, message, queue)
trans = Transaction.new(SERIES)
trans = Transaction.new
begin
trans.run { yield }
......
......@@ -10,9 +10,7 @@ module Gitlab
Thread.current[THREAD_KEY]
end
# name - The name of this transaction as a String.
def initialize(series)
@series = series
def initialize
@metrics = []
@uuid = SecureRandom.uuid
......@@ -40,9 +38,10 @@ module Gitlab
end
def add_metric(series, values, tags = {})
tags = tags.merge(transaction_id: @uuid)
tags = tags.merge(transaction_id: @uuid)
prefix = sidekiq? ? 'sidekiq_' : 'rails_'
@metrics << Metric.new(series, values, tags)
@metrics << Metric.new("#{prefix}#{series}", values, tags)
end
def increment(name, value)
......@@ -65,12 +64,16 @@ module Gitlab
values[name] = value
end
add_metric(@series, values, @tags)
add_metric('transactions', values, @tags)
end
def submit
Metrics.submit_metrics(@metrics.map(&:to_hash))
end
def sidekiq?
Sidekiq.server?
end
end
end
end
require 'spec_helper'
describe Gitlab::Metrics::Instrumentation do
let(:transaction) { Gitlab::Metrics::Transaction.new('rspec') }
let(:transaction) { Gitlab::Metrics::Transaction.new }
before do
@dummy = Class.new do
......
......@@ -15,7 +15,7 @@ describe Gitlab::Metrics::SidekiqMiddleware do
describe '#tag_worker' do
it 'adds the worker class and action to the transaction' do
trans = Gitlab::Metrics::Transaction.new('rspec')
trans = Gitlab::Metrics::Transaction.new
worker = double(:worker, class: double(:class, name: 'TestWorker'))
expect(trans).to receive(:add_tag).with(:action, 'TestWorker#perform')
......
require 'spec_helper'
describe Gitlab::Metrics::Subscribers::ActionView do
let(:transaction) { Gitlab::Metrics::Transaction.new('rspec') }
let(:transaction) { Gitlab::Metrics::Transaction.new }
let(:subscriber) { described_class.new }
......
require 'spec_helper'
describe Gitlab::Metrics::Subscribers::ActiveRecord do
let(:transaction) { Gitlab::Metrics::Transaction.new('rspec') }
let(:transaction) { Gitlab::Metrics::Transaction.new }
let(:subscriber) { described_class.new }
let(:event) do
......
require 'spec_helper'
describe Gitlab::Metrics::Transaction do
let(:transaction) { described_class.new('rspec') }
let(:transaction) { described_class.new }
describe '#duration' do
it 'returns the duration of a transaction in seconds' do
......@@ -32,7 +32,7 @@ describe Gitlab::Metrics::Transaction do
describe '#add_metric' do
it 'adds a metric tagged with the transaction UUID' do
expect(Gitlab::Metrics::Metric).to receive(:new).
with('foo', { number: 10 }, { transaction_id: transaction.uuid })
with('rails_foo', { number: 10 }, { transaction_id: transaction.uuid })
transaction.add_metric('foo', number: 10)
end
......@@ -44,7 +44,7 @@ describe Gitlab::Metrics::Transaction do
transaction.increment(:time, 2)
expect(transaction).to receive(:add_metric).
with('rspec', { duration: 0.0, time: 3 }, {})
with('transactions', { duration: 0.0, time: 3 }, {})
transaction.track_self
end
......@@ -70,7 +70,7 @@ describe Gitlab::Metrics::Transaction do
describe '#track_self' do
it 'adds a metric for the transaction itself' do
expect(transaction).to receive(:add_metric).
with('rspec', { duration: transaction.duration }, {})
with('transactions', { duration: transaction.duration }, {})
transaction.track_self
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