active_record_query_counter 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +33 -1
- data/VERSION +1 -1
- data/lib/active_record_query_counter/connection_adapter_extension.rb +31 -7
- data/lib/active_record_query_counter/thresholds.rb +5 -1
- data/lib/active_record_query_counter/transaction_manager_extension.rb +8 -0
- data/lib/active_record_query_counter.rb +21 -8
- metadata +4 -4
- /data/{MIT_LICENSE → MIT_LICENSE.txt} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 752c1c799ab4ee023a24f4f8c3550da35831fdcebf39e71c9bb945bbf4ca7ff9
|
4
|
+
data.tar.gz: ecd642c76f010996f40d0a6d791db96c06515137b2e1393d5655c1feae314b23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4a88b4283290565e8352d35f6c0b5483515dbcacbd17fb223fc90398b94d50ac2984519f84c6a188a576468cb1f9ea373edfcc6855301e78522676558511e9d
|
7
|
+
data.tar.gz: ff360b2f495c953f79668a27520722c425c94d9cafe6607dcae40d25b65d10dd8f38f7b0d478771495927a5229ecf1e6948328b6adb87be5d6d186c77727d4f2
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,19 @@ 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.1
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
|
11
|
+
- Classes not required to run the gem are now lazy loaded.
|
12
|
+
|
13
|
+
## 2.2.0
|
14
|
+
|
15
|
+
### Added
|
16
|
+
|
17
|
+
- Added `ActiveRecordQueryCounter.disable` method to allow disabling query counting behavior within a block.
|
18
|
+
- Rails 7.1 compatibility.
|
19
|
+
|
7
20
|
## 2.1.0
|
8
21
|
|
9
22
|
### Added
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# ActiveRecordQueryCounter
|
2
2
|
|
3
|
-
](https://github.com/bdurand/active_record_query_counter/actions/workflows/continuous_integration.yml)
|
4
|
+
[](https://github.com/bdurand/active_record_query_counter/actions/workflows/regression_test.yml)
|
4
5
|
[](https://github.com/testdouble/standard)
|
6
|
+
[](https://badge.fury.io/rb/active_record_query_counter)
|
5
7
|
|
6
8
|
This gem injects itself into ActiveRecord to give you insight into how your code is using the database.
|
7
9
|
|
@@ -60,6 +62,18 @@ Sidekiq.configure_server do |config|
|
|
60
62
|
end
|
61
63
|
```
|
62
64
|
|
65
|
+
If you want to disable query counting within a block of code, you can use the `disable` method.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
ActiveRecordQueryCounter.count_queries do
|
69
|
+
do_something
|
70
|
+
ActiveRecordQueryCounter.disable do
|
71
|
+
# Queries will not be counted in this block.
|
72
|
+
do_something_else
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
63
77
|
### Notifications
|
64
78
|
|
65
79
|
You can also subscribe to ActiveSupport notifications to get notified when query thresholds are exceeded.
|
@@ -192,6 +206,24 @@ ActiveSupport::Notifications.subscribe('active_record_query_counter.transaction_
|
|
192
206
|
end
|
193
207
|
```
|
194
208
|
|
209
|
+
## Installation
|
210
|
+
|
211
|
+
Add this line to your application's Gemfile:
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
gem 'active_record_query_counter'
|
215
|
+
```
|
216
|
+
|
217
|
+
And then execute:
|
218
|
+
```bash
|
219
|
+
$ bundle
|
220
|
+
```
|
221
|
+
|
222
|
+
Or install it yourself as:
|
223
|
+
```bash
|
224
|
+
$ gem install active_record_query_counter
|
225
|
+
```
|
226
|
+
|
195
227
|
## Contributing
|
196
228
|
|
197
229
|
Open a pull request on GitHub.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1
|
1
|
+
2.2.1
|
@@ -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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
@@ -30,7 +34,7 @@ module ActiveRecordQueryCounter
|
|
30
34
|
values.each do |key, value|
|
31
35
|
setter = "#{key}="
|
32
36
|
if respond_to?(setter)
|
33
|
-
public_send("#{key}=", value)
|
37
|
+
public_send(:"#{key}=", value)
|
34
38
|
else
|
35
39
|
raise ArgumentError, "Unknown threshold: #{key}"
|
36
40
|
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)
|
@@ -2,12 +2,9 @@
|
|
2
2
|
|
3
3
|
require_relative "active_record_query_counter/connection_adapter_extension"
|
4
4
|
require_relative "active_record_query_counter/counter"
|
5
|
-
require_relative "active_record_query_counter/rack_middleware"
|
6
|
-
require_relative "active_record_query_counter/sidekiq_middleware"
|
7
5
|
require_relative "active_record_query_counter/thresholds"
|
8
6
|
require_relative "active_record_query_counter/transaction_info"
|
9
7
|
require_relative "active_record_query_counter/transaction_manager_extension"
|
10
|
-
require_relative "active_record_query_counter/version"
|
11
8
|
|
12
9
|
# Everything you need to count ActiveRecord queries and row counts within a block.
|
13
10
|
#
|
@@ -19,6 +16,10 @@ require_relative "active_record_query_counter/version"
|
|
19
16
|
# puts ActiveRecordQueryCounter.row_count
|
20
17
|
# end
|
21
18
|
module ActiveRecordQueryCounter
|
19
|
+
autoload :RackMiddleware, "active_record_query_counter/rack_middleware"
|
20
|
+
autoload :SidekiqMiddleware, "active_record_query_counter/sidekiq_middleware"
|
21
|
+
autoload :VERSION, "active_record_query_counter/version"
|
22
|
+
|
22
23
|
IGNORED_STATEMENTS = %w[SCHEMA EXPLAIN].freeze
|
23
24
|
private_constant :IGNORED_STATEMENTS
|
24
25
|
|
@@ -48,6 +49,20 @@ module ActiveRecordQueryCounter
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
52
|
+
# Disable query counting in a block. Any queries or transactions inside the block will not
|
53
|
+
# be counted.
|
54
|
+
#
|
55
|
+
# @return [Object] the return value of the block
|
56
|
+
def disable(&block)
|
57
|
+
counter = current_counter
|
58
|
+
begin
|
59
|
+
self.current_counter = nil
|
60
|
+
yield
|
61
|
+
ensure
|
62
|
+
self.current_counter = counter
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
51
66
|
# Increment the query counters.
|
52
67
|
#
|
53
68
|
# @param row_count [Integer] the number of rows returned by the query
|
@@ -219,11 +234,9 @@ module ActiveRecordQueryCounter
|
|
219
234
|
# @param connection_class [Class] the connection adapter class to extend
|
220
235
|
# @return [void]
|
221
236
|
def enable!(connection_class)
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
unless ActiveRecord::ConnectionAdapters::TransactionManager.include?(TransactionManagerExtension)
|
226
|
-
ActiveRecord::ConnectionAdapters::TransactionManager.prepend(TransactionManagerExtension)
|
237
|
+
ActiveSupport.on_load(:active_record) do
|
238
|
+
ConnectionAdapterExtension.inject(connection_class)
|
239
|
+
TransactionManagerExtension.inject(ActiveRecord::ConnectionAdapters::TransactionManager)
|
227
240
|
end
|
228
241
|
|
229
242
|
@cache_subscription ||= ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start_time, _end_time, _id, payload|
|
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
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -46,7 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- CHANGELOG.md
|
49
|
-
- MIT_LICENSE
|
49
|
+
- MIT_LICENSE.txt
|
50
50
|
- README.md
|
51
51
|
- VERSION
|
52
52
|
- active_record_query_counter.gemspec
|
@@ -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.
|
81
|
+
rubygems_version: 3.4.12
|
82
82
|
signing_key:
|
83
83
|
specification_version: 4
|
84
84
|
summary: Count total number of ActiveRecord queries and row counts inside a block
|
File without changes
|