lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.27.4 vs lib/opentelemetry/instrumentation/pg/patches/connection.rb in opentelemetry-instrumentation-pg-0.28.0

- old
+ new

@@ -12,10 +12,13 @@ module Instrumentation 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) tracer.in_span(span_name, attributes: attrs, kind: :client) do if block @@ -84,15 +87,17 @@ # Rubocop is complaining about 19.31/18 for Metrics/AbcSize. # But, getting that metric in line would force us over the # module size limit! We can't win here unless we want to start # abstracting things into a million pieces. def span_attrs(kind, *args) + text = args[0] + if kind == :query - operation = extract_operation(args[0]) - sql = obfuscate_sql(args[0]).to_s + operation = extract_operation(text) + sql = obfuscate_sql(text).to_s else - statement_name = args[0] + statement_name = text if kind == :prepare sql = obfuscate_sql(args[1]).to_s lru_cache[statement_name] = sql operation = 'PREPARE' @@ -102,10 +107,11 @@ end end attrs = { 'db.operation' => validated_operation(operation), 'db.postgresql.prepared_statement_name' => statement_name } attrs['db.statement'] = sql unless config[:db_statement] == :omit + attrs['db.collection.name'] = collection_name(text) attrs.merge!(OpenTelemetry::Instrumentation::PG.attributes) attrs.compact! [span_name(operation), client_attributes.merge(attrs)] end @@ -121,9 +127,15 @@ [validated_operation(operation), db].compact.join(' ') end def validated_operation(operation) operation if PG::Constants::SQL_COMMANDS.include?(operation) + end + + def collection_name(text) + text.scan(TABLE_NAME).flatten[0] + rescue StandardError + nil end def client_attributes attributes = { 'db.system' => 'postgresql',