summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/sli_spec.rb
blob: 8ba4bf295688d02b2a6686894585a54ae3c022bc (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
# 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
    before do
      described_class.instance_variable_set(:@known_slis, nil)
    end

    describe '.[]' do
      it 'warns about an uninitialized SLI but returns and stores a new one' do
        sli = described_class[:bar]

        expect(described_class[:bar]).to be(sli)
      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 '.initialized?' do
      before do
        fake_total_counter(:boom)
        fake_success_counter(:boom)
      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), fake_success_counter(:hey)]

      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) }
    let!(:success_counter) { fake_success_counter(:heyo) }

    it 'increments both counters for labels successes' do
      sli.increment(labels: { hello: "world" }, success: true)

      expect(total_counter).to have_received(:increment).with({ hello: 'world' })
      expect(success_counter).to have_received(:increment).with({ hello: 'world' })
    end

    it 'only increments the total counters for labels when not successful' do
      sli.increment(labels: { hello: "world" }, success: false)

      expect(total_counter).to have_received(:increment).with({ hello: 'world' })
      expect(success_counter).not_to have_received(:increment).with({ hello: 'world' })
    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_success_counter(name)
    fake_prometheus_counter("gitlab_sli:#{name}:success_total")
  end
end