Commit ef9d9dde authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add tests for metrics behavior

parent 57902dbe
...@@ -54,23 +54,25 @@ module Gitlab ...@@ -54,23 +54,25 @@ module Gitlab
end end
def self.counter(name, docstring, base_labels = {}) 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 end
def self.summary(name, docstring, base_labels = {}) 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 end
def self.gauge(name, docstring, base_labels = {}) 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 end
def self.histogram(name, docstring, base_labels = {}, buckets = Histogram::DEFAULT_BUCKETS) def self.histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
dummy_metric || registry.get(name) || registry.histogram(name, docstring, base_labels, buckets) provide_metric(name) || registry.histogram(name, docstring, base_labels, buckets)
end end
def self.dummy_metric def self.provide_metric(name)
unless prometheus_metrics_enabled? if prometheus_metrics_enabled?
registry.get(name)
else
DummyMetric.new DummyMetric.new
end end
end end
......
...@@ -13,6 +13,18 @@ describe Gitlab::Metrics do ...@@ -13,6 +13,18 @@ describe Gitlab::Metrics do
end end
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 describe '.submit_metrics' do
it 'prepares and writes the metrics to InfluxDB' do it 'prepares and writes the metrics to InfluxDB' do
connection = double(:connection) connection = double(:connection)
...@@ -177,4 +189,117 @@ describe Gitlab::Metrics do ...@@ -177,4 +189,117 @@ describe Gitlab::Metrics do
end end
end 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 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