Module: ActiveRecordQueryCounter
- Defined in:
- lib/active_record_query_counter.rb,
lib/active_record_query_counter/counter.rb,
lib/active_record_query_counter/version.rb,
lib/active_record_query_counter/thresholds.rb,
lib/active_record_query_counter/rack_middleware.rb,
lib/active_record_query_counter/transaction_info.rb,
lib/active_record_query_counter/sidekiq_middleware.rb,
lib/active_record_query_counter/connection_adapter_extension.rb,
lib/active_record_query_counter/transaction_manager_extension.rb
Overview
Everything you need to count ActiveRecord queries and row counts within a block.
Defined Under Namespace
Modules: ConnectionAdapterExtension, TransactionManagerExtension Classes: Counter, RackMiddleware, SidekiqMiddleware, Thresholds, TransactionInfo
Constant Summary collapse
- VERSION =
File.read(File.join(__dir__, "..", "..", "VERSION")).strip
Class Method Summary collapse
-
.add_query(sql, name, binds, row_count, start_time, end_time) ⇒ void
private
Increment the query counters.
-
.add_transaction(start_time, end_time) ⇒ void
private
Increment the transaction counters.
-
.count_queries ⇒ Object
Enable query counting within a block.
-
.default_thresholds ⇒ ActiveRecordQueryCounter::Thresholds
The global notification thresholds for sending notifications.
-
.enable!(connection_class) ⇒ void
Enable the query counting behavior on a connection adapter class.
-
.first_transaction_start_time ⇒ Float?
Return the time when the first transaction began within the current block.
-
.info ⇒ Hash?
Return the query info as a hash with keys :query_count, :row_count, :query_time :transaction_count, and :transaction_type or nil if not inside a block where queries are being counted.
-
.last_transaction_end_time ⇒ Float?
Return the time when the last transaction ended within the current block.
-
.query_count ⇒ Integer?
Return the number of queries that have been counted within the current block.
-
.query_time ⇒ Float?
Return the total time spent executing queries within the current block.
-
.row_count ⇒ Integer?
Return the number of rows that have been counted within the current block.
-
.thresholds ⇒ Object
Get the current local notification thresholds.
-
.transaction_count ⇒ Integer?
Return the number of transactions that have been counted within the current block.
-
.transaction_time ⇒ Float?
Return the total time spent in transactions that have been counted within the current block.
-
.transactions ⇒ Array<ActiveRecordQueryCounter::TransactionInfo>?
Return an array of transaction information for any transactions that have been counted within the current block.
Class Method Details
.add_query(sql, name, binds, row_count, start_time, end_time) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Increment the query counters.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record_query_counter.rb', line 57 def add_query(sql, name, binds, row_count, start_time, end_time) return if IGNORED_STATEMENTS.include?(name) counter = current_counter return unless counter.is_a?(Counter) elapsed_time = end_time - start_time counter.query_count += 1 counter.row_count += row_count counter.query_time += elapsed_time trace = nil query_time_threshold = (counter.thresholds.query_time || -1) if query_time_threshold >= 0 && elapsed_time >= query_time_threshold trace = backtrace send_notification("query_time", start_time, end_time, sql: sql, binds: binds, row_count: row_count, trace: trace) end row_count_threshold = (counter.thresholds.row_count || -1) if row_count_threshold >= 0 && row_count >= row_count_threshold trace ||= backtrace send_notification("row_count", start_time, end_time, sql: sql, binds: binds, row_count: row_count, trace: trace) end end |
.add_transaction(start_time, end_time) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Increment the transaction counters.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record_query_counter.rb', line 88 def add_transaction(start_time, end_time) counter = current_counter return unless counter.is_a?(Counter) trace = backtrace counter.add_transaction(trace: trace, start_time: start_time, end_time: end_time) transaction_time_threshold = (counter.thresholds.transaction_time || -1) if transaction_time_threshold >= 0 && end_time - start_time >= transaction_time_threshold send_notification("transaction_time", start_time, end_time, trace: backtrace) end end |
.count_queries ⇒ Object
Enable query counting within a block.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/active_record_query_counter.rb', line 29 def count_queries save_counter = current_counter begin counter = Counter.new self.current_counter = counter retval = yield transaction_count = counter.transaction_count if transaction_count > 0 transaction_threshold = (counter.thresholds.transaction_count || -1) if transaction_threshold >= 0 && transaction_count >= transaction_threshold send_notification("transaction_count", counter.first_transaction_start_time, counter.last_transaction_end_time, transactions: counter.transactions) end end retval ensure self.current_counter = save_counter end end |
.default_thresholds ⇒ ActiveRecordQueryCounter::Thresholds
The global notification thresholds for sending notifications. The values set in these thresholds are used as the default values.
195 196 197 |
# File 'lib/active_record_query_counter.rb', line 195 def default_thresholds @default_thresholds ||= Thresholds.new end |
.enable!(connection_class) ⇒ void
This method returns an undefined value.
Enable the query counting behavior on a connection adapter class.
209 210 211 212 213 214 215 216 |
# File 'lib/active_record_query_counter.rb', line 209 def enable!(connection_class) unless connection_class.include?(ConnectionAdapterExtension) connection_class.prepend(ConnectionAdapterExtension) end unless ActiveRecord::ConnectionAdapters::TransactionManager.include?(TransactionManagerExtension) ActiveRecord::ConnectionAdapters::TransactionManager.prepend(TransactionManagerExtension) end end |
.first_transaction_start_time ⇒ Float?
Return the time when the first transaction began within the current block. Returns nil if not inside a block where queries are being counted or there are no transactions.
150 151 152 153 |
# File 'lib/active_record_query_counter.rb', line 150 def first_transaction_start_time counter = current_counter counter.first_transaction_start_time if counter.is_a?(Counter) end |
.info ⇒ Hash?
Return the query info as a hash with keys :query_count, :row_count, :query_time :transaction_count, and :transaction_type or nil if not inside a block where queries are being counted.
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/active_record_query_counter.rb', line 178 def info counter = current_counter if counter.is_a?(Counter) { query_count: counter.query_count, row_count: counter.row_count, query_time: counter.query_time, transaction_count: counter.transaction_count, transaction_time: counter.transaction_time } end end |
.last_transaction_end_time ⇒ Float?
Return the time when the last transaction ended within the current block. Returns nil if not inside a block where queries are being counted or there are no transactions.
159 160 161 162 |
# File 'lib/active_record_query_counter.rb', line 159 def last_transaction_end_time counter = current_counter counter.transactions.last&.end_time if counter.is_a?(Counter) end |
.query_count ⇒ Integer?
Return the number of queries that have been counted within the current block. Returns nil if not inside a block where queries are being counted.
105 106 107 108 |
# File 'lib/active_record_query_counter.rb', line 105 def query_count counter = current_counter counter.query_count if counter.is_a?(Counter) end |
.query_time ⇒ Float?
Return the total time spent executing queries within the current block. Returns nil if not inside a block where queries are being counted.
123 124 125 126 |
# File 'lib/active_record_query_counter.rb', line 123 def query_time counter = current_counter counter.query_time if counter.is_a?(Counter) end |
.row_count ⇒ Integer?
Return the number of rows that have been counted within the current block. Returns nil if not inside a block where queries are being counted.
114 115 116 117 |
# File 'lib/active_record_query_counter.rb', line 114 def row_count counter = current_counter counter.row_count if counter.is_a?(Counter) end |
.thresholds ⇒ Object
Get the current local notification thresholds. These thresholds are only used within the current ‘count_queries` block.
201 202 203 |
# File 'lib/active_record_query_counter.rb', line 201 def thresholds current_counter&.thresholds || default_thresholds.dup end |
.transaction_count ⇒ Integer?
Return the number of transactions that have been counted within the current block. Returns nil if not inside a block where queries are being counted.
132 133 134 135 |
# File 'lib/active_record_query_counter.rb', line 132 def transaction_count counter = current_counter counter.transaction_count if counter.is_a?(Counter) end |
.transaction_time ⇒ Float?
Return the total time spent in transactions that have been counted within the current block. Returns nil if not inside a block where queries are being counted.
141 142 143 144 |
# File 'lib/active_record_query_counter.rb', line 141 def transaction_time counter = current_counter counter.transaction_time if counter.is_a?(Counter) end |
.transactions ⇒ Array<ActiveRecordQueryCounter::TransactionInfo>?
Return an array of transaction information for any transactions that have been counted within the current block. Returns nil if not inside a block where queries are being counted.
168 169 170 171 |
# File 'lib/active_record_query_counter.rb', line 168 def transactions counter = current_counter counter.transactions if counter.is_a?(Counter) end |