Track total query/view timings in transactions
This commit is contained in:
parent
96075be6f4
commit
66a997a914
|
@ -6,6 +6,7 @@ if Gitlab::Metrics.enabled?
|
||||||
# These are manually require'd so the classes are registered properly with
|
# These are manually require'd so the classes are registered properly with
|
||||||
# ActiveSupport.
|
# ActiveSupport.
|
||||||
require 'gitlab/metrics/subscribers/action_view'
|
require 'gitlab/metrics/subscribers/action_view'
|
||||||
|
require 'gitlab/metrics/subscribers/active_record'
|
||||||
|
|
||||||
Gitlab::Application.configure do |config|
|
Gitlab::Application.configure do |config|
|
||||||
config.middleware.use(Gitlab::Metrics::RackMiddleware)
|
config.middleware.use(Gitlab::Metrics::RackMiddleware)
|
||||||
|
|
|
@ -19,6 +19,7 @@ module Gitlab
|
||||||
values = values_for(event)
|
values = values_for(event)
|
||||||
tags = tags_for(event)
|
tags = tags_for(event)
|
||||||
|
|
||||||
|
current_transaction.increment(:view_duration, event.duration)
|
||||||
current_transaction.add_metric(SERIES, values, tags)
|
current_transaction.add_metric(SERIES, values, tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
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
|
line: 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(transaction).to receive(:increment).
|
||||||
|
with(:view_duration, 2.1)
|
||||||
|
|
||||||
expect(transaction).to receive(:add_metric).
|
expect(transaction).to receive(:add_metric).
|
||||||
with(described_class::SERIES, values, tags)
|
with(described_class::SERIES, values, tags)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
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
|
Loading…
Reference in New Issue