use class variables instead of CONSTANTs

This commit is contained in:
Pawel Chojnacki 2017-12-12 00:23:56 +01:00
parent ca176a9bfe
commit fd0a516854
2 changed files with 12 additions and 8 deletions

View File

@ -2,8 +2,8 @@ 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)
@@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
@ -20,6 +20,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`.
#
@ -74,14 +78,14 @@ module Gitlab
end
def call_measurement_enabled?
expires_at = MEASUREMENT_ENABLED_CACHE_EXPIRES_AT.value
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, (Time.now + 30.seconds).to_i)
MEASUREMENT_ENABLED_CACHE.value = Feature.get(:prometheus_metrics_method_instrumentation).enabled?
if @@measurement_enabled_cache_expires_at.compare_and_set(expires_at, (Time.now + 30.seconds).to_i)
@@measurement_enabled_cache.value = Feature.get(:prometheus_metrics_method_instrumentation).enabled?
end
end
MEASUREMENT_ENABLED_CACHE.value
@@measurement_enabled_cache.value
end
end
end

View File

@ -21,7 +21,7 @@ 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
described_class.measurement_enabled_cache_expires_at.value = Time.now.to_i - 1
Feature.get(:prometheus_metrics_method_instrumentation).enable
end
@ -66,7 +66,7 @@ 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
described_class.measurement_enabled_cache_expires_at.value = Time.now.to_i - 1
Feature.get(:prometheus_metrics_method_instrumentation).disable
end