debian-mirror-gitlab/spec/lib/gitlab/metrics/sli_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

183 lines
6.3 KiB
Ruby
Raw Permalink Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
require 'fast_spec_helper'
RSpec.describe Gitlab::Metrics::Sli do
let(:prometheus) { double("prometheus") }
before do
stub_const("Gitlab::Metrics", prometheus)
end
describe 'Class methods' do
2022-07-16 23:28:13 +05:30
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)
2021-11-18 22:05:49 +05:30
end
2022-07-16 23:28:13 +05:30
it 'allows different SLIs to be defined on each subclass' do
apdex_counters = [
2022-07-23 23:45:48 +05:30
fake_total_counter('foo_apdex'),
fake_numerator_counter('foo_apdex', 'success')
2022-07-16 23:28:13 +05:30
]
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
error_rate_counters = [
2022-07-23 23:45:48 +05:30
fake_total_counter('foo'),
fake_numerator_counter('foo', 'error')
2022-07-16 23:28:13 +05:30
]
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
apdex = described_class::Apdex.initialize_sli(:foo, [{ hello: :world }])
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
expect(apdex_counters).to all(have_received(:get).with(hello: :world))
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
error_rate = described_class::ErrorRate.initialize_sli(:foo, [{ other: :labels }])
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
expect(error_rate_counters).to all(have_received(:get).with(other: :labels))
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
expect(described_class::Apdex[:foo]).to be(apdex)
expect(described_class::ErrorRate[:foo]).to be(error_rate)
2021-11-18 22:05:49 +05:30
end
end
2022-07-16 23:28:13 +05:30
subclasses = {
2022-07-23 23:45:48 +05:30
Gitlab::Metrics::Sli::Apdex => {
suffix: '_apdex',
numerator: :success
},
Gitlab::Metrics::Sli::ErrorRate => {
suffix: '',
numerator: :error
}
2022-07-16 23:28:13 +05:30
}
2021-11-18 22:05:49 +05:30
2022-07-23 23:45:48 +05:30
subclasses.each do |subclass, subclass_info|
2022-07-16 23:28:13 +05:30
describe subclass do
describe 'Class methods' do
before do
described_class.instance_variable_set(:@known_slis, nil)
end
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
describe '.[]' do
it 'returns and stores a new, uninitialized SLI' do
sli = described_class[:bar]
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
expect(described_class[:bar]).to be(sli)
expect(described_class[:bar]).not_to be_initialized
end
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
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 = [
2022-07-23 23:45:48 +05:30
fake_total_counter("bar#{subclass_info[:suffix]}"),
fake_numerator_counter("bar#{subclass_info[:suffix]}", subclass_info[:numerator])
2022-07-16 23:28:13 +05:30
]
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 = [
2022-07-23 23:45:48 +05:30
fake_total_counter("bar#{subclass_info[:suffix]}"),
fake_numerator_counter("bar#{subclass_info[:suffix]}", subclass_info[:numerator])
2022-07-16 23:28:13 +05:30
]
sli = described_class.initialize_sli(:bar, [{ hello: :world }])
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
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))
2021-11-18 22:05:49 +05:30
2022-07-16 23:28:13 +05:30
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
2022-07-23 23:45:48 +05:30
fake_total_counter("boom#{subclass_info[:suffix]}")
fake_numerator_counter("boom#{subclass_info[:suffix]}", subclass_info[:numerator])
2022-07-16 23:28:13 +05:30
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 = [
2022-07-23 23:45:48 +05:30
fake_total_counter("hey#{subclass_info[:suffix]}"),
fake_numerator_counter("hey#{subclass_info[:suffix]}", subclass_info[:numerator])
2022-07-16 23:28:13 +05:30
]
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) }
2022-07-23 23:45:48 +05:30
let!(:total_counter) { fake_total_counter("heyo#{subclass_info[:suffix]}") }
let!(:numerator_counter) { fake_numerator_counter("heyo#{subclass_info[:suffix]}", subclass_info[:numerator]) }
2022-07-16 23:28:13 +05:30
2022-07-23 23:45:48 +05:30
it "increments both counters for labels when #{subclass_info[:numerator]} is true" do
sli.increment(labels: { hello: "world" }, subclass_info[:numerator] => true)
2022-07-16 23:28:13 +05:30
expect(total_counter).to have_received(:increment).with({ hello: 'world' })
expect(numerator_counter).to have_received(:increment).with({ hello: 'world' })
end
2022-07-23 23:45:48 +05:30
it "only increments the total counters for labels when #{subclass_info[:numerator]} is false" do
sli.increment(labels: { hello: "world" }, subclass_info[:numerator] => false)
2022-07-16 23:28:13 +05:30
expect(total_counter).to have_received(:increment).with({ hello: 'world' })
expect(numerator_counter).not_to have_received(:increment).with({ hello: 'world' })
end
end
2021-11-18 22:05:49 +05:30
end
end
def fake_prometheus_counter(name)
fake_counter = double("prometheus counter: #{name}")
allow(fake_counter).to receive(:get)
allow(fake_counter).to receive(:increment)
allow(prometheus).to receive(:counter).with(name.to_sym, anything).and_return(fake_counter)
fake_counter
end
2022-08-13 15:12:31 +05:30
def fake_total_counter(name, separator = '_')
fake_prometheus_counter(['gitlab_sli', name, 'total'].join(separator))
2021-11-18 22:05:49 +05:30
end
2022-08-13 15:12:31 +05:30
def fake_numerator_counter(name, numerator_name, separator = '_')
fake_prometheus_counter(["gitlab_sli", name, "#{numerator_name}_total"].join(separator))
2021-11-18 22:05:49 +05:30
end
end