BigW Consortium Gitlab

metrics_spec.rb 4.67 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Metrics do
4 5 6
  describe '.settings' do
    it 'returns a Hash' do
      expect(described_class.settings).to be_an_instance_of(Hash)
7 8 9 10 11 12 13 14 15
    end
  end

  describe '.enabled?' do
    it 'returns a boolean' do
      expect([true, false].include?(described_class.enabled?)).to eq(true)
    end
  end

16
  describe '.submit_metrics' do
17 18 19 20 21 22
    it 'prepares and writes the metrics to InfluxDB' do
      connection = double(:connection)
      pool       = double(:pool)

      expect(pool).to receive(:with).and_yield(connection)
      expect(connection).to receive(:write_points).with(an_instance_of(Array))
23
      expect(described_class).to receive(:pool).and_return(pool)
24 25 26 27 28

      described_class.submit_metrics([{ 'series' => 'kittens', 'tags' => {} }])
    end
  end

29
  describe '.prepare_metrics' do
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    it 'returns a Hash with the keys as Symbols' do
      metrics = described_class.
        prepare_metrics([{ 'values' => {}, 'tags' => {} }])

      expect(metrics).to eq([{ values: {}, tags: {} }])
    end

    it 'escapes tag values' do
      metrics = described_class.prepare_metrics([
        { 'values' => {}, 'tags' => { 'foo' => 'bar=' } }
      ])

      expect(metrics).to eq([{ values: {}, tags: { 'foo' => 'bar\\=' } }])
    end

    it 'drops empty tags' do
      metrics = described_class.prepare_metrics([
        { 'values' => {}, 'tags' => { 'cats' => '', 'dogs' => nil } }
      ])

      expect(metrics).to eq([{ values: {}, tags: {} }])
    end
  end

54
  describe '.escape_value' do
55 56 57 58 59 60 61 62
    it 'escapes an equals sign' do
      expect(described_class.escape_value('foo=')).to eq('foo\\=')
    end

    it 'casts values to Strings' do
      expect(described_class.escape_value(10)).to eq('10')
    end
  end
63 64 65 66

  describe '.measure' do
    context 'without a transaction' do
      it 'returns the return value of the block' do
67
        val = described_class.measure(:foo) { 10 }
68 69 70 71 72 73 74 75 76

        expect(val).to eq(10)
      end
    end

    context 'with a transaction' do
      let(:transaction) { Gitlab::Metrics::Transaction.new }

      before do
77
        allow(described_class).to receive(:current_transaction).
78 79 80 81
          and_return(transaction)
      end

      it 'adds a metric to the current transaction' do
82 83
        expect(transaction).to receive(:increment).
          with('foo_real_time', a_kind_of(Numeric))
84

85 86
        expect(transaction).to receive(:increment).
          with('foo_cpu_time', a_kind_of(Numeric))
87

88 89 90
        expect(transaction).to receive(:increment).
          with('foo_call_count', 1)

91
        described_class.measure(:foo) { 10 }
92 93 94
      end

      it 'returns the return value of the block' do
95
        val = described_class.measure(:foo) { 10 }
96 97 98 99 100

        expect(val).to eq(10)
      end
    end
  end
101 102 103 104 105 106 107

  describe '.tag_transaction' do
    context 'without a transaction' do
      it 'does nothing' do
        expect_any_instance_of(Gitlab::Metrics::Transaction).
          not_to receive(:add_tag)

108
        described_class.tag_transaction(:foo, 'bar')
109 110 111 112 113 114 115
      end
    end

    context 'with a transaction' do
      let(:transaction) { Gitlab::Metrics::Transaction.new }

      it 'adds the tag to the transaction' do
116
        expect(described_class).to receive(:current_transaction).
117 118 119 120 121
          and_return(transaction)

        expect(transaction).to receive(:add_tag).
          with(:foo, 'bar')

122
        described_class.tag_transaction(:foo, 'bar')
123 124 125
      end
    end
  end
126 127 128 129 130 131 132

  describe '.action=' do
    context 'without a transaction' do
      it 'does nothing' do
        expect_any_instance_of(Gitlab::Metrics::Transaction).
          not_to receive(:action=)

133
        described_class.action = 'foo'
134 135 136 137 138 139 140
      end
    end

    context 'with a transaction' do
      it 'sets the action of a transaction' do
        trans = Gitlab::Metrics::Transaction.new

141
        expect(described_class).to receive(:current_transaction).
142 143 144 145
          and_return(trans)

        expect(trans).to receive(:action=).with('foo')

146
        described_class.action = 'foo'
147 148 149
      end
    end
  end
150 151 152 153 154 155

  describe '#series_prefix' do
    it 'returns a String' do
      expect(described_class.series_prefix).to be_an_instance_of(String)
    end
  end
156 157 158 159 160 161 162

  describe '.add_event' do
    context 'without a transaction' do
      it 'does nothing' do
        expect_any_instance_of(Gitlab::Metrics::Transaction).
          not_to receive(:add_event)

163
        described_class.add_event(:meow)
164 165 166 167 168 169 170 171 172
      end
    end

    context 'with a transaction' do
      it 'adds an event' do
        transaction = Gitlab::Metrics::Transaction.new

        expect(transaction).to receive(:add_event).with(:meow)

173
        expect(described_class).to receive(:current_transaction).
174 175
          and_return(transaction)

176
        described_class.add_event(:meow)
177 178 179
      end
    end
  end
180
end