BigW Consortium Gitlab

Commit 6a9091fb by Robert Speicher Committed by Oswaldo Ferreira

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 83998c0a) 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
parent 9d8c933f
# 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
......
......@@ -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
......
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