Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c6c87003
Commit
c6c87003
authored
Apr 15, 2021
by
Alex Kalderimis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Store duration and allow logging to specific file
parent
0f06648f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
12 deletions
+26
-12
spec/support/helpers/query_recorder.rb
spec/support/helpers/query_recorder.rb
+26
-12
No files found.
spec/support/helpers/query_recorder.rb
View file @
c6c87003
...
@@ -3,39 +3,53 @@
...
@@ -3,39 +3,53 @@
module
ActiveRecord
module
ActiveRecord
class
QueryRecorder
class
QueryRecorder
attr_reader
:log
,
:skip_cached
,
:cached
,
:data
attr_reader
:log
,
:skip_cached
,
:cached
,
:data
UNKNOWN
=
%w(unknown unknown)
.
freeze
def
initialize
(
skip_cached:
true
,
query_recorder_debug:
false
,
&
block
)
UNKNOWN
=
%w[unknown unknown]
.
freeze
@data
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
{
count:
0
,
occurrences:
[],
backtrace:
[]
}
}
def
initialize
(
skip_cached:
true
,
log_file:
nil
,
query_recorder_debug:
false
,
&
block
)
@data
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
{
count:
0
,
occurrences:
[],
backtrace:
[],
durations:
[]
}
}
@log
=
[]
@log
=
[]
@cached
=
[]
@cached
=
[]
@skip_cached
=
skip_cached
@skip_cached
=
skip_cached
@query_recorder_debug
=
query_recorder_debug
@query_recorder_debug
=
ENV
[
'QUERY_RECORDER_DEBUG'
]
||
query_recorder_debug
@log_file
=
File
.
open
(
log_file
,
'w'
)
if
log_file
&&
@query_recorder_debug
# force replacement of bind parameters to give tests the ability to check for ids
# force replacement of bind parameters to give tests the ability to check for ids
ActiveRecord
::
Base
.
connection
.
unprepared_statement
do
ActiveRecord
::
Base
.
connection
.
unprepared_statement
do
ActiveSupport
::
Notifications
.
subscribed
(
method
(
:callback
),
'sql.active_record'
,
&
block
)
ActiveSupport
::
Notifications
.
subscribed
(
method
(
:callback
),
'sql.active_record'
,
&
block
)
end
end
end
end
def
show_backtrace
(
values
)
def
show_backtrace
(
values
,
duration
)
values
[
:sql
].
lines
.
each
do
|
line
|
values
[
:sql
].
lines
.
each
do
|
line
|
Rails
.
logger
.
debug
(
"QueryRecorder SQL: -->
#{
line
}
"
)
print_to_log
(
:SQL
,
line
)
end
end
print_to_log
(
:DURATION
,
duration
)
Gitlab
::
BacktraceCleaner
.
clean_backtrace
(
caller
).
each
do
|
line
|
Gitlab
::
BacktraceCleaner
.
clean_backtrace
(
caller
).
each
do
|
line
|
Rails
.
logger
.
debug
(
"QueryRecorder backtrace: -->
#{
line
}
"
)
print_to_log
(
:backtrace
,
line
)
end
end
def
print_to_log
(
label
,
line
)
msg
=
"QueryRecorder
#{
label
}
: -->
#{
line
}
"
if
@log_file
@log_file
.
puts
(
msg
)
else
Rails
.
logger
.
debug
(
msg
)
end
end
end
end
def
get_sql_source
(
sql
)
def
get_sql_source
(
sql
)
matches
=
sql
.
match
(
/,line:(?<line>.*):in\s+`(?<method>.*)'\*\//
)
matches
=
sql
.
match
(
%r{,line:(?<line>.*):in
\s
+`(?<method>.*)'
\*
/}
)
matches
?
[
matches
[
:line
],
matches
[
:method
]]
:
UNKNOWN
matches
?
[
matches
[
:line
],
matches
[
:method
]]
:
UNKNOWN
end
end
def
store_sql_by_source
(
values:
{},
backtrace:
nil
)
def
store_sql_by_source
(
values:
{},
duration:
nil
,
backtrace:
nil
)
full_name
=
get_sql_source
(
values
[
:sql
]).
join
(
':'
)
full_name
=
get_sql_source
(
values
[
:sql
]).
join
(
':'
)
@data
[
full_name
][
:count
]
+=
1
@data
[
full_name
][
:count
]
+=
1
@data
[
full_name
][
:occurrences
]
<<
values
[
:sql
]
@data
[
full_name
][
:occurrences
]
<<
values
[
:sql
]
@data
[
full_name
][
:backtrace
]
<<
backtrace
@data
[
full_name
][
:backtrace
]
<<
backtrace
@data
[
full_name
][
:durations
]
<<
duration
end
end
def
find_query
(
query_regexp
,
limit
,
first_only:
false
)
def
find_query
(
query_regexp
,
limit
,
first_only:
false
)
...
@@ -57,14 +71,14 @@ module ActiveRecord
...
@@ -57,14 +71,14 @@ module ActiveRecord
end
end
def
callback
(
name
,
start
,
finish
,
message_id
,
values
)
def
callback
(
name
,
start
,
finish
,
message_id
,
values
)
store_backtrace
=
ENV
[
'QUERY_RECORDER_DEBUG'
]
||
@query_recorder_debug
duration
=
finish
-
start
backtrace
=
store_backtrace
?
show_backtrace
(
values
)
:
nil
backtrace
=
@query_recorder_debug
?
show_backtrace
(
values
,
duration
)
:
nil
if
values
[
:cached
]
&&
skip_cached
if
values
[
:cached
]
&&
skip_cached
@cached
<<
values
[
:sql
]
@cached
<<
values
[
:sql
]
elsif
!
values
[
:name
]
&
.
include?
(
"SCHEMA"
)
elsif
!
values
[
:name
]
&
.
include?
(
"SCHEMA"
)
@log
<<
values
[
:sql
]
@log
<<
values
[
:sql
]
store_sql_by_source
(
values:
values
,
backtrace:
backtrace
)
store_sql_by_source
(
values:
values
,
duration:
duration
,
backtrace:
backtrace
)
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment