diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 515e8262a..f8cde570f 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb @@ -14,6 +14,9 @@ module PG 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 + PG::Constants::EXEC_ISH_METHODS.each do |method| define_method method do |*args, &block| span_name, attrs = span_attrs(:query, *args) @@ -129,10 +132,7 @@ def validated_operation(operation) end def collection_name(text) - # Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands - pattern = /\b(?:FROM|INTO|UPDATE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|DROP\s+TABLE\s+IF\s+EXISTS)\s+([\w\.]+)/i - - text.scan(pattern).flatten[0] + text.scan(TABLE_NAME).flatten[0] end def client_attributes diff --git a/instrumentation/pg/test/fixtures/sql_table_name.json b/instrumentation/pg/test/fixtures/sql_table_name.json index e17ac991b..c897ddb21 100644 --- a/instrumentation/pg/test/fixtures/sql_table_name.json +++ b/instrumentation/pg/test/fixtures/sql_table_name.json @@ -5,54 +5,50 @@ }, { "name": "select_count_from", - "sql": "SELECT COUNT(*) FROM table_name WHERE condition" + "sql": "SELECT COUNT(*) FROM test_table WHERE condition" }, { "name": "from_with_subquery", - "sql": "SELECT * FROM (SELECT * FROM table_name) AS table_alias" + "sql": "SELECT * FROM (SELECT * FROM test_table) AS table_alias" }, { "name": "insert_into", "sql": "INSERT INTO table_name (column1, column2) VALUES (value1, value2)" }, - { - "name": "drop_table", - "sql": "DROP TABLE table_name" - }, { "name": "update", - "sql": "UPDATE table_name SET column1 = value1 WHERE condition" + "sql": "UPDATE test_table SET column1 = value1 WHERE condition" }, { "name": "delete_from", - "sql": "DELETE FROM table_name WHERE condition" + "sql": "DELETE FROM test_table WHERE condition" }, { "name": "create_table", - "sql": "CREATE TABLE table_name (column1 datatype, column2 datatype)" + "sql": "CREATE TABLE test_table (column1 datatype, column2 datatype)" }, { "name": "create_table_if_not_exists", - "sql": "CREATE TABLE IF NOT EXISTS table_name (column1 datatype, column2 datatype)" + "sql": "CREATE TABLE IF NOT EXISTS test_table (column1 datatype, column2 datatype)" }, { "name": "alter_table", - "sql": "ALTER TABLE table_name ADD column_name datatype" + "sql": "ALTER TABLE test_table ADD column_name datatype" }, { "name": "drop_table", - "sql": "DROP TABLE table_name" + "sql": "DROP TABLE test_table" }, { "name": "drop_table_if_exists", - "sql": "DROP TABLE IF EXISTS table_name" + "sql": "DROP TABLE IF EXISTS test_table" }, { "name": "insert_into", - "sql": "INSERT INTO X values('', 'a''b c',0, 1 , 'd''e f''s h')" + "sql": "INSERT INTO test_table values('', 'a''b c',0, 1 , 'd''e f''s h')" }, { "name": "from_with_join", - "sql": "SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column" + "sql": "SELECT columns FROM test_table JOIN table2 ON table1.column = table2.column" } ] \ No newline at end of file diff --git a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb index d3f184451..1c7592a7c 100644 --- a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb +++ b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb @@ -387,7 +387,7 @@ def self.load_fixture define_method(:"test_sql_table_name_#{name}") do table_name = client.send(:collection_name, query) - assert_equal('table_name', table_name) # TODO: use an expected name from fixtures or update fixtures to always use "table_name" + assert_equal('test_table', table_name) # TODO: use an expected name from fixtures or update fixtures to always use "table_name" end end end