Skip to content

Commit b66a16c

Browse files
author
Yorick Peterse
committed
Use string evaluation for method instrumentation
This is faster than using define_method since we don't have to keep block bindings around.
1 parent 60a6a24 commit b66a16c

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

lib/gitlab/metrics/instrumentation.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ def self.instrument(type, mod, name)
3131
alias_name = "_original_#{name}"
3232
target = type == :instance ? mod : mod.singleton_class
3333

34-
target.class_eval do
35-
alias_method(alias_name, name)
34+
target.class_eval <<-EOF, __FILE__, __LINE__ + 1
35+
alias_method :#{alias_name}, :#{name}
3636
37-
define_method(name) do |*args, &block|
38-
ActiveSupport::Notifications.
39-
instrument("#{type}_method.method_call", module: mod, name: name) do
40-
__send__(alias_name, *args, &block)
37+
def #{name}(*args, &block)
38+
ActiveSupport::Notifications
39+
.instrument("#{type}_method.method_call",
40+
module: #{mod.name.inspect},
41+
name: #{name.inspect}) do
42+
#{alias_name}(*args, &block)
4143
end
4244
end
43-
end
45+
EOF
4446
end
4547
end
4648
end

lib/gitlab/metrics/subscribers/method_call.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class MethodCall < ActiveSupport::Subscriber
1010
def instance_method(event)
1111
return unless current_transaction
1212

13-
label = "#{event.payload[:module].name}##{event.payload[:name]}"
13+
label = "#{event.payload[:module]}##{event.payload[:name]}"
1414

1515
add_metric(label, event.duration)
1616
end
1717

1818
def class_method(event)
1919
return unless current_transaction
2020

21-
label = "#{event.payload[:module].name}.#{event.payload[:name]}"
21+
label = "#{event.payload[:module]}.#{event.payload[:name]}"
2222

2323
add_metric(label, event.duration)
2424
end

spec/lib/gitlab/metrics/instrumentation_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def bar(text = 'bar')
1111
text
1212
end
1313
end
14+
15+
allow(@dummy).to receive(:name).and_return('Dummy')
1416
end
1517

1618
describe '.instrument_method' do
@@ -31,7 +33,7 @@ def bar(text = 'bar')
3133

3234
it 'fires an ActiveSupport notification upon calling the method' do
3335
expect(ActiveSupport::Notifications).to receive(:instrument).
34-
with('class_method.method_call', module: @dummy, name: :foo)
36+
with('class_method.method_call', module: 'Dummy', name: :foo)
3537

3638
@dummy.foo
3739
end
@@ -69,7 +71,7 @@ def bar(text = 'bar')
6971

7072
it 'fires an ActiveSupport notification upon calling the method' do
7173
expect(ActiveSupport::Notifications).to receive(:instrument).
72-
with('instance_method.method_call', module: @dummy, name: :bar)
74+
with('instance_method.method_call', module: 'Dummy', name: :bar)
7375

7476
@dummy.new.bar
7577
end

spec/lib/gitlab/metrics/subscribers/method_call_spec.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
let(:subscriber) { described_class.new }
77

88
let(:event) do
9-
double(:event, duration: 0.2,
10-
payload: { module: double(:mod, name: 'Foo'), name: :foo })
9+
double(:event, duration: 0.2, payload: { module: 'Foo', name: :foo })
1110
end
1211

1312
before do

0 commit comments

Comments
 (0)