NullMetric as a singleton

This commit is contained in:
Pawel Chojnacki 2018-01-19 14:54:06 +01:00
parent 3e898be8aa
commit f5383578d9
4 changed files with 7 additions and 3 deletions

View file

@ -34,7 +34,7 @@ module Gitlab
options.evaluate(&block)
if disabled_by_feature(options)
synchronized_cache_fill(name) { NullMetric.new }
synchronized_cache_fill(name) { NullMetric.instance }
else
synchronized_cache_fill(name) { build_metric!(type, name, options) }
end

View file

@ -2,6 +2,8 @@ module Gitlab
module Metrics
# Mocks ::Prometheus::Client::Metric and all derived metrics
class NullMetric
include Singleton
def method_missing(name, *args, &block)
nil
end

View file

@ -64,7 +64,7 @@ module Gitlab
if prometheus_metrics_enabled?
registry.get(name)
else
NullMetric.new
NullMetric.instance
end
end

View file

@ -53,7 +53,9 @@ describe Gitlab::Metrics::Concern do
describe "#fetch_#{metric_type}" do
let(:fetch_method) { "fetch_#{metric_type}".to_sym }
let(:null_metric) { Gitlab::Metrics::NullMetric.new }
let(:_metric_type) { metric_type }
let(:null_metric) { Gitlab::Metrics::NullMetric.instance }
context "when #{metric_type} is not cached" do
it 'initializes counter metric' do