Commit 2815df83 authored by Stan Hu's avatar Stan Hu

Merge branch '330894-determine-the-right-file-size-limit-for-highlighting-files' into 'master'

Add an additional metric for determine the right file size limit for highlighting files

See merge request gitlab-org/gitlab!63579
parents 1f3eca19 54be5f6e
...@@ -70,6 +70,8 @@ module Gitlab ...@@ -70,6 +70,8 @@ module Gitlab
end end
def highlight_rich(text, continue: true) def highlight_rich(text, continue: true)
add_highlight_attempt_metric
tag = lexer.tag tag = lexer.tag
tokens = lexer.lex(text, continue: continue) tokens = lexer.lex(text, continue: continue)
Timeout.timeout(timeout_time) { @formatter.format(tokens, context.merge(tag: tag)).html_safe } Timeout.timeout(timeout_time) { @formatter.format(tokens, context.merge(tag: tag)).html_safe }
...@@ -90,12 +92,25 @@ module Gitlab ...@@ -90,12 +92,25 @@ module Gitlab
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text) Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
end end
def add_highlight_attempt_metric
return unless Feature.enabled?(:track_highlight_timeouts)
highlighting_attempt.increment(source: (@language || "undefined"))
end
def add_highlight_timeout_metric def add_highlight_timeout_metric
return unless Feature.enabled?(:track_highlight_timeouts) return unless Feature.enabled?(:track_highlight_timeouts)
highlight_timeout.increment(source: Gitlab::Runtime.sidekiq? ? "background" : "foreground") highlight_timeout.increment(source: Gitlab::Runtime.sidekiq? ? "background" : "foreground")
end end
def highlighting_attempt
@highlight_attempt ||= Gitlab::Metrics.counter(
:file_highlighting_attempt,
'Counts the times highlighting has been attempted on a file'
)
end
def highlight_timeout def highlight_timeout
@highlight_timeout ||= Gitlab::Metrics.counter( @highlight_timeout ||= Gitlab::Metrics.counter(
:highlight_timeout, :highlight_timeout,
......
...@@ -143,9 +143,21 @@ RSpec.describe Gitlab::Highlight do ...@@ -143,9 +143,21 @@ RSpec.describe Gitlab::Highlight do
end end
describe 'highlight timeouts' do describe 'highlight timeouts' do
context 'when there is a timeout error while highlighting' do let(:result) { described_class.highlight(file_name, content, language: "ruby") }
let(:result) { described_class.highlight(file_name, content) }
context 'when there is an attempt' do
it "increments the attempt counter with a defined language" do
expect { result }.to change { highlight_attempt_total("ruby") }
end
it "increments the attempt counter with an undefined language" do
expect do
described_class.highlight(file_name, content)
end.to change { highlight_attempt_total("undefined") }
end
end
context 'when there is a timeout error while highlighting' do
before do before do
allow(Timeout).to receive(:timeout).twice.and_raise(Timeout::Error) allow(Timeout).to receive(:timeout).twice.and_raise(Timeout::Error)
# This is done twice because it's rescued first and then # This is done twice because it's rescued first and then
...@@ -177,6 +189,12 @@ RSpec.describe Gitlab::Highlight do ...@@ -177,6 +189,12 @@ RSpec.describe Gitlab::Highlight do
.get(source: source) .get(source: source)
end end
def highlight_attempt_total(source)
Gitlab::Metrics
.counter(:file_highlighting_attempt, 'Counts the times highlighting has been attempted on a file')
.get(source: source)
end
def over_highlight_size_limit(source) def over_highlight_size_limit(source)
Gitlab::Metrics Gitlab::Metrics
.counter(:over_highlight_size_limit, .counter(:over_highlight_size_limit,
......
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