Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ class << base

# Contains ActiveRecord::Querying to be patched
module ClassMethods
def find_by_sql(...)
tracer.in_span("#{self}.find_by_sql") do
super
if ::ActiveRecord.version >= Gem::Version.new('7.0.0')
def _query_by_sql(...)
tracer.in_span("#{self} query") do
super
end
end
else
def find_by_sql(...)
tracer.in_span("#{self} query") do
super
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@

before { exporter.reset }

describe 'find_by_sql' do
describe 'query' do
it 'traces' do
Account.create!

User.find_by_sql('SELECT * FROM users')
Account.first.users.to_a

user_find_spans = spans.select { |s| s.name == 'User query' }
account_find_span = spans.find { |s| s.name == 'Account query' }

find_span = spans.find { |s| s.name == 'User.find_by_sql' }
_(find_span).wont_be_nil
_(user_find_spans.length).must_equal(2)
_(account_find_span).wont_be_nil
end
end
end
11 changes: 10 additions & 1 deletion instrumentation/active_record/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@
database: 'db/development.sqlite3'
)

# Create User model
# Create ActiveRecord models
class Account < ActiveRecord::Base
has_many :users
end

class User < ActiveRecord::Base
belongs_to :account

validate :name_if_present

scope :recently_created, -> { where('created_at > ?', Time.now - 3600) }
Expand All @@ -54,9 +60,12 @@ class SuperUser < ActiveRecord::Base; end
# Simple migration to create a table to test against
class CreateUserTable < ActiveRecord::Migration[migration_version]
def change
create_table :accounts, &:timestamps

create_table :users do |t|
t.string 'name'
t.integer 'counter'
t.references 'account'
t.timestamps
end

Expand Down