Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass block argument in ActiveRecord find_by_sql patch #1259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class << base
module ClassMethods
method_name = ::ActiveRecord.version >= Gem::Version.new('7.0.0') ? :_query_by_sql : :find_by_sql

define_method(method_name) do |*args, **kwargs|
define_method(method_name) do |*args, **kwargs, &block|
tracer.in_span("#{self} query") do
super(*args, **kwargs)
super(*args, **kwargs, &block)
arielvalentin marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
let(:spans) { exporter.finished_spans }

before { exporter.reset }
after do
ActiveRecord::Base.subclasses.each do |model|
model.connection.truncate(model.table_name)
end
end

describe 'query' do
it 'traces' do
Expand All @@ -28,5 +33,79 @@
_(user_find_spans.length).must_equal(2)
_(account_find_span).wont_be_nil
end

describe 'find_by_sql' do
it 'creates a span' do
Account.create!

Account.find_by_sql('SELECT * FROM accounts')

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end

describe 'given a block' do
it 'creates a span' do
account = Account.create!

record_ids = []

Account.find_by_sql('SELECT * FROM accounts') do |record|
record_ids << record.id
end

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty

_(record_ids).must_equal([account.id])
end
end
end

describe 'find_by' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

Account.find_by(id: account.id)

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end
end

describe 'find' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

Account.find(account.id)

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end

describe 'given a block' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

record_ids = []

Account.find(account.id) do |record|
record_ids << record.id
end

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
_(record_ids).must_equal([account.id])
end
end
end
end
end
Loading