From 53a5b26b471e692d7e85625c0f964510e4deef50 Mon Sep 17 00:00:00 2001 From: Ariel Valentin Date: Sat, 14 Jan 2023 12:33:23 -0600 Subject: [PATCH] fix: Reduce Hash Allocations in PG Instrumentation (#232) * chore: enable Style/CollectionCompact * chore: Minimize object allocation This change removes allocation of 2 additional hashes per PG SQL span. --- .rubocop.yml | 2 -- .../opentelemetry/instrumentation/pg/patches/connection.rb | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0ea9849b4..bdb1fde70 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -55,5 +55,3 @@ Layout/LineLength: Max: 250 Lint/RedundantRequireStatement: Enabled: false -Style/CollectionCompact: - Enabled: false diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 532ed3b7f..9de6c70c2 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb @@ -86,7 +86,7 @@ def span_attrs(kind, *args) attrs = { 'db.operation' => validated_operation(operation), 'db.postgresql.prepared_statement_name' => statement_name } attrs['db.statement'] = sql unless config[:db_statement] == :omit attrs.merge!(OpenTelemetry::Instrumentation::PG.attributes) - attrs.reject! { |_, v| v.nil? } + attrs.compact! [span_name(operation), client_attributes.merge(attrs)] end @@ -131,7 +131,9 @@ def client_attributes } attributes['peer.service'] = config[:peer_service] if config[:peer_service] - attributes.merge(transport_attrs).reject { |_, v| v.nil? } + attributes.merge!(transport_attrs) + attributes.compact! + attributes end def transport_addr