active_record_query_counter 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d653eb9665511008bc78023c7e39ae8eb189e051847d83e16244424b5cbe595
4
- data.tar.gz: 6e9643c840a635228ea0a04cb02277b91c34b1ea5e69bd5b93753066be8ab4fb
3
+ metadata.gz: 00cf8b2bba420e0b37944a53f4be128aeac846d6e387b15b03c3393f3a0030c2
4
+ data.tar.gz: 10c05613e25513f1afab756f9cdd1133a19f7b54e34ca811b7f3ba5ad982ff0e
5
5
  SHA512:
6
- metadata.gz: df192239b02e3ed039eac91a64393d5332cad36dce242e9e02948b3ec0cbe23e7ae5d4b23dc58ebf01158fa885680135e6719f444a871abd37779e85a9f85d2d
7
- data.tar.gz: '069e6959ba3f7ead49824ae91d35774b3b336afe18248c84800eaca65115daa040bdf5871d0c3e591b648cce2b0d5414078fe51ef39bef8d9f0e777be8e7330d'
6
+ metadata.gz: 4294aac8c3b0b55197ba1d069cc0a57f269ad772c23480b928fcf3f0a01b7c4f806f4969bf32f2d229b8dea65bc976d18cc864f923ec468dbbb43fc2fbe20bdd
7
+ data.tar.gz: ffddf3b2d87b74a2ad184d0ec55c4b371cda418c262aa2d0889cf40916579f5bff575ef0e631a39764edd58f7d321b71fa1b993dda6c3ff0c9f1ef060ca02769
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 2.2.0
8
+
9
+ ### Added
10
+
11
+ - Added `ActiveRecordQueryCounter.disable` method to allow disabling query counting behavior within a block.
12
+ - Rails 7.1 compatibility.
13
+
7
14
  ## 2.1.0
8
15
 
9
16
  ### Added
data/README.md CHANGED
@@ -60,6 +60,18 @@ Sidekiq.configure_server do |config|
60
60
  end
61
61
  ```
62
62
 
63
+ If you want to disable query counting within a block of code, you can use the `disable` method.
64
+
65
+ ```ruby
66
+ ActiveRecordQueryCounter.count_queries do
67
+ do_something
68
+ ActiveRecordQueryCounter.disable do
69
+ # Queries will not be counted in this block.
70
+ do_something_else
71
+ end
72
+ end
73
+ ```
74
+
63
75
  ### Notifications
64
76
 
65
77
  You can also subscribe to ActiveSupport notifications to get notified when query thresholds are exceeded.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.2.0
@@ -3,14 +3,38 @@
3
3
  module ActiveRecordQueryCounter
4
4
  # Module to prepend to the connection adapter to inject the counting behavior.
5
5
  module ConnectionAdapterExtension
6
- def exec_query(sql, name = nil, binds = [], *args, **kwargs)
7
- start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
8
- result = super
9
- if result.is_a?(ActiveRecord::Result)
10
- end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
- ActiveRecordQueryCounter.add_query(sql, name, binds, result.length, start_time, end_time)
6
+ class << self
7
+ def inject(connection_class)
8
+ # Rails 7.1+ uses internal_exec_query instead of exec_query.
9
+ mod = (connection_class.instance_methods.include?(:internal_exec_query) ? InternalExecQuery : ExecQuery)
10
+ unless connection_class.include?(mod)
11
+ connection_class.prepend(mod)
12
+ end
13
+ end
14
+ end
15
+
16
+ module ExecQuery
17
+ def exec_query(sql, name = nil, binds = [], *args, **kwargs)
18
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
19
+ result = super
20
+ if result.is_a?(ActiveRecord::Result)
21
+ end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
22
+ ActiveRecordQueryCounter.add_query(sql, name, binds, result.length, start_time, end_time)
23
+ end
24
+ result
25
+ end
26
+ end
27
+
28
+ module InternalExecQuery
29
+ def internal_exec_query(sql, name = nil, binds = [], *args, **kwargs)
30
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
+ result = super
32
+ if result.is_a?(ActiveRecord::Result)
33
+ end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
34
+ ActiveRecordQueryCounter.add_query(sql, name, binds, result.length, start_time, end_time)
35
+ end
36
+ result
12
37
  end
13
- result
14
38
  end
15
39
  end
16
40
  end
@@ -6,6 +6,10 @@ module ActiveRecordQueryCounter
6
6
  class Thresholds
7
7
  attr_reader :query_time, :row_count, :transaction_time, :transaction_count
8
8
 
9
+ def initialize
10
+ clear
11
+ end
12
+
9
13
  def query_time=(value)
10
14
  @query_time = value&.to_f
11
15
  end
@@ -3,6 +3,14 @@
3
3
  module ActiveRecordQueryCounter
4
4
  # Extension to ActiveRecord::ConnectionAdapters::TransactionManager to count transactions.
5
5
  module TransactionManagerExtension
6
+ class << self
7
+ def inject(transaction_manager_class)
8
+ unless transaction_manager_class.include?(self)
9
+ transaction_manager_class.prepend(self)
10
+ end
11
+ end
12
+ end
13
+
6
14
  def begin_transaction(*args, **kwargs)
7
15
  if open_transactions == 0
8
16
  @active_record_query_counter_transaction_start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@@ -48,6 +48,20 @@ module ActiveRecordQueryCounter
48
48
  end
49
49
  end
50
50
 
51
+ # Disable query counting in a block. Any queries or transactions inside the block will not
52
+ # be counted.
53
+ #
54
+ # @return [Object] the return value of the block
55
+ def disable(&block)
56
+ counter = current_counter
57
+ begin
58
+ self.current_counter = nil
59
+ yield
60
+ ensure
61
+ self.current_counter = counter
62
+ end
63
+ end
64
+
51
65
  # Increment the query counters.
52
66
  #
53
67
  # @param row_count [Integer] the number of rows returned by the query
@@ -219,12 +233,8 @@ module ActiveRecordQueryCounter
219
233
  # @param connection_class [Class] the connection adapter class to extend
220
234
  # @return [void]
221
235
  def enable!(connection_class)
222
- unless connection_class.include?(ConnectionAdapterExtension)
223
- connection_class.prepend(ConnectionAdapterExtension)
224
- end
225
- unless ActiveRecord::ConnectionAdapters::TransactionManager.include?(TransactionManagerExtension)
226
- ActiveRecord::ConnectionAdapters::TransactionManager.prepend(TransactionManagerExtension)
227
- end
236
+ ConnectionAdapterExtension.inject(connection_class)
237
+ TransactionManagerExtension.inject(ActiveRecord::ConnectionAdapters::TransactionManager)
228
238
 
229
239
  @cache_subscription ||= ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start_time, _end_time, _id, payload|
230
240
  if payload[:cached]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_query_counter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-13 00:00:00.000000000 Z
11
+ date: 2023-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  requirements: []
81
- rubygems_version: 3.4.10
81
+ rubygems_version: 3.4.20
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: Count total number of ActiveRecord queries and row counts inside a block