lib/opentelemetry/instrumentation/trilogy/patches/client.rb in opentelemetry-instrumentation-trilogy-0.51.1 vs lib/opentelemetry/instrumentation/trilogy/patches/client.rb in opentelemetry-instrumentation-trilogy-0.52.0

- old
+ new

@@ -54,11 +54,11 @@ end def query(sql) tracer.in_span( database_span_name(sql), - attributes: client_attributes(sql), + attributes: client_attributes(sql).merge!(OpenTelemetry::Instrumentation::Trilogy.attributes), kind: :client ) do super(sql) end end @@ -85,14 +85,18 @@ def obfuscate_sql(sql) if sql.size > 2000 'SQL query too large to remove sensitive data ...' else - obfuscated = sql.gsub(FULL_SQL_REGEXP, '?') + obfuscated = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true) + obfuscated = obfuscated.gsub(FULL_SQL_REGEXP, '?') obfuscated = 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if detect_unmatched_pairs(obfuscated) obfuscated end + rescue StandardError => e + OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e) + 'OpenTelemetry error: failed to obfuscate sql' end def detect_unmatched_pairs(obfuscated) # We use this to check whether the query contains any quote characters # after obfuscation. If so, that's a good indication that the original @@ -100,24 +104,31 @@ # literals. In such a case, we'll replace the entire query with a # placeholder. %r{'|"|\/\*|\*\/}.match(obfuscated) end - def database_span_name(sql) - # Setting span name to the SQL query without obfuscation would - # result in PII + cardinality issues. - # First attempt to infer the statement type then fallback to - # current Otel approach {database.component_name}.{database_instance_name} - # https://github.com/open-telemetry/opentelemetry-python/blob/39fa078312e6f41c403aa8cad1868264011f7546/ext/opentelemetry-ext-dbapi/tests/test_dbapi_integration.py#L53 - # This creates span names like mysql.default, mysql.replica, postgresql.staging etc. + def database_span_name(sql) # rubocop:disable Metrics/CyclomaticComplexity + case config[:span_name] + when :statement_type + extract_statement_type(sql) + when :db_name + database_name + when :db_operation_and_name + op = OpenTelemetry::Instrumentation::Trilogy.attributes['db.operation'] + name = database_name + if op && name + "#{op} #{name}" + elsif op + op + elsif name + name + end + end || 'mysql' + end - statement_type = extract_statement_type(sql) - - return statement_type unless statement_type.nil? - - # fallback - 'mysql' + def database_name + connection_options[:database] end def net_peer_name if defined?(@connected_host) @connected_host @@ -138,9 +149,10 @@ def extract_statement_type(sql) QUERY_NAME_RE.match(sql) { |match| match[1].downcase } unless sql.nil? rescue StandardError => e OpenTelemetry.logger.error("Error extracting sql statement type: #{e.message}") + nil end end end end end