Skip to content

Commit 16bef85

Browse files
authored
fix: pass block argument in ActiveRecord find_by_sql patch (#1259)
1 parent bf6394d commit 16bef85

File tree

2 files changed

+81
-2
lines changed
  • instrumentation/active_record

2 files changed

+81
-2
lines changed

instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/querying.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class << base
2020
module ClassMethods
2121
method_name = ::ActiveRecord.version >= Gem::Version.new('7.0.0') ? :_query_by_sql : :find_by_sql
2222

23-
define_method(method_name) do |*args, **kwargs|
23+
define_method(method_name) do |*args, **kwargs, &block|
2424
tracer.in_span("#{self} query") do
25-
super(*args, **kwargs)
25+
super(*args, **kwargs, &block)
2626
end
2727
end
2828

instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
let(:spans) { exporter.finished_spans }
1515

1616
before { exporter.reset }
17+
after do
18+
ActiveRecord::Base.subclasses.each do |model|
19+
model.connection.truncate(model.table_name)
20+
end
21+
end
1722

1823
describe 'query' do
1924
it 'traces' do
@@ -28,5 +33,79 @@
2833
_(user_find_spans.length).must_equal(2)
2934
_(account_find_span).wont_be_nil
3035
end
36+
37+
describe 'find_by_sql' do
38+
it 'creates a span' do
39+
Account.create!
40+
41+
Account.find_by_sql('SELECT * FROM accounts')
42+
43+
account_find_span = spans.find { |s| s.name == 'Account query' }
44+
_(account_find_span).wont_be_nil
45+
_(account_find_span.attributes).must_be_empty
46+
end
47+
48+
describe 'given a block' do
49+
it 'creates a span' do
50+
account = Account.create!
51+
52+
record_ids = []
53+
54+
Account.find_by_sql('SELECT * FROM accounts') do |record|
55+
record_ids << record.id
56+
end
57+
58+
account_find_span = spans.find { |s| s.name == 'Account query' }
59+
_(account_find_span).wont_be_nil
60+
_(account_find_span.attributes).must_be_empty
61+
62+
_(record_ids).must_equal([account.id])
63+
end
64+
end
65+
end
66+
67+
describe 'find_by' do
68+
it 'creates a span' do
69+
account = Account.create!
70+
User.create!(account: account)
71+
72+
Account.find_by(id: account.id)
73+
74+
account_find_span = spans.find { |s| s.name == 'Account query' }
75+
_(account_find_span).wont_be_nil
76+
_(account_find_span.attributes).must_be_empty
77+
end
78+
end
79+
80+
describe 'find' do
81+
it 'creates a span' do
82+
account = Account.create!
83+
User.create!(account: account)
84+
85+
Account.find(account.id)
86+
87+
account_find_span = spans.find { |s| s.name == 'Account query' }
88+
_(account_find_span).wont_be_nil
89+
_(account_find_span.attributes).must_be_empty
90+
end
91+
92+
describe 'given a block' do
93+
it 'creates a span' do
94+
account = Account.create!
95+
User.create!(account: account)
96+
97+
record_ids = []
98+
99+
Account.find(account.id) do |record|
100+
record_ids << record.id
101+
end
102+
103+
account_find_span = spans.find { |s| s.name == 'Account query' }
104+
_(account_find_span).wont_be_nil
105+
_(account_find_span.attributes).must_be_empty
106+
_(record_ids).must_equal([account.id])
107+
end
108+
end
109+
end
31110
end
32111
end

0 commit comments

Comments
 (0)