Skip to content

Commit

Permalink
Merge branch 'main' into rubocop-perf
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin authored Nov 24, 2024
2 parents e588112 + 192b262 commit 651507b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions instrumentation/active_record/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History: opentelemetry-instrumentation-active_record

### v0.8.1 / 2024-11-21

* FIXED: Pass block argument in ActiveRecord `find_by_sql` patch.

### v0.8.0 / 2024-10-22

* BREAKING CHANGE: Rename Active Record find_by_sql spans to query
Expand Down
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)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module OpenTelemetry
module Instrumentation
module ActiveRecord
VERSION = '0.8.0'
VERSION = '0.8.1'
end
end
end
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Patches
# Module to prepend to PG::Connection for instrumentation
module Connection # rubocop:disable Metrics/ModuleLength
# Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands
TABLE_NAME = /\b(?:FROM|INTO|UPDATE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|DROP\s+TABLE(?:\s+IF\s+EXISTS)?|ALTER\s+TABLE(?:\s+IF\s+EXISTS)?)\s+([\w\.]+)/i
TABLE_NAME = /\b(?:FROM|INTO|UPDATE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|DROP\s+TABLE(?:\s+IF\s+EXISTS)?|ALTER\s+TABLE(?:\s+IF\s+EXISTS)?)\s+["]?([\w\.]+)["]?/i

PG::Constants::EXEC_ISH_METHODS.each do |method|
define_method method do |*args, &block|
Expand Down
6 changes: 5 additions & 1 deletion instrumentation/pg/test/fixtures/sql_table_name.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@
{
"name": "from_with_join",
"sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column"
},
{
"name": "table_name_with_double_quotes",
"sql": "SELECT columns FROM \"test_table\""
}
]
]

0 comments on commit 651507b

Please sign in to comment.