Commit 2cd5861f authored by Gary Holtz's avatar Gary Holtz Committed by Max Woolf

Add initial highlight timeout metrics behind a feature flag [RUN ALL RSPEC] [RUN AS-IF-FOSS]

parent a1eb1f4e
---
name: track_highlight_timeouts
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/60956
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/329909
milestone: '13.12'
type: development
group: group::code review
default_enabled: false
......@@ -64,6 +64,8 @@ module Gitlab
tokens = lexer.lex(text, continue: continue)
Timeout.timeout(timeout_time) { @formatter.format(tokens, context.merge(tag: tag)).html_safe }
rescue Timeout::Error => e
add_highlight_timeout_metric
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
highlight_plain(text)
rescue StandardError
......@@ -81,5 +83,18 @@ module Gitlab
def maximum_text_highlight_size
Gitlab.config.extra['maximum_text_highlight_size_kilobytes']
end
def add_highlight_timeout_metric
return unless Feature.enabled?(:track_highlight_timeouts)
highlight_timeout.increment(source: Gitlab::Runtime.sidekiq? ? "background" : "foreground")
end
def highlight_timeout
@highlight_timeout ||= Gitlab::Metrics.counter(
:highlight_timeout,
'Counts the times highlights have timed out'
)
end
end
end
......@@ -133,5 +133,39 @@ RSpec.describe Gitlab::Highlight do
subject.highlight("Content")
end
end
describe 'highlight timeouts' do
context 'when there is a timeout error while highlighting' do
let(:result) { described_class.highlight(file_name, content) }
before do
allow(Timeout).to receive(:timeout).twice.and_raise(Timeout::Error)
# This is done twice because it's rescued first and then
# calls the original exception
end
it "increments the foreground counter if it's in the foreground" do
expect { result }
.to raise_error(Timeout::Error)
.and change { highlight_timeout_total('foreground') }.by(1)
.and not_change { highlight_timeout_total('background') }
end
it "increments the background counter if it's in the background" do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
expect { result }
.to raise_error(Timeout::Error)
.and change { highlight_timeout_total('background') }.by(1)
.and not_change { highlight_timeout_total('foreground') }
end
end
end
end
def highlight_timeout_total(source)
Gitlab::Metrics
.counter(:highlight_timeout, 'Counts the times highlights have timed out')
.get(source: source)
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