diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
commit | 36a59d088eca61b834191dacea009677a96c052f (patch) | |
tree | e4f33972dab5d8ef79e3944a9f403035fceea43f /spec/lib/gitlab/metrics/sli_spec.rb | |
parent | a1761f15ec2cae7c7f7bbda39a75494add0dfd6f (diff) |
Add latest changes from gitlab-org/gitlab@15-0-stable-eev15.0.0-rc42
Diffstat (limited to 'spec/lib/gitlab/metrics/sli_spec.rb')
-rw-r--r-- | spec/lib/gitlab/metrics/sli_spec.rb | 183 |
1 files changed, 131 insertions, 52 deletions
diff --git a/spec/lib/gitlab/metrics/sli_spec.rb b/spec/lib/gitlab/metrics/sli_spec.rb index 8ba4bf29568..9b776d6738d 100644 --- a/spec/lib/gitlab/metrics/sli_spec.rb +++ b/spec/lib/gitlab/metrics/sli_spec.rb @@ -10,72 +10,151 @@ RSpec.describe Gitlab::Metrics::Sli do end describe 'Class methods' do - before do - described_class.instance_variable_set(:@known_slis, nil) + it 'does not allow them to be called on the parent module' do + expect(described_class).not_to respond_to(:[]) + expect(described_class).not_to respond_to(:initialize_sli) end - describe '.[]' do - it 'warns about an uninitialized SLI but returns and stores a new one' do - sli = described_class[:bar] + it 'allows different SLIs to be defined on each subclass' do + apdex_counters = [ + fake_total_counter('foo', 'apdex'), + fake_numerator_counter('foo', 'apdex', 'success') + ] - expect(described_class[:bar]).to be(sli) - end + error_rate_counters = [ + fake_total_counter('foo', 'error_rate'), + fake_numerator_counter('foo', 'error_rate', 'error') + ] - it 'returns the same object for multiple accesses' do - sli = described_class.initialize_sli(:huzzah, []) + apdex = described_class::Apdex.initialize_sli(:foo, [{ hello: :world }]) - 2.times do - expect(described_class[:huzzah]).to be(sli) - end - end - end + expect(apdex_counters).to all(have_received(:get).with(hello: :world)) - describe '.initialized?' do - before do - fake_total_counter(:boom) - fake_success_counter(:boom) - end + error_rate = described_class::ErrorRate.initialize_sli(:foo, [{ other: :labels }]) - it 'is true when an SLI was initialized with labels' do - expect { described_class.initialize_sli(:boom, [{ hello: :world }]) } - .to change { described_class.initialized?(:boom) }.from(false).to(true) - end + expect(error_rate_counters).to all(have_received(:get).with(other: :labels)) - it 'is false when an SLI was not initialized with labels' do - expect { described_class.initialize_sli(:boom, []) } - .not_to change { described_class.initialized?(:boom) }.from(false) - end + expect(described_class::Apdex[:foo]).to be(apdex) + expect(described_class::ErrorRate[:foo]).to be(error_rate) end end - describe '#initialize_counters' do - it 'initializes counters for the passed label combinations' do - counters = [fake_total_counter(:hey), fake_success_counter(:hey)] + subclasses = { + Gitlab::Metrics::Sli::Apdex => :success, + Gitlab::Metrics::Sli::ErrorRate => :error + } - described_class.new(:hey).initialize_counters([{ foo: 'bar' }, { foo: 'baz' }]) + subclasses.each do |subclass, numerator_type| + subclass_type = subclass.to_s.demodulize.underscore - expect(counters).to all(have_received(:get).with({ foo: 'bar' })) - expect(counters).to all(have_received(:get).with({ foo: 'baz' })) - end - end + describe subclass do + describe 'Class methods' do + before do + described_class.instance_variable_set(:@known_slis, nil) + end - describe "#increment" do - let!(:sli) { described_class.new(:heyo) } - let!(:total_counter) { fake_total_counter(:heyo) } - let!(:success_counter) { fake_success_counter(:heyo) } + describe '.[]' do + it 'returns and stores a new, uninitialized SLI' do + sli = described_class[:bar] - it 'increments both counters for labels successes' do - sli.increment(labels: { hello: "world" }, success: true) + expect(described_class[:bar]).to be(sli) + expect(described_class[:bar]).not_to be_initialized + end - expect(total_counter).to have_received(:increment).with({ hello: 'world' }) - expect(success_counter).to have_received(:increment).with({ hello: 'world' }) - end + it 'returns the same object for multiple accesses' do + sli = described_class.initialize_sli(:huzzah, []) + + 2.times do + expect(described_class[:huzzah]).to be(sli) + end + end + end + + describe '.initialize_sli' do + it 'returns and stores a new initialized SLI' do + counters = [ + fake_total_counter(:bar, subclass_type), + fake_numerator_counter(:bar, subclass_type, numerator_type) + ] + + sli = described_class.initialize_sli(:bar, [{ hello: :world }]) + + expect(sli).to be_initialized + expect(counters).to all(have_received(:get).with(hello: :world)) + expect(counters).to all(have_received(:get).with(hello: :world)) + end + + it 'does not change labels for an already-initialized SLI' do + counters = [ + fake_total_counter(:bar, subclass_type), + fake_numerator_counter(:bar, subclass_type, numerator_type) + ] + + sli = described_class.initialize_sli(:bar, [{ hello: :world }]) - it 'only increments the total counters for labels when not successful' do - sli.increment(labels: { hello: "world" }, success: false) + expect(sli).to be_initialized + expect(counters).to all(have_received(:get).with(hello: :world)) + expect(counters).to all(have_received(:get).with(hello: :world)) - expect(total_counter).to have_received(:increment).with({ hello: 'world' }) - expect(success_counter).not_to have_received(:increment).with({ hello: 'world' }) + counters.each do |counter| + expect(counter).not_to receive(:get) + end + + expect(described_class.initialize_sli(:bar, [{ other: :labels }])).to eq(sli) + end + end + + describe '.initialized?' do + before do + fake_total_counter(:boom, subclass_type) + fake_numerator_counter(:boom, subclass_type, numerator_type) + end + + it 'is true when an SLI was initialized with labels' do + expect { described_class.initialize_sli(:boom, [{ hello: :world }]) } + .to change { described_class.initialized?(:boom) }.from(false).to(true) + end + + it 'is false when an SLI was not initialized with labels' do + expect { described_class.initialize_sli(:boom, []) } + .not_to change { described_class.initialized?(:boom) }.from(false) + end + end + end + + describe '#initialize_counters' do + it 'initializes counters for the passed label combinations' do + counters = [ + fake_total_counter(:hey, subclass_type), + fake_numerator_counter(:hey, subclass_type, numerator_type) + ] + + described_class.new(:hey).initialize_counters([{ foo: 'bar' }, { foo: 'baz' }]) + + expect(counters).to all(have_received(:get).with({ foo: 'bar' })) + expect(counters).to all(have_received(:get).with({ foo: 'baz' })) + end + end + + describe "#increment" do + let!(:sli) { described_class.new(:heyo) } + let!(:total_counter) { fake_total_counter(:heyo, subclass_type) } + let!(:numerator_counter) { fake_numerator_counter(:heyo, subclass_type, numerator_type) } + + it "increments both counters for labels when #{numerator_type} is true" do + sli.increment(labels: { hello: "world" }, numerator_type => true) + + expect(total_counter).to have_received(:increment).with({ hello: 'world' }) + expect(numerator_counter).to have_received(:increment).with({ hello: 'world' }) + end + + it "only increments the total counters for labels when #{numerator_type} is false" do + sli.increment(labels: { hello: "world" }, numerator_type => false) + + expect(total_counter).to have_received(:increment).with({ hello: 'world' }) + expect(numerator_counter).not_to have_received(:increment).with({ hello: 'world' }) + end + end end end @@ -89,11 +168,11 @@ RSpec.describe Gitlab::Metrics::Sli do fake_counter end - def fake_total_counter(name) - fake_prometheus_counter("gitlab_sli:#{name}:total") + def fake_total_counter(name, type) + fake_prometheus_counter("gitlab_sli:#{name}_#{type}:total") end - def fake_success_counter(name) - fake_prometheus_counter("gitlab_sli:#{name}:success_total") + def fake_numerator_counter(name, type, numerator_name) + fake_prometheus_counter("gitlab_sli:#{name}_#{type}:#{numerator_name}_total") end end |