lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.25.0 vs lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.25.1

- old
+ new

@@ -106,20 +106,27 @@ def obfuscate_sql(sql) return sql unless config[:db_statement] == :obfuscate if sql.size > config[:obfuscation_limit] - truncated_sql = sql[..sql.index(generated_postgres_regex) - 1] - return truncated_sql + "...\nSQL truncated (> #{config[:obfuscation_limit]} characters)" + first_match_index = sql.index(generated_postgres_regex) + truncation_message = "SQL truncated (> #{config[:obfuscation_limit]} characters)" + return truncation_message unless first_match_index + + truncated_sql = sql[..first_match_index - 1] + return "#{truncated_sql}...\n#{truncation_message}" end # From: # https://github.com/newrelic/newrelic-ruby-agent/blob/9787095d4b5b2d8fcaf2fdbd964ed07c731a8b6b/lib/new_relic/agent/database/obfuscator.rb # https://github.com/newrelic/newrelic-ruby-agent/blob/9787095d4b5b2d8fcaf2fdbd964ed07c731a8b6b/lib/new_relic/agent/database/obfuscation_helpers.rb obfuscated = sql.gsub(generated_postgres_regex, '?') obfuscated = 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if PG::Constants::UNMATCHED_PAIRS_REGEX.match(obfuscated) obfuscated + rescue StandardError => e + OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e) + 'OpenTelemetry error: failed to obfuscate sql' end def generated_postgres_regex @generated_postgres_regex ||= Regexp.union(PG::Constants::POSTGRES_COMPONENTS.map { |component| PG::Constants::COMPONENTS_REGEX_MAP[component] }) end