lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.26.1 vs lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.27.0
- old
+ new
@@ -48,10 +48,20 @@
end
end
private
+ def obfuscate_sql(sql)
+ return sql unless config[:db_statement] == :obfuscate
+
+ OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(
+ sql,
+ obfuscation_limit: config[:obfuscation_limit],
+ adapter: :postgres
+ )
+ end
+
def tracer
PG::Instrumentation.instance.tracer
end
def config
@@ -108,37 +118,9 @@
[validated_operation(operation), db].compact.join(' ')
end
def validated_operation(operation)
operation if PG::Constants::SQL_COMMANDS.include?(operation)
- end
-
- def obfuscate_sql(sql)
- return sql unless config[:db_statement] == :obfuscate
-
- if sql.size > config[:obfuscation_limit]
- 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
def client_attributes
attributes = {
'db.system' => 'postgresql',