From 9a4fe904776c2d76478f6220cf072ac475118255 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Wed, 4 Sep 2024 13:24:09 -0700 Subject: [PATCH 1/7] fix: Remove prepended SQL comments when truncating Previously, when a SQL query with a prepended comment exceeded the obfuscation limit, the query would be truncated without obfuscation. Now, when the obfuscator detects a prepended comment in a query that needs to be truncated, the prepended comment will be replaced with the placeholder and the remaining query will be truncated to the obfuscation limit. --- .../lib/opentelemetry/helpers/sql_obfuscation.rb | 4 ++++ .../sql-obfuscation/test/helpers/sql_obfuscation_test.rb | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index da0394b42..09e9edeb7 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -62,6 +62,8 @@ module SqlObfuscation hexadecimal_literals comments multi_line_comments] }.freeze + PREPENDED_COMMENT_REGEX = %r{^/\*.*\*/} + PLACEHOLDER = '?' # We use these to check whether the query contains any quote characters @@ -127,6 +129,8 @@ def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) # @api private def truncate_statement(sql, regex, limit) + sql = sql.gsub(PREPENDED_COMMENT_REGEX, PLACEHOLDER) if sql.match?(PREPENDED_COMMENT_REGEX) + first_match_index = sql.index(regex) truncation_message = "SQL truncated (> #{limit} characters)" return truncation_message unless first_match_index diff --git a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb index e340cbdb5..d76970698 100644 --- a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb +++ b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb @@ -26,6 +26,15 @@ def test_obfuscation_limit_truncates_query_after_first_match assert_equal(expected, result) end + def test_obfuscation_limit_obfuscates_and_truncates_when_query_has_prepended_comment + comment = '/*service.name:foo,deployment.environtment:production,tracecontext:00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00,rails.route:examples/bars#index,host.name:baz-abc123.example.com*/' + sql = "#{comment} SELECT user.id FROM users where user.login = 'secretUserNameThatShouldBeObfuscated'" + expected = "? SELECT user.id FROM users where user.login = ...\nSQL truncated (> 42 characters)" + result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) + + assert_equal(expected, result) + end + def test_obfuscation_limit_truncates_when_query_not_encoded_with_utf8 sql = "SELECT * from 😄 where users.id = 1 and users.😄 = 'test@test.com'" expected = "SELECT * from where users.id = ...\nSQL truncated (> 42 characters)" From 74ffb888cf8ae8f4161e1e78696991e3f992047d Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 10:06:50 -0700 Subject: [PATCH 2/7] fix: Return message when obfuscation limit hit When the obfusaction limit is hit, return a message stating this and do not attempt to obfuscate --- .../lib/opentelemetry/helpers/sql_obfuscation.rb | 16 +--------------- .../test/helpers/sql_obfuscation_test.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index 09e9edeb7..a09025102 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -62,8 +62,6 @@ module SqlObfuscation hexadecimal_literals comments multi_line_comments] }.freeze - PREPENDED_COMMENT_REGEX = %r{^/\*.*\*/} - PLACEHOLDER = '?' # We use these to check whether the query contains any quote characters @@ -116,8 +114,8 @@ def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) # Original MySQL UTF-8 Encoding Fixes: # https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/160 # https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/345 + return "SQL truncated (> #{obfuscation_limit} characters)" if sql.size > obfuscation_limit sql = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true) - return truncate_statement(sql, regex, obfuscation_limit) if sql.size > obfuscation_limit sql = sql.gsub(regex, PLACEHOLDER) return 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if CLEANUP_REGEX[adapter].match(sql) @@ -126,18 +124,6 @@ def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) rescue StandardError => e OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e) end - - # @api private - def truncate_statement(sql, regex, limit) - sql = sql.gsub(PREPENDED_COMMENT_REGEX, PLACEHOLDER) if sql.match?(PREPENDED_COMMENT_REGEX) - - first_match_index = sql.index(regex) - truncation_message = "SQL truncated (> #{limit} characters)" - return truncation_message unless first_match_index - - truncated_sql = sql[..first_match_index - 1] - "#{truncated_sql}...\n#{truncation_message}" - end end end end diff --git a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb index d76970698..f38eed831 100644 --- a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb +++ b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb @@ -18,26 +18,26 @@ def test_named_arg_defaults_obfuscates assert_equal(expected, result) end - def test_obfuscation_limit_truncates_query_after_first_match + def test_obfuscation_limit_returns_truncation_message sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - expected = "SELECT * from users where users.id = ...\nSQL truncated (> 42 characters)" + expected = "SQL truncated (> 42 characters)" result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) assert_equal(expected, result) end - def test_obfuscation_limit_obfuscates_and_truncates_when_query_has_prepended_comment + def test_obfuscation_limit_returns_truncation_message_when_query_has_prepended_comment comment = '/*service.name:foo,deployment.environtment:production,tracecontext:00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00,rails.route:examples/bars#index,host.name:baz-abc123.example.com*/' sql = "#{comment} SELECT user.id FROM users where user.login = 'secretUserNameThatShouldBeObfuscated'" - expected = "? SELECT user.id FROM users where user.login = ...\nSQL truncated (> 42 characters)" + expected = "SQL truncated (> 42 characters)" result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) assert_equal(expected, result) end - def test_obfuscation_limit_truncates_when_query_not_encoded_with_utf8 + def test_obfuscation_limit_returns_truncation_message_when_not_utf8 sql = "SELECT * from 😄 where users.id = 1 and users.😄 = 'test@test.com'" - expected = "SELECT * from where users.id = ...\nSQL truncated (> 42 characters)" + expected = "SQL truncated (> 42 characters)" result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) assert_equal(expected, result) From 3217b9046698dc92377d5354a62a5db6fbd056b4 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 10:07:51 -0700 Subject: [PATCH 3/7] feat!: Update obfusaction limit This obfuscation limit matches what New Relic uses for their Ruby agent. The previous 2000 limit seemed arbitrary. --- helpers/sql-obfuscation/README.md | 2 +- .../lib/opentelemetry/helpers/sql_obfuscation.rb | 2 +- .../lib/opentelemetry/instrumentation/mysql2/instrumentation.rb | 2 +- .../pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb | 2 +- .../opentelemetry/instrumentation/trilogy/instrumentation.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/helpers/sql-obfuscation/README.md b/helpers/sql-obfuscation/README.md index 1e86bacdf..c518d1b08 100644 --- a/helpers/sql-obfuscation/README.md +++ b/helpers/sql-obfuscation/README.md @@ -26,7 +26,7 @@ end Make sure the `Instrumentation` class for your gem contains configuration options for: - `:obfuscation_limit`: the length at which the obfuscated SQL string will be truncated. - Example: `option :obfuscation_limit, default: 2000, validate: :integer` + Example: `option :obfuscation_limit, default: 16384, validate: :integer` If you want to add support for a new adapter, update the following constants to include keys for your adapter: diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index a09025102..a773d99ef 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -20,7 +20,7 @@ module Helpers # `option :db_statement, default: :include, validate: %I[omit include obfuscate]` # * `:obfuscation_limit` # Example: - # `option :obfuscation_limit, default: 2000, validate: :integer` + # `option :obfuscation_limit, default: 16384, validate: :integer` # # If you want to add support for a new adapter, update the following # constants to include keys for your adapter: diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb index 9a7b78ccb..8fe5569de 100644 --- a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb @@ -22,7 +22,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name] - option :obfuscation_limit, default: 2000, validate: :integer + option :obfuscation_limit, default: 16384, validate: :integer private diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb index 2eeb6ac66..d4b76db91 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb @@ -26,7 +26,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] - option :obfuscation_limit, default: 2000, validate: :integer + option :obfuscation_limit, default: 16384, validate: :integer private diff --git a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb index 684fbe660..88e706a1d 100644 --- a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb +++ b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb @@ -26,7 +26,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name] - option :obfuscation_limit, default: 2000, validate: :integer + option :obfuscation_limit, default: 16384, validate: :integer option :propagator, default: nil, validate: :string attr_reader :propagator From 15a3d105f5592415cc5515444a3115f700091cb6 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 10:29:29 -0700 Subject: [PATCH 4/7] fix: Revert obfuscation limit change --- helpers/sql-obfuscation/README.md | 2 +- .../lib/opentelemetry/helpers/sql_obfuscation.rb | 2 +- .../lib/opentelemetry/instrumentation/mysql2/instrumentation.rb | 2 +- .../pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb | 2 +- .../opentelemetry/instrumentation/trilogy/instrumentation.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/helpers/sql-obfuscation/README.md b/helpers/sql-obfuscation/README.md index c518d1b08..1e86bacdf 100644 --- a/helpers/sql-obfuscation/README.md +++ b/helpers/sql-obfuscation/README.md @@ -26,7 +26,7 @@ end Make sure the `Instrumentation` class for your gem contains configuration options for: - `:obfuscation_limit`: the length at which the obfuscated SQL string will be truncated. - Example: `option :obfuscation_limit, default: 16384, validate: :integer` + Example: `option :obfuscation_limit, default: 2000, validate: :integer` If you want to add support for a new adapter, update the following constants to include keys for your adapter: diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index a773d99ef..a09025102 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -20,7 +20,7 @@ module Helpers # `option :db_statement, default: :include, validate: %I[omit include obfuscate]` # * `:obfuscation_limit` # Example: - # `option :obfuscation_limit, default: 16384, validate: :integer` + # `option :obfuscation_limit, default: 2000, validate: :integer` # # If you want to add support for a new adapter, update the following # constants to include keys for your adapter: diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb index 8fe5569de..9a7b78ccb 100644 --- a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb @@ -22,7 +22,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name] - option :obfuscation_limit, default: 16384, validate: :integer + option :obfuscation_limit, default: 2000, validate: :integer private diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb index d4b76db91..2eeb6ac66 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/instrumentation.rb @@ -26,7 +26,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] - option :obfuscation_limit, default: 16384, validate: :integer + option :obfuscation_limit, default: 2000, validate: :integer private diff --git a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb index 88e706a1d..684fbe660 100644 --- a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb +++ b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb @@ -26,7 +26,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :peer_service, default: nil, validate: :string option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate] option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name] - option :obfuscation_limit, default: 16384, validate: :integer + option :obfuscation_limit, default: 2000, validate: :integer option :propagator, default: nil, validate: :string attr_reader :propagator From 6a13777bd85771c75f4009dfa5ff4fa621106c0a Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 11:13:15 -0700 Subject: [PATCH 5/7] fix: Guard clause for truncation Don't even set the regex adapter if the SQL's over the limit, just return the message and get out of there --- .../lib/opentelemetry/helpers/sql_obfuscation.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index a09025102..bd7967079 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -102,6 +102,8 @@ def generate_regex(dialect) # # @api public def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) + return "SQL truncated (> #{obfuscation_limit} characters)" if sql.size > obfuscation_limit + regex = case adapter when :mysql MYSQL_COMPONENTS_REGEX @@ -114,7 +116,6 @@ def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) # Original MySQL UTF-8 Encoding Fixes: # https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/160 # https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/345 - return "SQL truncated (> #{obfuscation_limit} characters)" if sql.size > obfuscation_limit sql = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true) sql = sql.gsub(regex, PLACEHOLDER) From e649508341185c645a737d9985a5f36ec79b6844 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 12:00:29 -0700 Subject: [PATCH 6/7] chore: Update tests and docs for sql limit message --- helpers/sql-obfuscation/README.md | 2 +- .../opentelemetry/helpers/sql_obfuscation.rb | 4 ++-- .../test/helpers/sql_obfuscation_test.rb | 21 ++----------------- .../mysql2/instrumentation_test.rb | 15 ++----------- .../pg/instrumentation_test.rb | 15 ++----------- .../trilogy/instrumentation_test.rb | 15 ++----------- 6 files changed, 11 insertions(+), 61 deletions(-) diff --git a/helpers/sql-obfuscation/README.md b/helpers/sql-obfuscation/README.md index 1e86bacdf..6055f8fe8 100644 --- a/helpers/sql-obfuscation/README.md +++ b/helpers/sql-obfuscation/README.md @@ -25,7 +25,7 @@ end Make sure the `Instrumentation` class for your gem contains configuration options for: -- `:obfuscation_limit`: the length at which the obfuscated SQL string will be truncated. +- `:obfuscation_limit`: the length at which the SQL string will not be obfuscated Example: `option :obfuscation_limit, default: 2000, validate: :integer` If you want to add support for a new adapter, update the following constants to include keys for your adapter: diff --git a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb index bd7967079..debb2b7e5 100644 --- a/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb +++ b/helpers/sql-obfuscation/lib/opentelemetry/helpers/sql_obfuscation.rb @@ -94,7 +94,7 @@ def generate_regex(dialect) # This is a SQL obfuscation utility intended for use in database adapter instrumentation. # # @param sql [String] The SQL to obfuscate. - # @param obfuscation_limit [optional Integer] The maximum length of an obfuscated sql statement. + # @param obfuscation_limit [optional Integer] the length at which the SQL string will not be obfuscated # @param adapter [optional Symbol] the type of database adapter calling the method. `:default`, `:mysql` and `:postgres` are supported. # @return [String] The SQL query string where the values are replaced with "?". When the sql statement exceeds the obufscation limit # the first matched pair from the SQL statement will be returned, with an appended truncation message. If trunaction is unsuccessful, @@ -102,7 +102,7 @@ def generate_regex(dialect) # # @api public def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) - return "SQL truncated (> #{obfuscation_limit} characters)" if sql.size > obfuscation_limit + return "SQL not obfuscated, query exceeds #{obfuscation_limit} characters" if sql.size > obfuscation_limit regex = case adapter when :mysql diff --git a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb index f38eed831..8aa5d633c 100644 --- a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb +++ b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb @@ -18,26 +18,9 @@ def test_named_arg_defaults_obfuscates assert_equal(expected, result) end - def test_obfuscation_limit_returns_truncation_message + def test_obfuscation_returns_message_when_limit_is_reached sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - expected = "SQL truncated (> 42 characters)" - result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) - - assert_equal(expected, result) - end - - def test_obfuscation_limit_returns_truncation_message_when_query_has_prepended_comment - comment = '/*service.name:foo,deployment.environtment:production,tracecontext:00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00,rails.route:examples/bars#index,host.name:baz-abc123.example.com*/' - sql = "#{comment} SELECT user.id FROM users where user.login = 'secretUserNameThatShouldBeObfuscated'" - expected = "SQL truncated (> 42 characters)" - result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) - - assert_equal(expected, result) - end - - def test_obfuscation_limit_returns_truncation_message_when_not_utf8 - sql = "SELECT * from 😄 where users.id = 1 and users.😄 = 'test@test.com'" - expected = "SQL truncated (> 42 characters)" + expected = "SQL not obfuscated, query exceeds 42 characters" result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) assert_equal(expected, result) diff --git a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb index 319e138d8..850d6d2c3 100644 --- a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb +++ b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb @@ -236,20 +236,9 @@ describe 'with obfuscation_limit' do let(:config) { { db_statement: :obfuscate, obfuscation_limit: 10 } } - it 'truncates SQL using config limit' do + it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SELECT * from users where users.id = ...\nSQL truncated (> 10 characters)" - expect do - client.query(sql) - end.must_raise Mysql2::Error - - _(span.attributes['db.statement']).must_equal obfuscated_sql - end - - it 'handles regex non-matches' do - sql = 'ALTER TABLE my_table DISABLE TRIGGER ALL;' - obfuscated_sql = 'SQL truncated (> 10 characters)' - + obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" expect do client.query(sql) end.must_raise Mysql2::Error diff --git a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb index 95f470014..91a42654b 100644 --- a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb +++ b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb @@ -297,20 +297,9 @@ describe 'with obfuscation_limit' do let(:config) { { db_statement: :obfuscate, obfuscation_limit: 10 } } - it 'truncates SQL using config limit' do + it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SELECT * from users where users.id = ...\nSQL truncated (> 10 characters)" - expect do - client.exec(sql) - end.must_raise PG::UndefinedTable - - _(span.attributes['db.statement']).must_equal obfuscated_sql - end - - it 'handles regex non-matches' do - sql = 'ALTER TABLE my_table DISABLE TRIGGER ALL;' - obfuscated_sql = 'SQL truncated (> 10 characters)' - + obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" expect do client.exec(sql) end.must_raise PG::UndefinedTable diff --git a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb index 9b1b7b845..572b3e3a2 100644 --- a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb +++ b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb @@ -323,20 +323,9 @@ describe 'with obfuscation_limit' do let(:config) { { db_statement: :obfuscate, obfuscation_limit: 10 } } - it 'truncates SQL using config limit' do + it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SELECT * from users where users.id = ...\nSQL truncated (> 10 characters)" - expect do - client.query(sql) - end.must_raise Trilogy::Error - - _(span.attributes['db.statement']).must_equal obfuscated_sql - end - - it 'handles regex non-matches' do - sql = 'ALTER TABLE my_table DISABLE TRIGGER ALL;' - obfuscated_sql = 'SQL truncated (> 10 characters)' - + obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" expect do client.query(sql) end.must_raise Trilogy::Error From 98bf708c174fe7816c34a78252a05be5c39d9c35 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 6 Sep 2024 12:12:11 -0700 Subject: [PATCH 7/7] style: Rubocop Style/StringLiterals --- helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb | 2 +- .../instrumentation/mysql2/instrumentation_test.rb | 2 +- .../opentelemetry/instrumentation/pg/instrumentation_test.rb | 2 +- .../instrumentation/trilogy/instrumentation_test.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb index 8aa5d633c..da6189b66 100644 --- a/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb +++ b/helpers/sql-obfuscation/test/helpers/sql_obfuscation_test.rb @@ -20,7 +20,7 @@ def test_named_arg_defaults_obfuscates def test_obfuscation_returns_message_when_limit_is_reached sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - expected = "SQL not obfuscated, query exceeds 42 characters" + expected = 'SQL not obfuscated, query exceeds 42 characters' result = OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: 42) assert_equal(expected, result) diff --git a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb index 850d6d2c3..405b714ee 100644 --- a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb +++ b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb @@ -238,7 +238,7 @@ it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" + obfuscated_sql = 'SQL not obfuscated, query exceeds 10 characters' expect do client.query(sql) end.must_raise Mysql2::Error diff --git a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb index 91a42654b..cc50728e5 100644 --- a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb +++ b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb @@ -299,7 +299,7 @@ it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" + obfuscated_sql = 'SQL not obfuscated, query exceeds 10 characters' expect do client.exec(sql) end.must_raise PG::UndefinedTable diff --git a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb index 572b3e3a2..350ee5f83 100644 --- a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb +++ b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb @@ -325,7 +325,7 @@ it 'returns a message when the limit is reached' do sql = "SELECT * from users where users.id = 1 and users.email = 'test@test.com'" - obfuscated_sql = "SQL not obfuscated, query exceeds 10 characters" + obfuscated_sql = 'SQL not obfuscated, query exceeds 10 characters' expect do client.query(sql) end.must_raise Trilogy::Error