Commit d97548da authored by Pawel Chojnacki's avatar Pawel Chojnacki

cleanup method call

parent e5d61415
...@@ -43,12 +43,14 @@ module Gitlab ...@@ -43,12 +43,14 @@ module Gitlab
return @@_metric_provider_cached_#{name} if @@_metric_provider_cached_#{name} return @@_metric_provider_cached_#{name} if @@_metric_provider_cached_#{name}
@@_metrics_provider_mutex.synchronize do @@_metrics_provider_mutex.synchronize do
puts "Initiaalized"
@@_metric_provider_cached_#{name} ||= #{metric_fetching_code} @@_metric_provider_cached_#{name} ||= #{metric_fetching_code}
end end
end end
def reload_#{name}!
@@_metric_provider_cached_#{name} = nil
end
METRIC METRIC
puts method_code
instance_eval(method_code, __FILE__, line) instance_eval(method_code, __FILE__, line)
module_eval(method_code, __FILE__, line) module_eval(method_code, __FILE__, line)
......
...@@ -41,7 +41,7 @@ module Gitlab ...@@ -41,7 +41,7 @@ module Gitlab
@call_count += 1 @call_count += 1
if above_threshold? if above_threshold?
gitlab_method_call_duration_seconds.observe(@transaction.labels.merge(labels), real_time) self.class.gitlab_method_call_duration_seconds.observe(@transaction.labels.merge(labels), real_time)
end end
retval retval
......
...@@ -5,6 +5,10 @@ describe Gitlab::Metrics::MethodCall do ...@@ -5,6 +5,10 @@ describe Gitlab::Metrics::MethodCall do
let(:method_call) { described_class.new('Foo#bar', :Foo, '#bar', transaction) } let(:method_call) { described_class.new('Foo#bar', :Foo, '#bar', transaction) }
describe '#measure' do describe '#measure' do
before do
described_class.reload_gitlab_method_call_duration_seconds!
end
it 'measures the performance of the supplied block' do it 'measures the performance of the supplied block' do
method_call.measure { 'foo' } method_call.measure { 'foo' }
...@@ -20,8 +24,6 @@ describe Gitlab::Metrics::MethodCall do ...@@ -20,8 +24,6 @@ describe Gitlab::Metrics::MethodCall do
context 'prometheus instrumentation is enabled' do context 'prometheus instrumentation is enabled' do
before do before do
allow(Feature.get(:prometheus_metrics_method_instrumentation)).to receive(:enabled?).and_call_original
described_class.measurement_enabled_cache_expires_at.value = Time.now.to_i - 1
Feature.get(:prometheus_metrics_method_instrumentation).enable Feature.get(:prometheus_metrics_method_instrumentation).enable
end end
...@@ -31,30 +33,12 @@ describe Gitlab::Metrics::MethodCall do ...@@ -31,30 +33,12 @@ describe Gitlab::Metrics::MethodCall do
end end
end end
it 'caches subsequent invocations of feature check' do it 'metric is not a NullMetric' do
10.times do expect(described_class).not_to be_instance_of(Gitlab::Metrics::NullMetric)
method_call.measure { 'foo' }
end
expect(Feature.get(:prometheus_metrics_method_instrumentation)).to have_received(:enabled?).once
end
it 'expires feature check cache after 1 minute' do
method_call.measure { 'foo' }
Timecop.travel(1.minute.from_now) do
method_call.measure { 'foo' }
end
Timecop.travel(1.minute.from_now + 1.second) do
method_call.measure { 'foo' }
end
expect(Feature.get(:prometheus_metrics_method_instrumentation)).to have_received(:enabled?).twice
end end
it 'observes the performance of the supplied block' do it 'observes the performance of the supplied block' do
expect(described_class.call_duration_histogram) expect(described_class.gitlab_method_call_duration_seconds)
.to receive(:observe) .to receive(:observe)
.with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric)) .with({ module: :Foo, method: '#bar' }, be_a_kind_of(Numeric))
...@@ -64,14 +48,12 @@ describe Gitlab::Metrics::MethodCall do ...@@ -64,14 +48,12 @@ describe Gitlab::Metrics::MethodCall do
context 'prometheus instrumentation is disabled' do context 'prometheus instrumentation is disabled' do
before do before do
described_class.measurement_enabled_cache_expires_at.value = Time.now.to_i - 1
Feature.get(:prometheus_metrics_method_instrumentation).disable Feature.get(:prometheus_metrics_method_instrumentation).disable
end end
it 'does not observe the performance' do it 'observes using NullMetric' do
expect(described_class.call_duration_histogram) expect(described_class.gitlab_method_call_duration_seconds).to be_instance_of(Gitlab::Metrics::NullMetric)
.not_to receive(:observe) expect(described_class.gitlab_method_call_duration_seconds).to receive(:observe)
method_call.measure { 'foo' } method_call.measure { 'foo' }
end end
...@@ -81,12 +63,10 @@ describe Gitlab::Metrics::MethodCall do ...@@ -81,12 +63,10 @@ describe Gitlab::Metrics::MethodCall do
context 'when measurement is below threshold' do context 'when measurement is below threshold' do
before do before do
allow(method_call).to receive(:above_threshold?).and_return(false) allow(method_call).to receive(:above_threshold?).and_return(false)
Feature.get(:prometheus_metrics_method_instrumentation).enable
end end
it 'does not observe the performance' do it 'does not observe the performance' do
expect(described_class.call_duration_histogram) expect(described_class.gitlab_method_call_duration_seconds)
.not_to receive(:observe) .not_to receive(:observe)
method_call.measure { 'foo' } method_call.measure { 'foo' }
...@@ -96,7 +76,7 @@ describe Gitlab::Metrics::MethodCall do ...@@ -96,7 +76,7 @@ describe Gitlab::Metrics::MethodCall do
describe '#to_metric' do describe '#to_metric' do
it 'returns a Metric instance' do it 'returns a Metric instance' do
expect(method_call).to receive(:real_time).and_return(4.0001) expect(method_call).to receive(:real_time).and_return(4.0001).twice
expect(method_call).to receive(:cpu_time).and_return(3.0001) expect(method_call).to receive(:cpu_time).and_return(3.0001)
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