Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sli_spec.rb « metrics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 102ea442b3a767f6d84af2e4fa895820e58e0c80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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
    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

    it 'allows different SLIs to be defined on each subclass' do
      apdex_counters = [
        fake_total_counter('foo_apdex'),
        fake_numerator_counter('foo_apdex', 'success')
      ]

      error_rate_counters = [
        fake_total_counter('foo'),
        fake_numerator_counter('foo', 'error')
      ]

      apdex = described_class::Apdex.initialize_sli(:foo, [{ hello: :world }])

      expect(apdex_counters).to all(have_received(:get).with(hello: :world))

      error_rate = described_class::ErrorRate.initialize_sli(:foo, [{ other: :labels }])

      expect(error_rate_counters).to all(have_received(:get).with(other: :labels))

      expect(described_class::Apdex[:foo]).to be(apdex)
      expect(described_class::ErrorRate[:foo]).to be(error_rate)
    end
  end

  subclasses = {
    Gitlab::Metrics::Sli::Apdex => {
      suffix: '_apdex',
      numerator: :success
    },
    Gitlab::Metrics::Sli::ErrorRate => {
      suffix: '',
      numerator: :error
    }
  }

  subclasses.each do |subclass, subclass_info|
    describe subclass do
      describe 'Class methods' do
        before do
          described_class.instance_variable_set(:@known_slis, nil)
        end

        describe '.[]' do
          it 'returns and stores a new, uninitialized SLI' do
            sli = described_class[:bar]

            expect(described_class[:bar]).to be(sli)
            expect(described_class[:bar]).not_to be_initialized
          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_info[:suffix]}"),
              fake_numerator_counter("bar#{subclass_info[:suffix]}", subclass_info[:numerator])
            ]

            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_info[:suffix]}"),
              fake_numerator_counter("bar#{subclass_info[:suffix]}", subclass_info[:numerator])
            ]

            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))

            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_info[:suffix]}")
            fake_numerator_counter("boom#{subclass_info[:suffix]}", subclass_info[:numerator])
          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_info[:suffix]}"),
            fake_numerator_counter("hey#{subclass_info[:suffix]}", subclass_info[:numerator])
          ]

          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_info[:suffix]}") }
        let!(:numerator_counter) { fake_numerator_counter("heyo#{subclass_info[:suffix]}", subclass_info[:numerator]) }

        it "increments both counters for labels when #{subclass_info[:numerator]} is true" do
          sli.increment(labels: { hello: "world" }, subclass_info[:numerator] => 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 #{subclass_info[:numerator]} is false" do
          sli.increment(labels: { hello: "world" }, subclass_info[:numerator] => 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

  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

  def fake_total_counter(name)
    fake_prometheus_counter("gitlab_sli:#{name}:total")
  end

  def fake_numerator_counter(name, numerator_name)
    fake_prometheus_counter("gitlab_sli:#{name}:#{numerator_name}_total")
  end
end