debian-mirror-gitlab/lib/gitlab/metrics/subscribers/active_record.rb

91 lines
3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Metrics
module Subscribers
# Class for tracking the total query duration of a transaction.
class ActiveRecord < ActiveSupport::Subscriber
attach_to :active_record
2018-11-20 20:47:30 +05:30
IGNORABLE_SQL = %w{BEGIN COMMIT}.freeze
2020-06-23 00:09:42 +05:30
DB_COUNTERS = %i{db_count db_write_count db_cached_count}.freeze
2021-04-17 20:07:23 +05:30
SQL_COMMANDS_WITH_COMMENTS_REGEX = /\A(\/\*.*\*\/\s)?((?!(.*[^\w'"](DELETE|UPDATE|INSERT INTO)[^\w'"])))(WITH.*)?(SELECT)((?!(FOR UPDATE|FOR SHARE)).)*$/i.freeze
2021-04-29 21:17:54 +05:30
SQL_DURATION_BUCKET = [0.05, 0.1, 0.25].freeze
TRANSACTION_DURATION_BUCKET = [0.1, 0.25, 1].freeze
2021-04-17 20:07:23 +05:30
# This event is published from ActiveRecordBaseTransactionMetrics and
# used to record a database transaction duration when calling
# ActiveRecord::Base.transaction {} block.
def transaction(event)
2021-04-29 21:17:54 +05:30
observe(:gitlab_database_transaction_seconds, event) do
buckets TRANSACTION_DURATION_BUCKET
end
2021-04-17 20:07:23 +05:30
end
2018-11-20 20:47:30 +05:30
def sql(event)
2020-10-24 23:57:45 +05:30
# Mark this thread as requiring a database connection. This is used
# by the Gitlab::Metrics::Samplers::ThreadsSampler to count threads
# using a connection.
Thread.current[:uses_db_connection] = true
2018-11-20 20:47:30 +05:30
payload = event.payload
2021-04-17 20:07:23 +05:30
return if ignored_query?(payload)
2018-11-20 20:47:30 +05:30
2021-04-17 20:07:23 +05:30
increment(:db_count)
increment(:db_cached_count) if cached_query?(payload)
increment(:db_write_count) unless select_sql_command?(payload)
2021-02-22 17:27:13 +05:30
2021-04-29 21:17:54 +05:30
observe(:gitlab_sql_duration_seconds, event) do
buckets SQL_DURATION_BUCKET
end
end
2020-07-28 23:09:34 +05:30
def self.db_counter_payload
return {} unless Gitlab::SafeRequestStore.active?
2021-04-17 20:07:23 +05:30
payload = {}
DB_COUNTERS.each do |counter|
payload[counter] = Gitlab::SafeRequestStore[counter].to_i
end
payload
2020-07-28 23:09:34 +05:30
end
2021-04-29 21:17:54 +05:30
def self.known_payload_keys
DB_COUNTERS
end
private
2021-04-17 20:07:23 +05:30
def ignored_query?(payload)
payload[:name] == 'SCHEMA' || IGNORABLE_SQL.include?(payload[:sql])
2020-06-23 00:09:42 +05:30
end
2021-04-17 20:07:23 +05:30
def cached_query?(payload)
payload.fetch(:cached, payload[:name] == 'CACHE')
end
2020-06-23 00:09:42 +05:30
2021-04-17 20:07:23 +05:30
def select_sql_command?(payload)
payload[:sql].match(SQL_COMMANDS_WITH_COMMENTS_REGEX)
2020-07-28 23:09:34 +05:30
end
def increment(counter)
2021-02-22 17:27:13 +05:30
current_transaction&.increment("gitlab_transaction_#{counter}_total".to_sym, 1)
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
Gitlab::SafeRequestStore[counter] = Gitlab::SafeRequestStore[counter].to_i + 1
end
2021-04-29 21:17:54 +05:30
def observe(histogram, event, &block)
current_transaction&.observe(histogram, event.duration / 1000.0, &block)
2020-06-23 00:09:42 +05:30
end
def current_transaction
2021-04-17 20:07:23 +05:30
::Gitlab::Metrics::WebTransaction.current || ::Gitlab::Metrics::BackgroundTransaction.current
end
end
end
end
end
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::Metrics::Subscribers::ActiveRecord.prepend_mod_with('Gitlab::Metrics::Subscribers::ActiveRecord')