summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPawel Chojnacki <pawel@chojnacki.ws>2017-11-23 01:25:45 +0100
committerPawel Chojnacki <pawel@chojnacki.ws>2017-11-23 23:33:01 +0100
commitefe4cab92b1c93b2beb75fc6b4c0dbe0787d301e (patch)
treef06c337916d60a6fe50e557199af0b6e7aadcc01
parent84c5260f1240ebbe3aa136473324474b0cc0eb8a (diff)
downloadgitlab-ce-efe4cab92b1c93b2beb75fc6b4c0dbe0787d301e.tar.gz
check method timing threshold when observing method performance
-rw-r--r--lib/gitlab/metrics/method_call.rb4
-rw-r--r--spec/lib/gitlab/metrics/method_call_spec.rb65
2 files changed, 59 insertions, 10 deletions
diff --git a/lib/gitlab/metrics/method_call.rb b/lib/gitlab/metrics/method_call.rb
index 5177d953795..03bdb5885a8 100644
--- a/lib/gitlab/metrics/method_call.rb
+++ b/lib/gitlab/metrics/method_call.rb
@@ -59,7 +59,7 @@ module Gitlab
@cpu_time += cpu_time
@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_cpu_duration_histogram.observe(@transaction.labels.merge(labels), cpu_time / 1000.0)
end
@@ -87,7 +87,7 @@ module Gitlab
end
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
diff --git a/spec/lib/gitlab/metrics/method_call_spec.rb b/spec/lib/gitlab/metrics/method_call_spec.rb
index f1e9e414e0d..cffd061a54d 100644
--- a/spec/lib/gitlab/metrics/method_call_spec.rb
+++ b/spec/lib/gitlab/metrics/method_call_spec.rb
@@ -13,16 +13,65 @@ describe Gitlab::Metrics::MethodCall do
expect(method_call.call_count).to eq(1)
end
- it 'observes the performance of the supplied block' do
- expect(described_class.call_real_duration_histogram)
- .to receive(:observe)
- .with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric))
+ context 'when measurement is above threshold' do
+ before do
+ allow(method_call).to receive(:above_threshold?).and_return(true)
+ end
- expect(described_class.call_cpu_duration_histogram)
- .to receive(:observe)
- .with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric))
+ 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
- method_call.measure { 'foo' }
+ it 'observes the performance of the supplied block' do
+ expect(described_class.call_real_duration_histogram)
+ .to receive(:observe)
+ .with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric))
+
+ expect(described_class.call_cpu_duration_histogram)
+ .to receive(:observe)
+ .with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric))
+
+ method_call.measure { 'foo' }
+ 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