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

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

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

12 13 14 15 16 17 18 19 20 21 22 23
      class << self
        def buzz(text = 'buzz')
          text
        end
        private :buzz

        def flaky(text = 'flaky')
          text
        end
        protected :flaky
      end

24 25 26
      def bar(text = 'bar')
        text
      end
27 28 29 30 31 32 33 34 35 36

      def wadus(text = 'wadus')
        text
      end
      private :wadus

      def chaf(text = 'chaf')
        text
      end
      protected :chaf
37
    end
38 39

    allow(@dummy).to receive(:name).and_return('Dummy')
40 41
  end

Y
Yorick Peterse 已提交
42 43 44 45 46 47
  describe '.series' do
    it 'returns a String' do
      expect(described_class.series).to be_an_instance_of(String)
    end
  end

Y
Yorick Peterse 已提交
48 49 50 51 52 53 54 55
  describe '.configure' do
    it 'yields self' do
      described_class.configure do |c|
        expect(c).to eq(described_class)
      end
    end
  end

56 57 58 59 60
  describe '.instrument_method' do
    describe 'with metrics enabled' do
      before do
        allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)

Y
Yorick Peterse 已提交
61
        described_class.instrument_method(@dummy, :foo)
62 63
      end

64 65 66 67 68 69 70 71 72 73
      it 'instruments the Class' do
        target = @dummy.singleton_class

        expect(described_class.instrumented?(target)).to eq(true)
      end

      it 'defines a proxy method' do
        mod = described_class.proxy_module(@dummy.singleton_class)

        expect(mod.method_defined?(:foo)).to eq(true)
74 75 76 77 78 79
      end

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

80
      it 'tracks the call duration upon calling the method' do
81 82 83
        allow(Gitlab::Metrics).to receive(:method_call_threshold).
          and_return(0)

84
        allow(described_class).to receive(:transaction).
85 86
          and_return(transaction)

Y
Yorick Peterse 已提交
87
        expect_any_instance_of(Gitlab::Metrics::MethodCall).to receive(:measure)
88 89 90

        @dummy.foo
      end
91 92 93 94 95

      it 'does not track method calls below a given duration threshold' do
        allow(Gitlab::Metrics).to receive(:method_call_threshold).
          and_return(100)

96
        expect(transaction).not_to receive(:add_metric)
97 98 99

        @dummy.foo
      end
100 101 102 103 104 105 106 107 108 109

      it 'generates a method with the correct arity when using methods without arguments' do
        dummy = Class.new do
          def self.test; end
        end

        described_class.instrument_method(dummy, :test)

        expect(dummy.method(:test).arity).to eq(0)
      end
110 111 112 113 114 115 116 117

      describe 'when a module is instrumented multiple times' do
        it 'calls the instrumented method with the correct arguments' do
          described_class.instrument_method(@dummy, :foo)

          expect(@dummy.foo).to eq('foo')
        end
      end
118 119 120 121 122 123 124 125
    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
126
        described_class.instrument_method(@dummy, :foo)
127

128 129 130
        target = @dummy.singleton_class

        expect(described_class.instrumented?(target)).to eq(false)
131 132 133 134 135 136 137 138 139
      end
    end
  end

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

140
        described_class.
141 142 143
          instrument_instance_method(@dummy, :bar)
      end

144 145 146 147 148 149 150 151
      it 'instruments instances of the Class' do
        expect(described_class.instrumented?(@dummy)).to eq(true)
      end

      it 'defines a proxy method' do
        mod = described_class.proxy_module(@dummy)

        expect(mod.method_defined?(:bar)).to eq(true)
152 153 154 155 156 157
      end

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

158
      it 'tracks the call duration upon calling the method' do
159 160 161
        allow(Gitlab::Metrics).to receive(:method_call_threshold).
          and_return(0)

162
        allow(described_class).to receive(:transaction).
163 164
          and_return(transaction)

Y
Yorick Peterse 已提交
165
        expect_any_instance_of(Gitlab::Metrics::MethodCall).to receive(:measure)
166

