From 081a3b0f8caadb06649ae1000823e0fb555ab600 Mon Sep 17 00:00:00 2001 From: Keiko Oda Date: Sat, 11 May 2024 05:46:37 +0900 Subject: [PATCH] feat: Support prepend SQL comment for PG instrumentation (#690) Co-authored-by: Ariel Valentin Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> --- .../instrumentation/pg/patches/connection.rb | 4 +++- .../instrumentation/pg/instrumentation_test.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 49d0946d5..631610acf 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb @@ -112,7 +112,9 @@ def span_attrs(kind, *args) def extract_operation(sql) # From: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/9244a08a8d014afe26b82b91cf86e407c2599d73/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts#L35 - sql.to_s.split[0].to_s.upcase + # Ignores prepend comment + comment_regex = %r{\A\/\*.*?\*\/}m + sql.to_s.sub(comment_regex, '').split[0].to_s.upcase end def span_name(operation) diff --git a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb index 332e90720..b7e97b9ac 100644 --- a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb +++ b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb @@ -177,6 +177,18 @@ end end + it 'ignores prepend comment to extract operation' do + client.query('/* comment */ SELECT 1') + + _(span.name).must_equal 'SELECT postgres' + _(span.attributes['db.system']).must_equal 'postgresql' + _(span.attributes['db.name']).must_equal 'postgres' + _(span.attributes['db.statement']).must_equal '/* comment */ SELECT 1' + _(span.attributes['db.operation']).must_equal 'SELECT' + _(span.attributes['net.peer.name']).must_equal host.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i + end + it 'only caches 50 prepared statement names' do 51.times { |i| client.prepare("foo#{i}", "SELECT $1 AS foo#{i}") } client.exec_prepared('foo0', [1])