diff --git a/lib/gitlab/metrics.rb b/lib/gitlab/metrics.rb index 9783d4e3582..a41cbd214a1 100644 --- a/lib/gitlab/metrics.rb +++ b/lib/gitlab/metrics.rb @@ -54,23 +54,25 @@ module Gitlab end def self.counter(name, docstring, base_labels = {}) - dummy_metric || registry.get(name) || registry.counter(name, docstring, base_labels) + provide_metric(name) || registry.counter(name, docstring, base_labels) end def self.summary(name, docstring, base_labels = {}) - dummy_metric || registry.get(name) || registry.summary(name, docstring, base_labels) + provide_metric(name) || registry.summary(name, docstring, base_labels) end def self.gauge(name, docstring, base_labels = {}) - dummy_metric || registry.get(name) || registry.gauge(name, docstring, base_labels) + provide_metric(name) || registry.gauge(name, docstring, base_labels) end - def self.histogram(name, docstring, base_labels = {}, buckets = Histogram::DEFAULT_BUCKETS) - dummy_metric || registry.get(name) || registry.histogram(name, docstring, base_labels, buckets) + def self.histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS) + provide_metric(name) || registry.histogram(name, docstring, base_labels, buckets) end - def self.dummy_metric - unless prometheus_metrics_enabled? + def self.provide_metric(name) + if prometheus_metrics_enabled? + registry.get(name) + else DummyMetric.new end end diff --git a/spec/lib/gitlab/metrics_spec.rb b/spec/lib/gitlab/metrics_spec.rb index 208a8d028cd..65bd06cda08 100644 --- a/spec/lib/gitlab/metrics_spec.rb +++ b/spec/lib/gitlab/metrics_spec.rb @@ -13,6 +13,18 @@ describe Gitlab::Metrics do end end + describe '.prometheus_metrics_enabled?' do + it 'returns a boolean' do + expect([true, false].include?(described_class.prometheus_metrics_enabled?)).to eq(true) + end + end + + describe '.influx_metrics_enabled?' do + it 'returns a boolean' do + expect([true, false].include?(described_class.influx_metrics_enabled?)).to eq(true) + end + end + describe '.submit_metrics' do it 'prepares and writes the metrics to InfluxDB' do connection = double(:connection) @@ -177,4 +189,117 @@ describe Gitlab::Metrics do end end end + + shared_examples 'prometheus metrics API' do + describe '#counter' do + subject { described_class.counter(:couter, 'doc') } + + describe '#increment' do + it { expect { subject.increment }.not_to raise_exception } + it { expect { subject.increment({}) }.not_to raise_exception } + it { expect { subject.increment({}, 1) }.not_to raise_exception } + end + end + + describe '#summary' do + subject { described_class.summary(:summary, 'doc') } + + describe '#observe' do + it { expect { subject.observe({}, 2) }.not_to raise_exception } + end + end + + describe '#gauge' do + subject { described_class.gauge(:gauge, 'doc') } + + describe '#observe' do + it { expect { subject.set({}, 1) }.not_to raise_exception } + end + end + + describe '#histogram' do + subject { described_class.histogram(:histogram, 'doc') } + + describe '#observe' do + it { expect { subject.observe({}, 2) }.not_to raise_exception } + end + end + end + + context 'prometheus metrics disabled' do + before do + allow(described_class).to receive(:prometheus_metrics_enabled?).and_return(false) + end + + it_behaves_like 'prometheus metrics API' + + describe '#dummy_metric' do + subject { described_class.provide_metric(:test) } + + it { is_expected.to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#counter' do + subject { described_class.counter(:counter, 'doc') } + + it { is_expected.to be_a(Gitlab::Metrics::DummyMetric) } + + end + + describe '#summary' do + subject { described_class.summary(:summary, 'doc') } + + it { is_expected.to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#gauge' do + subject { described_class.gauge(:gauge, 'doc') } + + it { is_expected.to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#histogram' do + subject { described_class.histogram(:histogram, 'doc') } + + it { is_expected.to be_a(Gitlab::Metrics::DummyMetric) } + end + end + + context 'prometheus metrics enabled' do + before do + allow(described_class).to receive(:prometheus_metrics_enabled?).and_return(true) + end + + it_behaves_like 'prometheus metrics API' + + describe '#dummy_metric' do + subject { described_class.provide_metric(:test) } + + it { is_expected.to be_nil } + end + + describe '#counter' do + subject { described_class.counter(:name, 'doc') } + + it { is_expected.not_to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#summary' do + subject { described_class.summary(:name, 'doc') } + + it { is_expected.not_to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#gauge' do + subject { described_class.gauge(:name, 'doc') } + + it { is_expected.not_to be_a(Gitlab::Metrics::DummyMetric) } + end + + describe '#histogram' do + subject { described_class.histogram(:name, 'doc') } + + it { is_expected.not_to be_a(Gitlab::Metrics::DummyMetric) } + end + end end