167 168 169 170 171 172 173
        @dummy.new.bar
      end

      it 'does not track method calls below a given duration threshold' do
        allow(Gitlab::Metrics).to receive(:method_call_threshold).
          and_return(100)

174
        expect(transaction).not_to receive(:add_metric)
175

176 177 178 179 180 181 182 183 184 185
        @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
186
        described_class.
187 188
          instrument_instance_method(@dummy, :bar)

189
        expect(described_class.instrumented?(@dummy)).to eq(false)
190 191 192
      end
    end
  end
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  describe '.instrument_class_hierarchy' do
    before do
      allow(Gitlab::Metrics).to receive(:enabled?).and_return(true)

      @child1 = Class.new(@dummy) do
        def self.child1_foo; end
        def child1_bar; end
      end

      @child2 = Class.new(@child1) do
        def self.child2_foo; end
        def child2_bar; end
      end
    end

    it 'recursively instruments a class hierarchy' do
      described_class.instrument_class_hierarchy(@dummy)

212 213
      expect(described_class.instrumented?(@child1.singleton_class)).to eq(true)
      expect(described_class.instrumented?(@child2.singleton_class)).to eq(true)
214

215 216
      expect(described_class.instrumented?(@child1)).to eq(true)
      expect(described_class.instrumented?(@child2)).to eq(true)
217 218 219 220 221
    end

    it 'does not instrument the root module' do
      described_class.instrument_class_hierarchy(@dummy)

222
      expect(described_class.instrumented?(@dummy)).to eq(false)
223 224 225
    end
  end

226 227 228 229 230 231 232 233
  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)

234
      expect(described_class.instrumented?(@dummy.singleton_class)).to eq(true)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      expect(@dummy.method(:foo).source_location.first).to match(/instrumentation\.rb/)
    end

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

      expect(described_class.instrumented?(@dummy.singleton_class)).to eq(true)
      expect(@dummy.method(:flaky).source_location.first).to match(/instrumentation\.rb/)
    end

    it 'instruments all private instance methods' do
      described_class.instrument_methods(@dummy)

      expect(described_class.instrumented?(@dummy.singleton_class)).to eq(true)
      expect(@dummy.method(:buzz).source_location.first).to match(/instrumentation\.rb/)
250
    end
251 252 253 254 255 256 257 258 259 260 261

    it 'only instruments methods directly defined in the module' do
      mod = Module.new do
        def kittens
        end
      end

      @dummy.extend(mod)

      described_class.instrument_methods(@dummy)

262
      expect(@dummy).not_to respond_to(:_original_kittens)
263
    end
264 265 266 267 268 269

    it 'can take a block to determine if a method should be instrumented' do
      described_class.instrument_methods(@dummy) do
        false
      end

270
      expect(@dummy).not_to respond_to(:_original_foo)
271
    end
272 273 274 275 276 277 278 279 280 281
  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)

282
      expect(described_class.instrumented?(@dummy)).to eq(true)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
      expect(@dummy.new.method(:bar).source_location.first).to match(/instrumentation\.rb/)
    end

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

      expect(described_class.instrumented?(@dummy)).to eq(true)
      expect(@dummy.new.method(:chaf).source_location.first).to match(/instrumentation\.rb/)
    end

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

      expect(described_class.instrumented?(@dummy)).to eq(true)
      expect(@dummy.new.method(:wadus).source_location.first).to match(/instrumentation\.rb/)
298
    end
299 300 301 302 303 304 305 306 307 308 309

    it 'only instruments methods directly defined in the module' do
      mod = Module.new do
        def kittens
        end
      end

      @dummy.include(mod)

      described_class.instrument_instance_methods(@dummy)

310
      expect(@dummy.new.method(:kittens).source_location.first).not_to match(/instrumentation\.rb/)
311
    end
312 313 314 315 316 317

    it 'can take a block to determine if a method should be instrumented' do
      described_class.instrument_instance_methods(@dummy) do
        false
      end

318
      expect(@dummy.new.method(:bar).source_location.first).not_to match(/instrumentation\.rb/)
319
    end
320
  end
321
end