Commit 66a997a9 authored by Yorick Peterse's avatar Yorick Peterse

Track total query/view timings in transactions

parent 96075be6
......@@ -6,6 +6,7 @@ if Gitlab::Metrics.enabled?
# These are manually require'd so the classes are registered properly with
# ActiveSupport.
require 'gitlab/metrics/subscribers/action_view'
require 'gitlab/metrics/subscribers/active_record'
Gitlab::Application.configure do |config|
config.middleware.use(Gitlab::Metrics::RackMiddleware)
......
......@@ -19,6 +19,7 @@ module Gitlab
values = values_for(event)
tags = tags_for(event)
current_transaction.increment(:view_duration, event.duration)
current_transaction.add_metric(SERIES, values, tags)
end
......
module Gitlab
module Metrics
module Subscribers
# Class for tracking the total query duration of a transaction.
class ActiveRecord < ActiveSupport::Subscriber
attach_to :active_record
def sql(event)
return unless current_transaction
current_transaction.increment(:sql_duration, event.duration)
end
private
def current_transaction
Transaction.current
end
end
end
end
end
......@@ -28,6 +28,9 @@ describe Gitlab::Metrics::Subscribers::ActionView do
line: 4
}
expect(transaction).to receive(:increment).
with(:view_duration, 2.1)
expect(transaction).to receive(:add_metric).
with(described_class::SERIES, values, tags)
......
require 'spec_helper'
describe Gitlab::Metrics::Subscribers::ActiveRecord do
let(:transaction) { Gitlab::Metrics::Transaction.new('rspec') }
let(:subscriber) { described_class.new }
let(:event) do
double(:event, duration: 0.2,
payload: { sql: 'SELECT * FROM users WHERE id = 10' })
end
describe '#sql' do
describe 'without a current transaction' do
it 'simply returns' do
expect_any_instance_of(Gitlab::Metrics::Transaction).
to_not receive(:increment)
subscriber.sql(event)
end
end
describe 'with a current transaction' do
it 'increments the :sql_duration value' do
expect(subscriber).to receive(:current_transaction).
at_least(:once).
and_return(transaction)
expect(transaction).to receive(:increment).
with(:sql_duration, 0.2)
subscriber.sql(event)
end
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