lib/opentelemetry/helpers/sql_obfuscation.rb in opentelemetry-helpers-sql-obfuscation-0.1.1 vs lib/opentelemetry/helpers/sql_obfuscation.rb in opentelemetry-helpers-sql-obfuscation-0.2.0

- old
+ new

@@ -92,18 +92,20 @@ ORACLE_COMPONENTS_REGEX = generate_regex(:oracle) # 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, # a string describing the error will be returned. # # @api public def obfuscate_sql(sql, obfuscation_limit: 2000, adapter: :default) + return "SQL not obfuscated, query exceeds #{obfuscation_limit} characters" if sql.size > obfuscation_limit + regex = case adapter when :mysql MYSQL_COMPONENTS_REGEX when :postgres POSTGRES_COMPONENTS_REGEX @@ -113,27 +115,16 @@ # 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 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) sql rescue StandardError => e OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e) - end - - # @api private - def truncate_statement(sql, regex, limit) - 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