active_record_query_counter 2.3.0 → 3.0.0
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 +14 -0
- data/README.md +17 -0
- data/VERSION +1 -1
- data/active_record_query_counter.gemspec +2 -2
- data/lib/active_record_query_counter/connection_adapter_extension.rb +39 -13
- data/lib/active_record_query_counter.rb +61 -6
- metadata +6 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 89a919244edbce9bf36bdaa232078b04470f1bacc952469eab4a4fffc2416e08
|
|
4
|
+
data.tar.gz: 97a66d47fda85ad15b832da3e390f8cc035a374983c64f53e4d673094b54d8b9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c5c657cf312deb55e21063b5d08f824cd762e42050704b06fc0610e00f938aacac8fc37050c60d293452521841ca614430cb50cd3bf10d3bca794df9d0dee6d0
|
|
7
|
+
data.tar.gz: d81cb599eea68c911fad417a902848f1b1f2d0f29f4d095b70d5af8ef2f6366dac0e2b2de0fe534ed51fcbffb3d696147edfc53e64b549862a4dfe78a156a54d
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,20 @@ 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
|
+
## 3.0.0
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Query time now excludes GC pause time and Ruby thread CPU time so that it more closely reflects the time actually spent waiting on the database. This is what is now reported as the event duration in the `query_time` and `row_count` notifications.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- Added `:elapsed_time` (the raw wall clock time), `:gc_time`, and `:cpu_time` (all in milliseconds) to the `query_time` and `row_count` notification payloads.
|
|
16
|
+
|
|
17
|
+
### Removed
|
|
18
|
+
|
|
19
|
+
- Dropped support for Ruby versions older than 3.1 (required for `GC.total_time`).
|
|
20
|
+
|
|
7
21
|
## 2.3.0
|
|
8
22
|
|
|
9
23
|
### Added
|
data/README.md
CHANGED
|
@@ -55,6 +55,15 @@ ActiveRecordQueryCounter.count_queries do
|
|
|
55
55
|
end
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
### Query Time
|
|
59
|
+
|
|
60
|
+
The query time (`ActiveRecordQueryCounter.query_time` and the duration reported by the notifications) is **not** the raw wall clock time a query took. The wall clock time includes time the thread was not actually waiting on the database, such as GC pauses (which can be triggered by other threads and stop the world) and the Ruby CPU work of building the result objects. On a busy, multi-threaded server these can add up to seconds, making a trivial query look pathologically slow.
|
|
61
|
+
|
|
62
|
+
To report the time actually spent waiting on the database as closely as possible, the GC time and thread CPU time that elapsed while the query ran are subtracted from the wall clock time. The raw wall clock time is still available as `:elapsed_time` in the notification payloads.
|
|
63
|
+
|
|
64
|
+
> [!NOTE]
|
|
65
|
+
> Measuring GC time requires Ruby's GC total time measurement, which is enabled by default (`GC.measure_total_time`). Thread CPU time is measured via `Process::CLOCK_THREAD_CPUTIME_ID`; on platforms that do not provide it, CPU time is treated as zero.
|
|
66
|
+
|
|
58
67
|
### Middleware Integration
|
|
59
68
|
|
|
60
69
|
For **Rails** and **Sidekiq**, middleware is included to enable query counting in web requests and workers.
|
|
@@ -106,6 +115,11 @@ Triggered when a query exceeds the query_time threshold with the payload:
|
|
|
106
115
|
- `:binds` - The bind parameters that were used.
|
|
107
116
|
- `:row_count` - The number of rows returned.
|
|
108
117
|
- `:trace` - The stack trace of where the query was executed.
|
|
118
|
+
- `:elapsed_time` - The raw wall clock time the query took (in milliseconds).
|
|
119
|
+
- `:gc_time` - The GC time that elapsed while the query ran (in milliseconds).
|
|
120
|
+
- `:cpu_time` - The thread CPU time spent while the query ran (in milliseconds).
|
|
121
|
+
|
|
122
|
+
The duration of the notification event is the query time: the wall clock time with the GC time and CPU time subtracted out (see [Query Time](#query-time)). The raw wall clock time is still available as `:elapsed_time`.
|
|
109
123
|
|
|
110
124
|
##### 2. active_record_query_counter.row_count notification
|
|
111
125
|
|
|
@@ -115,6 +129,9 @@ Triggered when a query exceeds the row_count threshold with the payload:
|
|
|
115
129
|
- `:binds` - The bind parameters that were used.
|
|
116
130
|
- `:row_count` - The number of rows returned.
|
|
117
131
|
- `:trace` - The stack trace of where the query was executed.
|
|
132
|
+
- `:elapsed_time` - The raw wall clock time the query took (in milliseconds).
|
|
133
|
+
- `:gc_time` - The GC time that elapsed while the query ran (in milliseconds).
|
|
134
|
+
- `:cpu_time` - The thread CPU time spent while the query ran (in milliseconds).
|
|
118
135
|
|
|
119
136
|
##### 3. active_record_query_counter.transaction_time notification
|
|
120
137
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
3.0.0
|
|
@@ -34,9 +34,9 @@ Gem::Specification.new do |spec|
|
|
|
34
34
|
|
|
35
35
|
spec.require_paths = ["lib"]
|
|
36
36
|
|
|
37
|
-
spec.add_dependency "activerecord", ">=
|
|
37
|
+
spec.add_dependency "activerecord", ">= 6.0"
|
|
38
38
|
|
|
39
39
|
spec.add_development_dependency "bundler"
|
|
40
40
|
|
|
41
|
-
spec.required_ruby_version = ">=
|
|
41
|
+
spec.required_ruby_version = ">= 3.1"
|
|
42
42
|
end
|
|
@@ -3,37 +3,63 @@
|
|
|
3
3
|
module ActiveRecordQueryCounter
|
|
4
4
|
# Module to prepend to the connection adapter to inject the counting behavior.
|
|
5
5
|
module ConnectionAdapterExtension
|
|
6
|
+
# Clock used to measure the CPU time consumed by the current thread while a query runs.
|
|
7
|
+
# It is not available on every platform (e.g. Windows), in which case CPU time is not
|
|
8
|
+
# measured and is treated as zero.
|
|
9
|
+
CPU_CLOCK_ID = (Process::CLOCK_THREAD_CPUTIME_ID if defined?(Process::CLOCK_THREAD_CPUTIME_ID))
|
|
10
|
+
|
|
6
11
|
class << self
|
|
7
12
|
def inject(connection_class)
|
|
8
13
|
# Rails 7.1+ uses internal_exec_query instead of exec_query.
|
|
9
|
-
mod = (connection_class.
|
|
14
|
+
mod = (connection_class.method_defined?(:internal_exec_query) ? InternalExecQuery : ExecQuery)
|
|
10
15
|
unless connection_class.include?(mod)
|
|
11
16
|
connection_class.prepend(mod)
|
|
12
17
|
end
|
|
13
18
|
end
|
|
14
|
-
end
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
# Measure a query by wrapping its execution. In addition to the wall clock time, the GC
|
|
21
|
+
# time and thread CPU time spent while the query runs are captured so that the time
|
|
22
|
+
# actually spent waiting on the database can be isolated from time lost to garbage
|
|
23
|
+
# collection and Ruby VM work.
|
|
24
|
+
#
|
|
25
|
+
# @param sql [String] the SQL statement being executed
|
|
26
|
+
# @param name [String, nil] the name of the query
|
|
27
|
+
# @param binds [Array] the bind parameters
|
|
28
|
+
# @yield executes the query and returns its result
|
|
29
|
+
# @return [Object] the result of the query
|
|
30
|
+
def measure_query(sql, name, binds)
|
|
31
|
+
gc_start = GC.total_time
|
|
32
|
+
cpu_start = current_cpu_time
|
|
18
33
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
19
|
-
result =
|
|
34
|
+
result = yield
|
|
20
35
|
if result.is_a?(ActiveRecord::Result)
|
|
21
36
|
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
22
|
-
|
|
37
|
+
cpu_time = current_cpu_time - cpu_start
|
|
38
|
+
gc_time = (GC.total_time - gc_start) / 1_000_000_000.0
|
|
39
|
+
ActiveRecordQueryCounter.add_query(sql, name, binds, result.length, start_time, end_time, gc_time, cpu_time)
|
|
23
40
|
end
|
|
24
41
|
result
|
|
25
42
|
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# The current thread CPU time in seconds, or 0.0 when the platform does not support it.
|
|
47
|
+
#
|
|
48
|
+
# @return [Float]
|
|
49
|
+
def current_cpu_time
|
|
50
|
+
CPU_CLOCK_ID ? Process.clock_gettime(CPU_CLOCK_ID) : 0.0
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
module ExecQuery
|
|
55
|
+
def exec_query(sql, name = nil, binds = [], *args, **kwargs)
|
|
56
|
+
ConnectionAdapterExtension.measure_query(sql, name, binds) { super }
|
|
57
|
+
end
|
|
26
58
|
end
|
|
27
59
|
|
|
28
60
|
module InternalExecQuery
|
|
29
61
|
def internal_exec_query(sql, name = nil, binds = [], *args, **kwargs)
|
|
30
|
-
|
|
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
|
|
62
|
+
ConnectionAdapterExtension.measure_query(sql, name, binds) { super }
|
|
37
63
|
end
|
|
38
64
|
end
|
|
39
65
|
end
|
|
@@ -65,32 +65,51 @@ module ActiveRecordQueryCounter
|
|
|
65
65
|
|
|
66
66
|
# Increment the query counters.
|
|
67
67
|
#
|
|
68
|
+
# The reported query time is the wall clock time spent executing the query with the GC
|
|
69
|
+
# time and Ruby thread CPU time subtracted out so that it reflects the time actually
|
|
70
|
+
# spent waiting on the database as closely as possible (see {.database_query_time}). This
|
|
71
|
+
# query time, rather than the raw wall clock time, is what is accumulated, compared against
|
|
72
|
+
# the threshold, and used as the duration of the emitted notification.
|
|
73
|
+
#
|
|
74
|
+
# @param sql [String] the SQL statement that was executed
|
|
75
|
+
# @param name [String, nil] the name of the query
|
|
76
|
+
# @param binds [Array] the bind parameters
|
|
68
77
|
# @param row_count [Integer] the number of rows returned by the query
|
|
69
|
-
# @param
|
|
78
|
+
# @param start_time [Float] the monotonic time when the query started
|
|
79
|
+
# @param end_time [Float] the monotonic time when the query ended
|
|
80
|
+
# @param gc_time [Float] the GC time in seconds that elapsed while the query ran
|
|
81
|
+
# @param cpu_time [Float] the thread CPU time in seconds spent while the query ran
|
|
70
82
|
# @return [void]
|
|
71
83
|
# @api private
|
|
72
|
-
def add_query(sql, name, binds, row_count, start_time, end_time)
|
|
84
|
+
def add_query(sql, name, binds, row_count, start_time, end_time, gc_time, cpu_time)
|
|
73
85
|
return if IGNORED_STATEMENTS.include?(name)
|
|
74
86
|
|
|
75
87
|
counter = current_counter
|
|
76
88
|
return unless counter.is_a?(Counter)
|
|
77
89
|
|
|
78
90
|
elapsed_time = end_time - start_time
|
|
91
|
+
query_time = database_query_time(elapsed_time, gc_time, cpu_time)
|
|
79
92
|
counter.query_count += 1
|
|
80
93
|
counter.row_count += row_count
|
|
81
|
-
counter.query_time +=
|
|
94
|
+
counter.query_time += query_time
|
|
95
|
+
|
|
96
|
+
# The notification duration is the database query time, so the event ends that long after
|
|
97
|
+
# it started rather than at the raw wall clock end time.
|
|
98
|
+
notification_end_time = start_time + query_time
|
|
82
99
|
|
|
83
100
|
trace = nil
|
|
84
101
|
query_time_threshold = (counter.thresholds.query_time || -1)
|
|
85
|
-
if query_time_threshold >= 0 &&
|
|
102
|
+
if query_time_threshold >= 0 && query_time >= query_time_threshold
|
|
86
103
|
trace = backtrace
|
|
87
|
-
|
|
104
|
+
payload = notification_payload(sql: sql, binds: binds, row_count: row_count, trace: trace, elapsed_time: elapsed_time, gc_time: gc_time, cpu_time: cpu_time)
|
|
105
|
+
send_notification("query_time", start_time, notification_end_time, **payload)
|
|
88
106
|
end
|
|
89
107
|
|
|
90
108
|
row_count_threshold = (counter.thresholds.row_count || -1)
|
|
91
109
|
if row_count_threshold >= 0 && row_count >= row_count_threshold
|
|
92
110
|
trace ||= backtrace
|
|
93
|
-
|
|
111
|
+
payload = notification_payload(sql: sql, binds: binds, row_count: row_count, trace: trace, elapsed_time: elapsed_time, gc_time: gc_time, cpu_time: cpu_time)
|
|
112
|
+
send_notification("row_count", start_time, notification_end_time, **payload)
|
|
94
113
|
end
|
|
95
114
|
end
|
|
96
115
|
|
|
@@ -283,6 +302,42 @@ module ActiveRecordQueryCounter
|
|
|
283
302
|
ActiveSupport::Notifications.publish("active_record_query_counter.#{name}", start_time, end_time, id, payload)
|
|
284
303
|
end
|
|
285
304
|
|
|
305
|
+
def notification_payload(sql:, binds:, row_count:, trace:, elapsed_time:, gc_time:, cpu_time:)
|
|
306
|
+
{
|
|
307
|
+
sql: sql,
|
|
308
|
+
binds: binds,
|
|
309
|
+
row_count: row_count,
|
|
310
|
+
trace: trace,
|
|
311
|
+
elapsed_time: (elapsed_time * 1000.0).round(6),
|
|
312
|
+
gc_time: (gc_time * 1000.0).round(6),
|
|
313
|
+
cpu_time: (cpu_time * 1000.0).round(6)
|
|
314
|
+
}
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Estimate the time spent waiting on the database by subtracting the GC time and thread CPU
|
|
318
|
+
# time from the wall clock time the query took.
|
|
319
|
+
#
|
|
320
|
+
# The GC time and CPU time normally measure distinct, non-overlapping intervals: a GC pause
|
|
321
|
+
# triggered by another thread happens while this thread is parked waiting on the database
|
|
322
|
+
# (off CPU, so it does not count as CPU time), while CPU time covers the Ruby work of
|
|
323
|
+
# building the result. They only overlap when the query's own thread triggers a GC, which
|
|
324
|
+
# runs on that thread and so counts as both GC time and CPU time. When that overlap is large
|
|
325
|
+
# enough to drive the result negative, only the larger of the two is subtracted so the shared
|
|
326
|
+
# interval is removed once. The result is clamped so it never exceeds the wall clock time and
|
|
327
|
+
# is never negative.
|
|
328
|
+
#
|
|
329
|
+
# @param elapsed_time [Float] the wall clock time the query took in seconds
|
|
330
|
+
# @param gc_time [Float] the GC time in seconds that elapsed while the query ran
|
|
331
|
+
# @param cpu_time [Float] the thread CPU time in seconds spent while the query ran
|
|
332
|
+
# @return [Float] the estimated database time in seconds
|
|
333
|
+
def database_query_time(elapsed_time, gc_time, cpu_time)
|
|
334
|
+
return 0.0 if elapsed_time <= 0.0
|
|
335
|
+
|
|
336
|
+
query_time = elapsed_time - (gc_time + cpu_time)
|
|
337
|
+
query_time = elapsed_time - [gc_time, cpu_time].max if query_time.negative?
|
|
338
|
+
query_time.clamp(0.0, elapsed_time)
|
|
339
|
+
end
|
|
340
|
+
|
|
286
341
|
def backtrace
|
|
287
342
|
caller.reject { |line| line.start_with?(__dir__) }
|
|
288
343
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_record_query_counter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brian Durand
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -16,14 +15,14 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '6.0'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '6.0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: bundler
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -38,7 +37,6 @@ dependencies:
|
|
|
38
37
|
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
39
|
version: '0'
|
|
41
|
-
description:
|
|
42
40
|
email:
|
|
43
41
|
- bbdurand@gmail.com
|
|
44
42
|
executables: []
|
|
@@ -66,7 +64,6 @@ metadata:
|
|
|
66
64
|
homepage_uri: https://github.com/bdurand/active_record_query_counter
|
|
67
65
|
source_code_uri: https://github.com/bdurand/active_record_query_counter
|
|
68
66
|
changelog_uri: https://github.com/bdurand/active_record_query_counter/blob/main/CHANGELOG.md
|
|
69
|
-
post_install_message:
|
|
70
67
|
rdoc_options: []
|
|
71
68
|
require_paths:
|
|
72
69
|
- lib
|
|
@@ -74,15 +71,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
74
71
|
requirements:
|
|
75
72
|
- - ">="
|
|
76
73
|
- !ruby/object:Gem::Version
|
|
77
|
-
version: '
|
|
74
|
+
version: '3.1'
|
|
78
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
76
|
requirements:
|
|
80
77
|
- - ">="
|
|
81
78
|
- !ruby/object:Gem::Version
|
|
82
79
|
version: '0'
|
|
83
80
|
requirements: []
|
|
84
|
-
rubygems_version: 3.
|
|
85
|
-
signing_key:
|
|
81
|
+
rubygems_version: 3.6.9
|
|
86
82
|
specification_version: 4
|
|
87
83
|
summary: Provides detailed insights into how your code interacts with the database
|
|
88
84
|
by hooking into ActiveRecord.
|