instrumentation_spec.rb 3.4 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Metrics::Instrumentation do
4 5
  let(:transaction) { Gitlab::Metrics::Transaction.new }

6 7 8 9 10 11 12 13 14 15
  before do
    @dummy = Class.new do
      def self.foo(text = 'foo')
        text
      end

      def bar(text = 'bar')
        text
      end
    end
16 17

    allow(@dummy).to receive(:name).and_return('Dummy')
18 19
  end

Y
Yorick Peterse 已提交
20 21 22 23 24 25 26 27
  describe '.configure' do
    it 'yields self' do
      described_class.configure do |c|
        expect(c).to eq(described_class)
      end
    end
  end

28 29 30 31 32
  describe '.instrument_method' do
    describe 'with metrics enabled' do
      before do
        allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)

Y
Yorick Peterse 已提交
33
        described_class.instrument_method(@dummy, :foo)
34 35 36 37 38 39 40 41 42 43
      end

      it 'renames the original method' do
        expect(@dummy).to respond_to(:_original_foo)
      end

      it 'calls the instrumented method with the correct arguments' do
        expect(@dummy.foo).to eq('foo')
      end

44
      it 'tracks the call duration upon calling the method' do
45
        allow(described_class).to receive(:transaction).
46 47 48 49 50
          and_return(transaction)

        expect(transaction).to receive(:add_metric).
          with(described_class::SERIES, an_instance_of(Hash),
               method: 'Dummy.foo')
51 52 53 54 55 56 57 58 59 60 61

        @dummy.foo
      end
    end

    describe 'with metrics disabled' do
      before do
        allow(Gitlab::Metrics).to receive(:enabled?).and_return(false)
      end

      it 'does not instrument the method' do
62
        described_class.instrument_method(@dummy, :foo)
63 64 65 66 67 68 69 70 71 72 73

        expect(@dummy).to_not respond_to(:_original_foo)
      end
    end
  end

  describe '.instrument_instance_method' do
    describe 'with metrics enabled' do
      before do
        allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)

74
        described_class.
75 76 77 78 79 80 81 82 83 84 85
          instrument_instance_method(@dummy, :bar)
      end

      it 'renames the original method' do
        expect(@dummy.method_defined?(:_original_bar)).to eq(true)
      end

      it 'calls the instrumented method with the correct arguments' do
        expect(@dummy.new.bar).to eq('bar')
      end

86
      it 'tracks the call duration upon calling the method' do
87
        allow(described_class).to receive(:transaction).
88 89 90 91 92
          and_return(transaction)

        expect(transaction).to receive(:add_metric).
          with(described_class::SERIES, an_instance_of(Hash),
               method: 'Dummy#bar')
93 94 95 96 97 98 99 100 101 102 103

        @dummy.new.bar
      end
    end

    describe 'with metrics disabled' do
      before do
        allow(Gitlab::Metrics).to receive(:enabled?).and_return(false)
      end

      it 'does not instrument the method' do
104
        described_class.
105 106 107 108 109 110
          instrument_instance_method(@dummy, :bar)

        expect(@dummy.method_defined?(:_original_bar)).to eq(false)
      end
    end
  end
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  describe '.instrument_methods' do
    before do
      allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)
    end

    it 'instruments all public class methods' do
      described_class.instrument_methods(@dummy)

      expect(@dummy).to respond_to(:_original_foo)
    end
  end

  describe '.instrument_instance_methods' do
    before do
      allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)
    end

    it 'instruments all public instance methods' do
      described_class.instrument_instance_methods(@dummy)

      expect(@dummy.method_defined?(:_original_bar)).to eq(true)
    end
  end
135
end