BigW Consortium Gitlab

transaction_spec.rb 4.78 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Metrics::Transaction do
4
  let(:transaction) { described_class.new }
5 6 7 8 9 10 11 12 13

  describe '#duration' do
    it 'returns the duration of a transaction in seconds' do
      transaction.run { sleep(0.5) }

      expect(transaction.duration).to be >= 0.5
    end
  end

14 15 16 17 18 19 20 21
  describe '#allocated_memory' do
    it 'returns the allocated memory in bytes' do
      transaction.run { 'a' * 32 }

      expect(transaction.allocated_memory).to be_a_kind_of(Numeric)
    end
  end

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  describe '#run' do
    it 'yields the supplied block' do
      expect { |b| transaction.run(&b) }.to yield_control
    end

    it 'stores the transaction in the current thread' do
      transaction.run do
        expect(Thread.current[described_class::THREAD_KEY]).to eq(transaction)
      end
    end

    it 'removes the transaction from the current thread upon completion' do
      transaction.run { }

      expect(Thread.current[described_class::THREAD_KEY]).to be_nil
    end
  end

  describe '#add_metric' do
41
    it 'adds a metric to the transaction' do
42 43
      expect(Gitlab::Metrics::Metric).to receive(:new).
        with('rails_foo', { number: 10 }, {})
44 45 46 47 48

      transaction.add_metric('foo', number: 10)
    end
  end

49 50 51
  describe '#method_call_for' do
    it 'returns a MethodCall' do
      method = transaction.method_call_for('Foo#bar')
52

53
      expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall)
54 55 56
    end
  end

57 58 59 60 61
  describe '#increment' do
    it 'increments a counter' do
      transaction.increment(:time, 1)
      transaction.increment(:time, 2)

62 63
      values = { duration: 0.0, time: 3, allocated_memory: a_kind_of(Numeric) }

64 65
      expect(transaction).to receive(:add_metric).
        with('transactions', values, {})
66 67 68 69 70

      transaction.track_self
    end
  end

71 72 73 74
  describe '#set' do
    it 'sets a value' do
      transaction.set(:number, 10)

75 76 77 78 79 80
      values = {
        duration:         0.0,
        number:           10,
        allocated_memory: a_kind_of(Numeric)
      }

81 82
      expect(transaction).to receive(:add_metric).
        with('transactions', values, {})
83 84 85 86 87

      transaction.track_self
    end
  end

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  describe '#add_tag' do
    it 'adds a tag' do
      transaction.add_tag(:foo, 'bar')

      expect(transaction.tags).to eq({ foo: 'bar' })
    end
  end

  describe '#finish' do
    it 'tracks the transaction details and submits them to Sidekiq' do
      expect(transaction).to receive(:track_self)
      expect(transaction).to receive(:submit)

      transaction.finish
    end
  end

  describe '#track_self' do
    it 'adds a metric for the transaction itself' do
107 108 109 110 111
      values = {
        duration:         transaction.duration,
        allocated_memory: a_kind_of(Numeric)
      }

112 113
      expect(transaction).to receive(:add_metric).
        with('transactions', values, {})
114 115 116 117 118 119 120 121 122

      transaction.track_self
    end
  end

  describe '#submit' do
    it 'submits the metrics to Sidekiq' do
      transaction.track_self

123 124
      expect(Gitlab::Metrics).to receive(:submit_metrics).
        with([an_instance_of(Hash)])
125 126 127

      transaction.submit
    end
128 129 130 131 132 133 134 135

    it 'adds the action as a tag for every metric' do
      transaction.action = 'Foo#bar'
      transaction.track_self

      hash = {
        series:    'rails_transactions',
        tags:      { action: 'Foo#bar' },
136
        values:    { duration: 0.0, allocated_memory: a_kind_of(Numeric) },
Douwe Maan committed
137
        timestamp: a_kind_of(Integer)
138 139
      }

140 141
      expect(Gitlab::Metrics).to receive(:submit_metrics).
        with([hash])
142 143 144

      transaction.submit
    end
145 146 147 148 149 150 151 152 153

    it 'does not add an action tag for events' do
      transaction.action = 'Foo#bar'
      transaction.add_event(:meow)

      hash = {
        series:    'events',
        tags:      { event: :meow },
        values:    { count: 1 },
Douwe Maan committed
154
        timestamp: a_kind_of(Integer)
155 156
      }

157 158
      expect(Gitlab::Metrics).to receive(:submit_metrics).
        with([hash])
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

      transaction.submit
    end
  end

  describe '#add_event' do
    it 'adds a metric' do
      transaction.add_event(:meow)

      expect(transaction.metrics[0]).to be_an_instance_of(Gitlab::Metrics::Metric)
    end

    it "does not prefix the metric's series name" do
      transaction.add_event(:meow)

      metric = transaction.metrics[0]

      expect(metric.series).to eq(described_class::EVENT_SERIES)
    end

    it 'tracks a counter for every event' do
      transaction.add_event(:meow)

      metric = transaction.metrics[0]

      expect(metric.values).to eq(count: 1)
    end

    it 'tracks the event name' do
      transaction.add_event(:meow)

      metric = transaction.metrics[0]

      expect(metric.tags).to eq(event: :meow)
    end

    it 'allows tracking of custom tags' do
      transaction.add_event(:meow, animal: 'cat')

      metric = transaction.metrics[0]

      expect(metric.tags).to eq(event: :meow, animal: 'cat')
    end
202 203
  end
end