Commit 883b9284 authored by Stan Hu's avatar Stan Hu

Merge branch 'perfbar_grouping' into 'master'

Group performance bar SQL queries by location

See merge request gitlab-org/gitlab!52678
parents e50f7270 262292f2
...@@ -1006,7 +1006,7 @@ installations from source. ...@@ -1006,7 +1006,7 @@ installations from source.
Performance bar statistics (currently only duration of SQL queries) are recorded in that file. For example: Performance bar statistics (currently only duration of SQL queries) are recorded in that file. For example:
```json ```json
{"severity":"INFO","time":"2020-12-04T09:29:44.592Z","correlation_id":"33680b1490ccd35981b03639c406a697","filename":"app/models/ci/pipeline.rb","filenum":"395","method":"each_with_object","request_id":"rYHomD0VJS4","duration_ms":26.889,"type": "sql"} {"severity":"INFO","time":"2020-12-04T09:29:44.592Z","correlation_id":"33680b1490ccd35981b03639c406a697","filename":"app/models/ci/pipeline.rb","method_path":"app/models/ci/pipeline.rb:each_with_object","request_id":"rYHomD0VJS4","duration_ms":26.889,"count":2,"type": "sql"}
``` ```
These statistics are logged on .com only, disabled on self-deployments. These statistics are logged on .com only, disabled on self-deployments.
......
...@@ -27,27 +27,40 @@ module Gitlab ...@@ -27,27 +27,40 @@ module Gitlab
end end
def log_sql_queries(id, data) def log_sql_queries(id, data)
return [] unless queries = data.dig('data', 'active-record', 'details') queries_by_location(data).each do |location, queries|
next unless location
queries.each do |query|
next unless location = parse_backtrace(query['backtrace'])
log_info = location.merge( duration = queries.sum { |query| query['duration'].to_f }
log_info = {
method_path: "#{location[:filename]}:#{location[:method]}",
filename: location[:filename],
type: :sql, type: :sql,
request_id: id, request_id: id,
duration_ms: query['duration'].to_f count: queries.count,
) duration_ms: duration
}
logger.info(log_info) logger.info(log_info)
end end
end end
def queries_by_location(data)
return [] unless queries = data.dig('data', 'active-record', 'details')
queries.group_by do |query|
parse_backtrace(query['backtrace'])
end
end
def parse_backtrace(backtrace) def parse_backtrace(backtrace)
return unless match = /(?<filename>.*):(?<filenum>\d+):in `(?<method>.*)'/.match(backtrace.first) return unless match = /(?<filename>.*):(?<filenum>\d+):in `(?<method>.*)'/.match(backtrace.first)
{ {
filename: match[:filename], filename: match[:filename],
filenum: match[:filenum].to_i, # filenum may change quite frequently with every change in the file,
# because the intention is to aggregate these queries, we group
# them rather by method name which should not change so frequently
# filenum: match[:filenum].to_i,
method: match[:method] method: match[:method]
} }
end end
......
...@@ -22,10 +22,12 @@ RSpec.describe Gitlab::PerformanceBar::Stats do ...@@ -22,10 +22,12 @@ RSpec.describe Gitlab::PerformanceBar::Stats do
expect(logger).to receive(:info) expect(logger).to receive(:info)
.with({ duration_ms: 1.096, filename: 'lib/gitlab/pagination/offset_pagination.rb', .with({ duration_ms: 1.096, filename: 'lib/gitlab/pagination/offset_pagination.rb',
filenum: 53, method: 'add_pagination_headers', request_id: 'foo', type: :sql }) method_path: 'lib/gitlab/pagination/offset_pagination.rb:add_pagination_headers',
count: 1, request_id: 'foo', type: :sql })
expect(logger).to receive(:info) expect(logger).to receive(:info)
.with({ duration_ms: 0.817, filename: 'lib/api/helpers.rb', .with({ duration_ms: 1.634, filename: 'lib/api/helpers.rb',
filenum: 112, method: 'find_project', request_id: 'foo', type: :sql }).twice method_path: 'lib/api/helpers.rb:find_project',
count: 2, request_id: 'foo', type: :sql })
subject subject
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment