Skip to content

Commit

Permalink
fix: Send net.peer.port as an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Nov 19, 2022
1 parent 5590972 commit 9c9ddd3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c9ddd3

Please sign in to comment.