From c9b3671008816a0df4632b46839813fdf160bf53 Mon Sep 17 00:00:00 2001 From: Endre Sara Date: Sun, 28 Jan 2024 14:17:00 -0500 Subject: [PATCH] fix(net.peer.port): net.peer.port needs to be a number not a string Signed-off-by: esara --- .../src/utils.ts | 16 ++++++++++++---- .../test/index.test.ts | 2 +- .../src/instrumentation.ts | 9 +++++---- .../test/mongodb-v3.test.ts | 2 +- .../src/utils.ts | 16 ++++++++++++++-- .../src/utils.ts | 16 ++++++++++++++-- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts b/plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts index aa49f6c357..4cebd4112e 100644 --- a/plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts @@ -45,10 +45,18 @@ export const getPeerAttributes = ( if (typeof server === 'string') { const [host, port] = server && server.split(':'); - return { - [SemanticAttributes.NET_PEER_NAME]: host, - [SemanticAttributes.NET_PEER_PORT]: port, - }; + if (host && port) { + const portNumber = parseInt(port, 10); + if (!isNaN(portNumber)) { + return { + [SemanticAttributes.NET_PEER_NAME]: host, + [SemanticAttributes.NET_PEER_PORT]: portNumber, + }; + } + return { + [SemanticAttributes.NET_PEER_NAME]: host, + }; + } } return {}; }; diff --git a/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts b/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts index 01f116d0fc..7d4ef77723 100644 --- a/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts +++ b/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts @@ -36,7 +36,7 @@ const memoryExporter = new InMemorySpanExporter(); const CONFIG = { host: process.env.OPENTELEMETRY_MEMCACHED_HOST || 'localhost', - port: process.env.OPENTELEMETRY_MEMCACHED_PORT || '11211', + port: process.env.OPENTELEMETRY_MEMCACHED_PORT || 11211, }; const DEFAULT_ATTRIBUTES = { diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index 3be3019848..02af814bf2 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -813,10 +813,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { }); if (host && port) { - span.setAttributes({ - [SemanticAttributes.NET_PEER_NAME]: host, - [SemanticAttributes.NET_PEER_PORT]: port, - }); + span.setAttribute(SemanticAttributes.NET_PEER_NAME, host); + const portNumber = parseInt(port, 10); + if (!isNaN(portNumber)) { + span.setAttribute(SemanticAttributes.NET_PEER_PORT, portNumber); + } } if (!commandObj) return; const dbStatementSerializer = diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index a8f0444839..7b2bce8a97 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -585,7 +585,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { ); assert.strictEqual( mongoSpan.attributes[SemanticAttributes.NET_PEER_PORT], - process.env.MONGODB_PORT || '27017' + process.env.MONGODB_PORT || 27017 ); done(); } diff --git a/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts index 9eb7e90827..05eb1d809f 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts @@ -33,10 +33,22 @@ export function getConnectionAttributes( config: ConnectionConfig | PoolActualConfig ): SpanAttributes { const { host, port, database, user } = getConfig(config); - + const portNumber = parseInt(port, 10); + if (!isNaN(portNumber)) { + return { + [SemanticAttributes.NET_PEER_NAME]: host, + [SemanticAttributes.NET_PEER_PORT]: portNumber, + [SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString( + host, + port, + database + ), + [SemanticAttributes.DB_NAME]: database, + [SemanticAttributes.DB_USER]: user, + }; + } return { [SemanticAttributes.NET_PEER_NAME]: host, - [SemanticAttributes.NET_PEER_PORT]: port, [SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString( host, port, diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts index 3091569a40..c38f5ac683 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts @@ -48,10 +48,22 @@ interface Config { */ export function getConnectionAttributes(config: Config): SpanAttributes { const { host, port, database, user } = getConfig(config); - + const portNumber = parseInt(port, 10); + if (!isNaN(portNumber)) { + return { + [SemanticAttributes.NET_PEER_NAME]: host, + [SemanticAttributes.NET_PEER_PORT]: portNumber, + [SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString( + host, + port, + database + ), + [SemanticAttributes.DB_NAME]: database, + [SemanticAttributes.DB_USER]: user, + }; + } return { [SemanticAttributes.NET_PEER_NAME]: host, - [SemanticAttributes.NET_PEER_PORT]: port, [SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString( host, port,