From 52018ce54abc61ebfb44c4e176b004697999f4ea Mon Sep 17 00:00:00 2001 From: Hielke Jager Date: Wed, 6 Nov 2024 14:29:56 +0100 Subject: [PATCH 1/4] fix: get correct table name if table name is quoted --- .../instrumentation/pg/patches/connection.rb | 2 +- .../pg/test/fixtures/sql_table_name.json | 16 ++++++++++++++++ 2 files changed, 17 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 98814950b..0ad8c1c32 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..fb305bc20 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -50,5 +50,21 @@ { "name": "from_with_join", "sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column" + }, + { + "name": "table_name_with_single_quotes", + "sql": "SELECT columns FROM 'test_table'" + }, + { + "name": "table_name_with_double_quotes", + "sql": "SELECT columns FROM \"test_table\"" + }, + { + "name": "table_name_with_single_quotes", + "sql": "SELECT columns FROM 'test_table'" + }, + { + "name": "table_name_with_double_quotes", + "sql": "SELECT columns FROM \"test_table\"" } ] \ No newline at end of file From 007a1bf6ab7e09b23d6b2a5021408535c9307421 Mon Sep 17 00:00:00 2001 From: Hielke Jager Date: Wed, 6 Nov 2024 14:32:52 +0100 Subject: [PATCH 2/4] Remove double test --- instrumentation/pg/test/fixtures/sql_table_name.json | 8 -------- 1 file changed, 8 deletions(-) diff --git a/instrumentation/pg/test/fixtures/sql_table_name.json b/instrumentation/pg/test/fixtures/sql_table_name.json index fb305bc20..3e972ec6d 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -55,14 +55,6 @@ "name": "table_name_with_single_quotes", "sql": "SELECT columns FROM 'test_table'" }, - { - "name": "table_name_with_double_quotes", - "sql": "SELECT columns FROM \"test_table\"" - }, - { - "name": "table_name_with_single_quotes", - "sql": "SELECT columns FROM 'test_table'" - }, { "name": "table_name_with_double_quotes", "sql": "SELECT columns FROM \"test_table\"" From f3b9dfbe6fa8f2a0645cc8e0cac3418ac31e1aeb Mon Sep 17 00:00:00 2001 From: Hielke Jager Date: Wed, 6 Nov 2024 14:51:01 +0100 Subject: [PATCH 3/4] Add new line to sql_table_name.json --- instrumentation/pg/test/fixtures/sql_table_name.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/pg/test/fixtures/sql_table_name.json b/instrumentation/pg/test/fixtures/sql_table_name.json index 3e972ec6d..7b8a4ab27 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -59,4 +59,4 @@ "name": "table_name_with_double_quotes", "sql": "SELECT columns FROM \"test_table\"" } - ] \ No newline at end of file +] From 3db20170d310c254e8f1771369efa2aeb15fd9f3 Mon Sep 17 00:00:00 2001 From: Hielke Jager Date: Sun, 10 Nov 2024 20:37:22 +0100 Subject: [PATCH 4/4] Stop accepting table name in single quotes --- .../opentelemetry/instrumentation/pg/patches/connection.rb | 2 +- instrumentation/pg/test/fixtures/sql_table_name.json | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 0ad8c1c32..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 7b8a4ab27..7cf96681f 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -51,10 +51,6 @@ "name": "from_with_join", "sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column" }, - { - "name": "table_name_with_single_quotes", - "sql": "SELECT columns FROM 'test_table'" - }, { "name": "table_name_with_double_quotes", "sql": "SELECT columns FROM \"test_table\""