diff --git a/lib/gitlab/metrics/concern.rb b/lib/gitlab/metrics/concern.rb index 6a05fb9b815..11852f50319 100644 --- a/lib/gitlab/metrics/concern.rb +++ b/lib/gitlab/metrics/concern.rb @@ -33,19 +33,25 @@ module Gitlab options = MetricOptions.new(opts) options.evaluate(&block) + if disabled_by_feature(options) + synchronized_cache_fill(name) { NullMetric.new } + else + synchronized_cache_fill(name) { build_metric!(type, name, options) } + end + end + + def synchronized_cache_fill(key) MUTEX.synchronize do @_metrics_provider_cache ||= {} - @_metrics_provider_cache[name] ||= build_metric!(type, name, options) + @_metrics_provider_cache[key] ||= yield end + end - @_metrics_provider_cache[name] + def disabled_by_feature(options) + options.with_feature && !Feature.get(options.with_feature).enabled? end def build_metric!(type, name, options) - unless options.with_feature.nil? || Feature.get(options.with_feature).enabled? - return NullMetric.new - end - case type when :gauge Gitlab::Metrics.gauge(name, options.docstring, options.base_labels, options.multiprocess_mode) diff --git a/spec/lib/gitlab/metrics/concern_spec.rb b/spec/lib/gitlab/metrics/concern_spec.rb index fc5f19000ba..1f750397750 100644 --- a/spec/lib/gitlab/metrics/concern_spec.rb +++ b/spec/lib/gitlab/metrics/concern_spec.rb @@ -128,5 +128,5 @@ describe Gitlab::Metrics::Concern do include_examples 'metric', :counter, {} include_examples 'metric', :gauge, {}, :all - include_examples 'metric', :histogram, {}, [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10] + include_examples 'metric', :histogram, {}, [0.005, 0.01, 0.1, 1, 10] end