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