From 3b2090b5f53ee56e8d06ef7c144588141080804c Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 8 Mar 2024 19:35:04 +0100 Subject: [PATCH] fix(instrumentation-pg): prevent net.peer.port from being NaN (#1982) * fix(instrumentation-pg): prevent net.peer.port from being NaN * fix: lint * fix: test name * Update plugins/node/opentelemetry-instrumentation-pg/src/utils.ts --- .../src/utils.ts | 13 +++++- .../test/utils.test.ts | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index d97f7f9c3e..bcf8e4efb7 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -94,6 +94,17 @@ export function getConnectionString(params: PgParsedConnectionParams) { return `postgresql://${host}:${port}/${database}`; } +function getPort(port: number | undefined): number | undefined { + // Port may be NaN as parseInt() is used on the value, passing null will result in NaN being parsed. + // https://github.com/brianc/node-postgres/blob/2a8efbee09a284be12748ed3962bc9b816965e36/packages/pg/lib/connection-parameters.js#L66 + if (Number.isInteger(port)) { + return port; + } + + // Unable to find the default used in pg code, so falling back to 'undefined'. + return undefined; +} + export function getSemanticAttributesFromConnection( params: PgParsedConnectionParams ) { @@ -101,7 +112,7 @@ export function getSemanticAttributesFromConnection( [SemanticAttributes.DB_NAME]: params.database, // required [SemanticAttributes.DB_CONNECTION_STRING]: getConnectionString(params), // required [SemanticAttributes.NET_PEER_NAME]: params.host, // required - [SemanticAttributes.NET_PEER_PORT]: params.port, + [SemanticAttributes.NET_PEER_PORT]: getPort(params.port), [SemanticAttributes.DB_USER]: params.user, }; } diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts index ec32b55694..d3dca9aa3c 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/utils.test.ts @@ -28,6 +28,7 @@ import { PgInstrumentationConfig } from '../src'; import { AttributeNames } from '../src/enums/AttributeNames'; import { PgClientExtended } from '../src/internal-types'; import * as utils from '../src/utils'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; const memoryExporter = new InMemorySpanExporter(); @@ -193,4 +194,48 @@ describe('utils.ts', () => { assert.deepStrictEqual(pgValues, ['0']); }); }); + + describe('.getSemanticAttributesFromConnection()', () => { + it('should set port attribute to undefined when port is not an integer', () => { + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: Infinity, + })[SemanticAttributes.NET_PEER_PORT], + undefined + ); + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: -Infinity, + })[SemanticAttributes.NET_PEER_PORT], + undefined + ); + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: NaN, + })[SemanticAttributes.NET_PEER_PORT], + undefined + ); + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: 1.234, + })[SemanticAttributes.NET_PEER_PORT], + undefined + ); + }); + + it('should set port attribute when port is an integer', () => { + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: 1234, + })[SemanticAttributes.NET_PEER_PORT], + 1234 + ); + assert.strictEqual( + utils.getSemanticAttributesFromConnection({ + port: Number.MAX_VALUE, + })[SemanticAttributes.NET_PEER_PORT], + Number.MAX_VALUE + ); + }); + }); });