Commit d304f883 authored by Sean McGivern's avatar Sean McGivern

Expose a clean_backtrace method from Gitlab::Profiler

This method makes it easier for other parts of the app to get clean backtraces,
as well as console users.
parent 8db7e253
...@@ -11,6 +11,7 @@ module Gitlab ...@@ -11,6 +11,7 @@ module Gitlab
lib/gitlab/etag_caching/ lib/gitlab/etag_caching/
lib/gitlab/metrics/ lib/gitlab/metrics/
lib/gitlab/middleware/ lib/gitlab/middleware/
ee/lib/gitlab/middleware/
lib/gitlab/performance_bar/ lib/gitlab/performance_bar/
lib/gitlab/request_profiler/ lib/gitlab/request_profiler/
lib/gitlab/profiler.rb lib/gitlab/profiler.rb
...@@ -98,11 +99,7 @@ module Gitlab ...@@ -98,11 +99,7 @@ module Gitlab
super super
backtrace = Rails.backtrace_cleaner.clean(caller) Gitlab::Profiler.clean_backtrace(caller).each do |caller_line|
backtrace.each do |caller_line|
next if caller_line.match(Regexp.union(IGNORE_BACKTRACES))
stripped_caller_line = caller_line.sub("#{Rails.root}/", '') stripped_caller_line = caller_line.sub("#{Rails.root}/", '')
super(" ↳ #{stripped_caller_line}") super(" ↳ #{stripped_caller_line}")
...@@ -112,6 +109,12 @@ module Gitlab ...@@ -112,6 +109,12 @@ module Gitlab
end end
end end
def self.clean_backtrace(backtrace)
Array(Rails.backtrace_cleaner.clean(backtrace)).reject do |line|
line.match(Regexp.union(IGNORE_BACKTRACES))
end
end
def self.with_custom_logger(logger) def self.with_custom_logger(logger)
original_colorize_logging = ActiveSupport::LogSubscriber.colorize_logging original_colorize_logging = ActiveSupport::LogSubscriber.colorize_logging
original_activerecord_logger = ActiveRecord::Base.logger original_activerecord_logger = ActiveRecord::Base.logger
......
...@@ -135,6 +135,51 @@ describe Gitlab::Profiler do ...@@ -135,6 +135,51 @@ describe Gitlab::Profiler do
end end
end end
describe '.clean_backtrace' do
it 'uses the Rails backtrace cleaner' do
backtrace = []
expect(Rails.backtrace_cleaner).to receive(:clean).with(backtrace)
described_class.clean_backtrace(backtrace)
end
it 'removes lines from IGNORE_BACKTRACES' do
backtrace = [
"lib/gitlab/gitaly_client.rb:294:in `block (2 levels) in migrate'",
"lib/gitlab/gitaly_client.rb:331:in `allow_n_plus_1_calls'",
"lib/gitlab/gitaly_client.rb:280:in `block in migrate'",
"lib/gitlab/metrics/influx_db.rb:103:in `measure'",
"lib/gitlab/gitaly_client.rb:278:in `migrate'",
"lib/gitlab/git/repository.rb:1451:in `gitaly_migrate'",
"lib/gitlab/git/commit.rb:66:in `find'",
"app/models/repository.rb:1047:in `find_commit'",
"lib/gitlab/metrics/instrumentation.rb:159:in `block in find_commit'",
"lib/gitlab/metrics/method_call.rb:36:in `measure'",
"lib/gitlab/metrics/instrumentation.rb:159:in `find_commit'",
"app/models/repository.rb:113:in `commit'",
"lib/gitlab/i18n.rb:50:in `with_locale'",
"lib/gitlab/middleware/multipart.rb:95:in `call'",
"lib/gitlab/request_profiler/middleware.rb:14:in `call'",
"ee/lib/gitlab/database/load_balancing/rack_middleware.rb:37:in `call'",
"ee/lib/gitlab/jira/middleware.rb:15:in `call'"
]
expect(described_class.clean_backtrace(backtrace))
.to eq([
"lib/gitlab/gitaly_client.rb:294:in `block (2 levels) in migrate'",
"lib/gitlab/gitaly_client.rb:331:in `allow_n_plus_1_calls'",
"lib/gitlab/gitaly_client.rb:280:in `block in migrate'",
"lib/gitlab/gitaly_client.rb:278:in `migrate'",
"lib/gitlab/git/repository.rb:1451:in `gitaly_migrate'",
"lib/gitlab/git/commit.rb:66:in `find'",
"app/models/repository.rb:1047:in `find_commit'",
"app/models/repository.rb:113:in `commit'",
"ee/lib/gitlab/jira/middleware.rb:15:in `call'"
])
end
end
describe '.with_custom_logger' do describe '.with_custom_logger' do
context 'when the logger is set' do context 'when the logger is set' do
it 'uses the replacement logger for the duration of the block' do it 'uses the replacement logger for the duration of the block' do
......
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