diff options
author | Robert Speicher <robert@gitlab.com> | 2017-12-13 18:46:40 +0000 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2017-12-19 14:04:41 -0200 |
commit | 6a9091fb68d74fc003c385fd3f3933ffeddffd26 (patch) | |
tree | 0e395aae4c8b10079ebb1737c63c956118a1267b | |
parent | 9d8c933f18441457dbc3507a80a42e744c491e7b (diff) | |
download | gitlab-ce-6a9091fb68d74fc003c385fd3f3933ffeddffd26.tar.gz |
Merge branch 'pawel/cache_feature_check_for_5_minutes_for_method_call' into 'master'
Cache feature check for 1 minute for MethodCall instrumentation toggle
See merge request gitlab-org/gitlab-ce!15800
(cherry picked from commit 83998c0a33c70c786f48eef5ce723eb6dd76761f)
53dc9e83 Cache feature check for 5 minutes for MethodCall instrumentation toggle
b503e6ff Implement simple in memory cache that expires after 5 minutes
6af84964 Set cache expire only once the cache is filled,
5904b033 Implemente measurement enabled cache using AtomicReference
408208bc Use AtomicFixNum to implement CAS isolated cache update.
ca176a9b move call_measurement_enabled? method to the bottom of the file
fd0a5168 use class variables instead of CONSTANTs
da19ce62 Expire feature flag cache after 1minute
51668d3e Use class variable and add rubocop exception
db9e5bf7 fix test case description
-rw-r--r-- | lib/gitlab/metrics/method_call.rb | 17 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/method_call_spec.rb | 32 |
2 files changed, 48 insertions, 1 deletions
diff --git a/lib/gitlab/metrics/method_call.rb b/lib/gitlab/metrics/method_call.rb index 65d55576ac2..9112164f22e 100644 --- a/lib/gitlab/metrics/method_call.rb +++ b/lib/gitlab/metrics/method_call.rb @@ -1,7 +1,11 @@ +# rubocop:disable Style/ClassVars + module Gitlab module Metrics # Class for tracking timing information about method calls class MethodCall + @@measurement_enabled_cache = Concurrent::AtomicBoolean.new(false) + @@measurement_enabled_cache_expires_at = Concurrent::AtomicFixnum.new(Time.now.to_i) MUTEX = Mutex.new BASE_LABELS = { module: nil, method: nil }.freeze attr_reader :real_time, :cpu_time, :call_count, :labels @@ -18,6 +22,10 @@ module Gitlab end end + def self.measurement_enabled_cache_expires_at + @@measurement_enabled_cache_expires_at + end + # name - The full name of the method (including namespace) such as # `User#sign_in`. # @@ -72,7 +80,14 @@ module Gitlab end def call_measurement_enabled? - Feature.get(:prometheus_metrics_method_instrumentation).enabled? + expires_at = @@measurement_enabled_cache_expires_at.value + if expires_at < Time.now.to_i + if @@measurement_enabled_cache_expires_at.compare_and_set(expires_at, 1.minute.from_now.to_i) + @@measurement_enabled_cache.value = Feature.get(:prometheus_metrics_method_instrumentation).enabled? + end + end + + @@measurement_enabled_cache.value end end end diff --git a/spec/lib/gitlab/metrics/method_call_spec.rb b/spec/lib/gitlab/metrics/method_call_spec.rb index 5341addf911..78767d06462 100644 --- a/spec/lib/gitlab/metrics/method_call_spec.rb +++ b/spec/lib/gitlab/metrics/method_call_spec.rb @@ -20,9 +20,39 @@ describe Gitlab::Metrics::MethodCall do context 'prometheus instrumentation is enabled' 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 end + around do |example| + Timecop.freeze do + example.run + end + end + + it 'caches subsequent invocations of feature check' do + 10.times do + 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 + it 'observes the performance of the supplied block' do expect(described_class.call_duration_histogram) .to receive(:observe) @@ -34,6 +64,8 @@ describe Gitlab::Metrics::MethodCall do context 'prometheus instrumentation is disabled' do before do + described_class.measurement_enabled_cache_expires_at.value = Time.now.to_i - 1 + Feature.get(:prometheus_metrics_method_instrumentation).disable end |