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
end
def highlight_rich(text, continue: true)
add_highlight_attempt_metric
tag = lexer.tag
tokens = lexer.lex(text, continue: continue)
Timeout.timeout(timeout_time) { @formatter.format(tokens, context.merge(tag: tag)).html_safe }
......@@ -90,12 +92,25 @@ module Gitlab
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
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
return unless Feature.enabled?(:track_highlight_timeouts)
highlight_timeout.increment(source: Gitlab::Runtime.sidekiq? ? "background" : "foreground")
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
@highlight_timeout ||= Gitlab::Metrics.counter(
:highlight_timeout,
......
......@@ -143,9 +143,21 @@ RSpec.describe Gitlab::Highlight do
end
describe 'highlight timeouts' do
context 'when there is a timeout error while highlighting' do
let(:result) { described_class.highlight(file_name, content) }
let(:result) { described_class.highlight(file_name, content, language: "ruby") }
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
allow(Timeout).to receive(:timeout).twice.and_raise(Timeout::Error)
# This is done twice because it's rescued first and then
......@@ -177,6 +189,12 @@ RSpec.describe Gitlab::Highlight do
.get(source: source)
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)
Gitlab::Metrics
.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