summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/instrumentation_spec.rb
blob: ad4290c43bbc725aaffd40e756f144f156f6bb32 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
require 'spec_helper'

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

  before do
    @dummy = Class.new do
      def self.foo(text = 'foo')
        text
      end

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

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

  describe '.configure' do
    it 'yields self' do
      described_class.configure do |c|
        expect(c).to eq(described_class)
      end
    end
  end

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

        described_class.instrument_method(@dummy, :foo)
      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

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

        allow(described_class).to receive(:transaction).
          and_return(transaction)

        expect(transaction).to receive(:increment).
          with(:method_duration, a_kind_of(Numeric))

        expect(transaction).to receive(:add_metric).
          with(described_class::SERIES, an_instance_of(Hash),
               method: 'Dummy.foo')

        @dummy.foo
      end

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

        expect(transaction).to_not receive(:add_metric)

        @dummy.foo
      end

      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
    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
        described_class.instrument_method(@dummy, :foo)

        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)

        described_class.
          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

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

        allow(described_class).to receive(:transaction).
          and_return(transaction)

        expect(transaction).to receive(:increment).
          with(:method_duration, a_kind_of(Numeric))

        expect(transaction).to receive(:add_metric).
          with(described_class::SERIES, an_instance_of(Hash),
               method: 'Dummy#bar')

        @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)

        expect(transaction).to_not receive(:add_metric)

        @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
        described_class.
          instrument_instance_method(@dummy, :bar)

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

  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)

      expect(@child1).to respond_to(:_original_child1_foo)
      expect(@child2).to respond_to(:_original_child2_foo)

      expect(@child1.method_defined?(:_original_child1_bar)).to eq(true)
      expect(@child2.method_defined?(:_original_child2_bar)).to eq(true)
    end

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

      expect(@dummy).to_not respond_to(:_original_foo)
      expect(@dummy.method_defined?(:_original_bar)).to eq(false)
    end
  end

  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

    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)

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

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

      expect(@dummy).to_not 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

    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)

      expect(@dummy.method_defined?(:_original_kittens)).to eq(false)
    end

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

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