Commit 5904b033 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Implemente measurement enabled cache using AtomicReference

parent 6af84964
......@@ -2,6 +2,7 @@ module Gitlab
module Metrics
# Class for tracking timing information about method calls
class MethodCall
MEASUREMENT_ENABLED_CACHE = Concurrent::AtomicReference.new({ enabled: false, expires_at: Time.now })
MUTEX = Mutex.new
BASE_LABELS = { module: nil, method: nil }.freeze
attr_reader :real_time, :cpu_time, :call_count, :labels
......@@ -18,25 +19,19 @@ module Gitlab
end
end
def self.call_measurement_enabled?
return @call_measurement_enabled unless call_measurement_enabled_cache_expired?
MUTEX.synchronize do
return @call_measurement_enabled unless call_measurement_enabled_cache_expired?
@call_measurement_enabled = Feature.get(:prometheus_metrics_method_instrumentation).enabled?
@call_measurement_enabled_cache_expires_at = Time.now + 5.minutes
@call_measurement_enabled
end
def call_measurement_enabled?
res = MEASUREMENT_ENABLED_CACHE.update do |cache|
if cache[:expires_at] < Time.now
{
enabled: Feature.get(:prometheus_metrics_method_instrumentation).enabled?,
expires_at: Time.now + 5.minutes
}
else
cache
end
def self.call_measurement_enabled_cache_expired?
@call_measurement_enabled.nil? || @call_measurement_enabled_cache_expires_at.nil? || @call_measurement_enabled_cache_expires_at < Time.now
end
def self.call_measurement_enabled_cache_expire
@call_measurement_enabled = nil
@call_measurement_enabled_cache_expires_at = nil
res[:enabled]
end
# name - The full name of the method (including namespace) such as
......@@ -66,7 +61,7 @@ module Gitlab
@cpu_time += cpu_time
@call_count += 1
if self.class.call_measurement_enabled? && above_threshold?
if call_measurement_enabled? && above_threshold?
self.class.call_duration_histogram.observe(@transaction.labels.merge(labels), real_time / 1000.0)
end
......
......@@ -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.call_measurement_enabled_cache_expire
described_class::MEASUREMENT_ENABLED_CACHE.set({enabled: false, expires_at: Time.now - 1.second})
Feature.get(:prometheus_metrics_method_instrumentation).enable
end
......@@ -62,7 +62,7 @@ describe Gitlab::Metrics::MethodCall do
context 'prometheus instrumentation is disabled' do
before do
described_class.call_measurement_enabled_cache_expire
described_class::MEASUREMENT_ENABLED_CACHE.set({enabled: false, expires_at: Time.now})
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