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

Implemente measurement enabled cache using AtomicReference

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