Skip to content

Commit 651507b

Browse files
Merge branch 'main' into rubocop-perf
2 parents e588112 + 192b262 commit 651507b

File tree

6 files changed

+92
-5
lines changed

6 files changed

+92
-5
lines changed

instrumentation/active_record/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release History: opentelemetry-instrumentation-active_record
22

3+
### v0.8.1 / 2024-11-21
4+
5+
* FIXED: Pass block argument in ActiveRecord `find_by_sql` patch.
6+
37
### v0.8.0 / 2024-10-22
48

59
* BREAKING CHANGE: Rename Active Record find_by_sql spans to query

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/lib/opentelemetry/instrumentation/active_record/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module OpenTelemetry
88
module Instrumentation
99
module ActiveRecord
10-
VERSION = '0.8.0'
10+
VERSION = '0.8.1'
1111
end
1212
end
1313
end

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

instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Patches
1515
# Module to prepend to PG::Connection for instrumentation
1616
module Connection # rubocop:disable Metrics/ModuleLength
1717
# Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands
18-
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
18+
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
1919

2020
PG::Constants::EXEC_ISH_METHODS.each do |method|
2121
define_method method do |*args, &block|

instrumentation/pg/test/fixtures/sql_table_name.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@
5050
{
5151
"name": "from_with_join",
5252
"sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column"
53+
},
54+
{
55+
"name": "table_name_with_double_quotes",
56+
"sql": "SELECT columns FROM \"test_table\""
5357
}
54-
]
58+
]

0 commit comments

Comments
 (0)