From 16bef8547c533e8691ef446244ba6603650e4018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Peder=20Brandtz=C3=A6g?= Date: Thu, 21 Nov 2024 13:03:58 +0100 Subject: [PATCH 1/3] fix: pass block argument in ActiveRecord `find_by_sql` patch (#1259) --- .../active_record/patches/querying.rb | 4 +- .../active_record/patches/querying_test.rb | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/querying.rb b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/querying.rb index 094575ae4..0b9f37904 100644 --- a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/querying.rb +++ b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/patches/querying.rb @@ -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 diff --git a/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb b/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb index 879af50e6..5a398a3c2 100644 --- a/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb +++ b/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb @@ -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 @@ -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 From 7ad08f9c80e18fd2ae9e5ff56ae23421c1b6aed9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 21 Nov 2024 07:17:18 -0600 Subject: [PATCH 2/3] release: Release opentelemetry-instrumentation-active_record 0.8.1 (was 0.8.0) (#1262) * release: Release opentelemetry-instrumentation-active_record 0.8.1 (was 0.8.0) * squash: Update CHANGELOG.md --------- Co-authored-by: OpenTelemetry Bot <107717825+opentelemetrybot@users.noreply.github.com> Co-authored-by: Ariel Valentin --- instrumentation/active_record/CHANGELOG.md | 4 ++++ .../opentelemetry/instrumentation/active_record/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/instrumentation/active_record/CHANGELOG.md b/instrumentation/active_record/CHANGELOG.md index 1d9746ce9..4f5f80d27 100644 --- a/instrumentation/active_record/CHANGELOG.md +++ b/instrumentation/active_record/CHANGELOG.md @@ -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 diff --git a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/version.rb b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/version.rb index 1f42c3003..bf2bf31c3 100644 --- a/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/version.rb +++ b/instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/version.rb @@ -7,7 +7,7 @@ module OpenTelemetry module Instrumentation module ActiveRecord - VERSION = '0.8.0' + VERSION = '0.8.1' end end end From 192b26280794ffa572e9c891053adeac5c729277 Mon Sep 17 00:00:00 2001 From: Hielke Jager Date: Sun, 24 Nov 2024 18:38:18 +0100 Subject: [PATCH 3/3] fix: get correct table name if table name is quoted (#1234) * fix: get correct table name if table name is quoted * Remove double test * Add new line to sql_table_name.json * Stop accepting table name in single quotes --------- Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> Co-authored-by: Ariel Valentin --- .../opentelemetry/instrumentation/pg/patches/connection.rb | 2 +- instrumentation/pg/test/fixtures/sql_table_name.json | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 98814950b..a40da7cd7 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb @@ -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| diff --git a/instrumentation/pg/test/fixtures/sql_table_name.json b/instrumentation/pg/test/fixtures/sql_table_name.json index eacd9571f..7cf96681f 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -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\"" } - ] \ No newline at end of file +]