From 9c9ddd3aa842c231e3dd8fe15f37fae5156df6ee Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Sat, 19 Nov 2022 08:19:24 -0600 Subject: [PATCH] fix: Send net.peer.port as an integer See: https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions --- .../instrumentation/pg/patches/connection.rb | 4 +-- .../pg/instrumentation_test.rb | 26 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb index 900148471..e98f242f5 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg/patches/connection.rb @@ -166,12 +166,12 @@ def transport_attrs def transport_port # The port method can fail in older versions of the gem. It is # accurate and safe to use when the DEF_PGPORT constant is defined. - return port.to_s if defined?(::PG::DEF_PGPORT) + return port if defined?(::PG::DEF_PGPORT) # As a fallback, we can use the port of the parsed connection # string when there is exactly one. p = conninfo_hash[:port] - return p unless p.nil? || p.empty? || p.include?(',') + return p.to_i unless p.nil? || p.empty? || p.include?(',') end end end diff --git a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb index d0c7c3b6d..81a9f3fb8 100644 --- a/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb +++ b/instrumentation/pg/test/opentelemetry/instrumentation/pg/instrumentation_test.rb @@ -114,7 +114,7 @@ _(span.attributes['db.statement']).must_equal 'SELECT 1' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end end @@ -128,7 +128,7 @@ _(span.attributes['db.statement']).must_equal 'SELECT $1 AS a' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end end @@ -143,7 +143,7 @@ _(span.attributes['db.operation']).must_equal 'PREPARE' _(span.attributes['db.postgresql.prepared_statement_name']).must_equal 'foo' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end end @@ -159,7 +159,7 @@ _(last_span.attributes['db.statement']).must_equal 'SELECT $1 AS a' _(last_span.attributes['db.postgresql.prepared_statement_name']).must_equal 'foo' _(last_span.attributes['net.peer.name']).must_equal host.to_s - _(last_span.attributes['net.peer.port']).must_equal port.to_s + _(last_span.attributes['net.peer.port']).must_equal port.to_i end end @@ -173,7 +173,7 @@ _(span.attributes['db.statement']).must_equal 'SELECT 1' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end end @@ -189,7 +189,7 @@ _(last_span.attributes['db.statement']).must_be_nil _(last_span.attributes['db.postgresql.prepared_statement_name']).must_equal 'foo0' _(last_span.attributes['net.peer.name']).must_equal host.to_s - _(last_span.attributes['net.peer.port']).must_equal port.to_s + _(last_span.attributes['net.peer.port']).must_equal port.to_i end it 'after error' do @@ -203,7 +203,7 @@ _(span.attributes['db.statement']).must_equal 'SELECT INVALID' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i _(span.status.code).must_equal( OpenTelemetry::Trace::Status::ERROR @@ -226,7 +226,7 @@ _(span.attributes['db.statement']).must_equal explain_sql _(span.attributes['db.operation']).must_equal 'EXPLAIN' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end it 'uses database name as span.name fallback with invalid sql' do @@ -240,7 +240,7 @@ _(span.attributes['db.statement']).must_equal 'DESELECT 1' _(span.attributes['db.operation']).must_be_nil _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i _(span.status.code).must_equal( OpenTelemetry::Trace::Status::ERROR @@ -267,7 +267,7 @@ _(span.attributes['db.statement']).must_equal obfuscated_sql _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i end end @@ -285,7 +285,7 @@ _(span.name).must_equal 'SELECT postgres' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i _(span.attributes['db.statement']).must_be_nil end @@ -305,7 +305,7 @@ _(span.name).must_equal 'SELECT postgres' _(span.attributes['db.operation']).must_equal 'SELECT' _(span.attributes['net.peer.name']).must_equal host.to_s - _(span.attributes['net.peer.port']).must_equal port.to_s + _(span.attributes['net.peer.port']).must_equal port.to_i _(span.attributes['db.statement']).must_be_nil end @@ -341,7 +341,7 @@ _(span.attributes['net.peer.name']).must_equal host if ::PG.const_defined?('DEF_PORT') - _(span.attributes['net.peer.port']).must_equal port + _(span.attributes['net.peer.port']).must_equal port.to_i end end end