Commit efe4cab9 authored by Pawel Chojnacki's avatar Pawel Chojnacki

check method timing threshold when observing method performance

parent 84c5260f
...@@ -59,7 +59,7 @@ module Gitlab ...@@ -59,7 +59,7 @@ module Gitlab
@cpu_time += cpu_time @cpu_time += cpu_time
@call_count += 1 @call_count += 1
if prometheus_enabled? if prometheus_enabled? && above_threshold?
self.class.call_real_duration_histogram.observe(@transaction.labels.merge(labels), real_time / 1000.0) self.class.call_real_duration_histogram.observe(@transaction.labels.merge(labels), real_time / 1000.0)
self.class.call_cpu_duration_histogram.observe(@transaction.labels.merge(labels), cpu_time / 1000.0) self.class.call_cpu_duration_histogram.observe(@transaction.labels.merge(labels), cpu_time / 1000.0)
end end
...@@ -87,7 +87,7 @@ module Gitlab ...@@ -87,7 +87,7 @@ module Gitlab
end end
def prometheus_enabled? def prometheus_enabled?
@prometheus_enabled ||= current_application_settings(:prometheus_metrics_method_instrumentation_enabled) @prometheus_enabled ||= Gitlab::CurrentSettings.current_application_settings[:prometheus_metrics_method_instrumentation_enabled]
end end
end end
end end
......
...@@ -13,6 +13,17 @@ describe Gitlab::Metrics::MethodCall do ...@@ -13,6 +13,17 @@ describe Gitlab::Metrics::MethodCall do
expect(method_call.call_count).to eq(1) expect(method_call.call_count).to eq(1)
end end
context 'when measurement is above threshold' do
before do
allow(method_call).to receive(:above_threshold?).and_return(true)
end
context 'prometheus instrumentation is enabled' do
before do
allow(Gitlab::CurrentSettings).to receive(:current_application_settings)
.and_return(prometheus_metrics_method_instrumentation_enabled: true)
end
it 'observes the performance of the supplied block' do it 'observes the performance of the supplied block' do
expect(described_class.call_real_duration_histogram) expect(described_class.call_real_duration_histogram)
.to receive(:observe) .to receive(:observe)
...@@ -26,6 +37,44 @@ describe Gitlab::Metrics::MethodCall do ...@@ -26,6 +37,44 @@ describe Gitlab::Metrics::MethodCall do
end end
end end
context 'prometheus instrumentation is disabled' do
before do
allow(Gitlab::CurrentSettings).to receive(:current_application_settings)
.and_return(prometheus_metrics_method_instrumentation_enabled: false)
end
it 'does not observe the performance' do
expect(described_class.call_real_duration_histogram)
.not_to receive(:observe)
expect(described_class.call_cpu_duration_histogram)
.not_to receive(:observe)
method_call.measure { 'foo' }
end
end
end
context 'when measurement is below threshold' do
before do
allow(method_call).to receive(:above_threshold?).and_return(false)
allow(Gitlab::CurrentSettings).to receive(:current_application_settings)
.and_return(prometheus_metrics_method_instrumentation_enabled: true)
end
it 'does not observe the performance' do
expect(described_class.call_real_duration_histogram)
.not_to receive(:observe)
expect(described_class.call_cpu_duration_histogram)
.not_to receive(:observe)
method_call.measure { 'foo' }
end
end
end
describe '#to_metric' do describe '#to_metric' do
it 'returns a Metric instance' do it 'returns a Metric instance' do
method_call.measure { 'foo' } method_call.measure { 'foo' }
......
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