gitlab-org--gitlab-foss/spec/lib/gitlab/metrics/methods_spec.rb

140 lines
4.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-01-17 21:16:25 +00:00
require 'spec_helper'
RSpec.describe Gitlab::Metrics::Methods do
2018-01-29 11:33:08 +00:00
subject { Class.new { include Gitlab::Metrics::Methods } }
2018-01-17 21:16:25 +00:00
shared_context 'metric' do |metric_type, *args|
let(:docstring) { 'description' }
2018-01-18 12:37:53 +00:00
let(:metric_name) { :sample_metric }
2018-01-17 21:16:25 +00:00
describe "#define_metrics" do
define_method(:call_define_metric_method) do |**args|
subject.__send__(:define_metric, metric_type, metric_name, **args)
2018-01-19 13:54:40 +00:00
end
2018-01-17 21:16:25 +00:00
context 'metrics access method not defined' do
it "defines metrics accessing method" do
2018-01-18 12:37:53 +00:00
expect(subject).not_to respond_to(metric_name)
2018-01-17 21:16:25 +00:00
call_define_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
2018-01-18 12:37:53 +00:00
expect(subject).to respond_to(metric_name)
2018-01-17 21:16:25 +00:00
end
end
context 'metrics access method defined' do
before do
call_define_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
end
it 'raises error when trying to redefine method' do
expect { call_define_metric_method(docstring: docstring) }.to raise_error(ArgumentError)
2018-01-17 21:16:25 +00:00
end
context 'metric is not cached' do
it 'calls fetch_metric' do
2018-01-19 13:54:40 +00:00
expect(subject).to receive(:init_metric).with(metric_type, metric_name, docstring: docstring)
2018-01-17 21:16:25 +00:00
2018-01-22 19:53:14 +00:00
subject.public_send(metric_name)
2018-01-17 21:16:25 +00:00
end
end
context 'metric is cached' do
before do
2018-01-22 19:53:14 +00:00
subject.public_send(metric_name)
2018-01-17 21:16:25 +00:00
end
it 'returns cached metric' do
2018-01-19 13:54:40 +00:00
expect(subject).not_to receive(:init_metric)
2018-01-17 21:16:25 +00:00
2018-01-22 19:53:14 +00:00
subject.public_send(metric_name)
2018-01-17 21:16:25 +00:00
end
end
end
end
describe "#fetch_metric" do
2018-01-19 13:54:06 +00:00
let(:null_metric) { Gitlab::Metrics::NullMetric.instance }
2018-01-17 21:16:25 +00:00
define_method(:call_fetch_metric_method) do |**args|
subject.__send__(:fetch_metric, metric_type, metric_name, **args)
2018-01-19 13:54:40 +00:00
end
2018-01-18 12:37:53 +00:00
context "when #{metric_type} is not cached" do
2018-01-17 21:16:25 +00:00
it 'initializes counter metric' do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
call_fetch_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
2018-01-18 12:37:53 +00:00
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
2018-01-17 21:16:25 +00:00
end
end
2018-01-18 12:37:53 +00:00
context "when #{metric_type} is cached" do
2018-01-17 21:16:25 +00:00
before do
call_fetch_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
end
it 'uses class metric cache' do
expect(Gitlab::Metrics).not_to receive(metric_type)
call_fetch_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
end
context 'when metric is reloaded' do
before do
2018-01-18 12:37:53 +00:00
subject.reload_metric!(metric_name)
2018-01-17 21:16:25 +00:00
end
it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
call_fetch_metric_method(docstring: docstring)
2018-01-17 21:16:25 +00:00
2018-01-18 12:37:53 +00:00
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end
end
end
context 'when metric is configured with feature' do
let(:feature_name) { :some_metric_feature }
let(:metric) { call_fetch_metric_method(docstring: docstring, with_feature: feature_name) }
2018-01-18 12:37:53 +00:00
context 'when feature is enabled' do
before do
stub_feature_flags(feature_name => true)
2018-01-18 12:37:53 +00:00
end
it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
metric
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end
end
context 'when feature is disabled' do
before do
stub_feature_flags(feature_name => false)
2018-01-18 12:37:53 +00:00
end
it "returns NullMetric" do
2018-01-19 13:54:40 +00:00
allow(Gitlab::Metrics).to receive(metric_type)
2018-01-18 12:37:53 +00:00
expect(metric).to be_instance_of(Gitlab::Metrics::NullMetric)
expect(Gitlab::Metrics).not_to have_received(metric_type)
2018-01-17 21:16:25 +00:00
end
end
end
end
end
include_examples 'metric', :counter, {}
include_examples 'metric', :gauge, {}, :all
include_examples 'metric', :histogram, {}, ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
2018-01-17 21:16:25 +00:00
end