From 038e0bfda951055ce91724a3b4a3042a9f918700 Mon Sep 17 00:00:00 2001 From: Endre Sara Date: Mon, 19 Feb 2024 03:42:00 -0500 Subject: [PATCH] fix(net.peer.port): net.peer.port needs to be a number not a string (#1916) Signed-off-by: esara --- .../src/utils.ts | 16 ++++++++++++---- .../test/index.test.ts | 4 +++- .../src/instrumentation.ts | 9 +++++---- .../test/mongodb-v3.test.ts | 6 ++++-- .../test/mongodb-v4-v5-v6.metrics.test.ts | 2 +- .../test/mongodb-v4.test.ts | 2 +- .../test/mongodb-v5-v6.test.ts | 2 +- .../src/utils.ts | 16 ++++++++++++++-- .../src/utils.ts | 16 ++++++++++++++-- 9 files changed, 55 insertions(+), 18 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..bdff8e4279 100644 --- a/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts +++ b/plugins/node/opentelemetry-instrumentation-memcached/test/index.test.ts @@ -36,7 +36,9 @@ 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 + ? parseInt(process.env.OPENTELEMETRY_MEMCACHED_PORT) + : 27017, }; 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..938c00875a 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -49,7 +49,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { } const URL = `mongodb://${process.env.MONGODB_HOST || DEFAULT_MONGO_HOST}:${ - process.env.MONGODB_PORT || '27017' + process.env.MONGODB_PORT || 27017 }`; const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests'; const COLLECTION_NAME = 'test'; @@ -585,7 +585,9 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { ); assert.strictEqual( mongoSpan.attributes[SemanticAttributes.NET_PEER_PORT], - process.env.MONGODB_PORT || '27017' + process.env.MONGODB_PORT + ? parseInt(process.env.MONGODB_PORT) + : 27017 ); done(); } diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts index 189ff458d6..eb8d3b3946 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts @@ -76,7 +76,7 @@ describe('MongoDBInstrumentation-Metrics', () => { } const HOST = process.env.MONGODB_HOST || DEFAULT_MONGO_HOST; - const PORT = process.env.MONGODB_PORT || '27017'; + const PORT = process.env.MONGODB_PORT || 27017; const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-metrics'; const COLLECTION_NAME = 'test-metrics'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index 724e33cffd..7fb8dc4fca 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -51,7 +51,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { } const HOST = process.env.MONGODB_HOST || DEFAULT_MONGO_HOST; - const PORT = process.env.MONGODB_PORT || '27017'; + const PORT = process.env.MONGODB_PORT || 27017; const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-traces'; const COLLECTION_NAME = 'test-traces'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts index e9f80ad6c5..b264a44efe 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts @@ -60,7 +60,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { } const HOST = process.env.MONGODB_HOST || DEFAULT_MONGO_HOST; - const PORT = process.env.MONGODB_PORT || '27017'; + const PORT = process.env.MONGODB_PORT || 27017; const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-traces'; const COLLECTION_NAME = 'test-traces'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; 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,