Class: Gitlab::Metrics::Subscribers::ActiveRecord

Inherits:
ActiveSupport::Subscriber
  • Object
show all
Defined in:
lib/gitlab/metrics/subscribers/active_record.rb

Overview

Class for tracking the total query duration of a transaction.

Constant Summary collapse

IGNORABLE_SQL =
%w{BEGIN COMMIT}.freeze
DB_COUNTERS =
%i{db_count db_write_count db_cached_count}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.db_counter_payloadObject


31
32
33
34
35
36
37
# File 'lib/gitlab/metrics/subscribers/active_record.rb', line 31

def self.db_counter_payload
  return {} unless Gitlab::SafeRequestStore.active?

  DB_COUNTERS.map do |counter|
    [counter, Gitlab::SafeRequestStore[counter].to_i]
  end.to_h
end

Instance Method Details

#sql(event) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/metrics/subscribers/active_record.rb', line 13

def sql(event)
  # 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

  return unless current_transaction

  payload = event.payload
  return if payload[:name] == 'SCHEMA' || IGNORABLE_SQL.include?(payload[:sql])

  current_transaction.observe(:gitlab_sql_duration_seconds, event.duration / 1000.0) do
    buckets [0.05, 0.1, 0.25]
  end

  increment_db_counters(payload)
end