2016-01-14 18:37:52 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Subscribers
|
|
|
|
# Class for tracking the total query duration of a transaction.
|
|
|
|
class ActiveRecord < ActiveSupport::Subscriber
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Metrics::Methods
|
2016-01-14 18:37:52 +05:30
|
|
|
attach_to :active_record
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
IGNORABLE_SQL = %w{BEGIN COMMIT}.freeze
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def sql(event)
|
|
|
|
return unless current_transaction
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
payload = event.payload
|
|
|
|
|
|
|
|
return if payload[:name] == 'SCHEMA' || IGNORABLE_SQL.include?(payload[:sql])
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
self.class.gitlab_sql_duration_seconds.observe(current_transaction.labels, event.duration / 1000.0)
|
|
|
|
|
|
|
|
current_transaction.increment(:sql_duration, event.duration, false)
|
|
|
|
current_transaction.increment(:sql_count, 1, false)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
define_histogram :gitlab_sql_duration_seconds do
|
|
|
|
docstring 'SQL time'
|
|
|
|
base_labels Transaction::BASE_LABELS
|
2018-11-18 11:00:15 +05:30
|
|
|
buckets [0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0]
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def current_transaction
|
|
|
|
Transaction.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|