Skip to content

Commit

Permalink
fix(net.peer.port): net.peer.port needs to be a number not a string
Browse files Browse the repository at this point in the history
Signed-off-by: esara <endresara@gmail.com>
  • Loading branch information
esara committed Feb 14, 2024
1 parent cf034c8 commit 6c7e62e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
16 changes: 12 additions & 4 deletions plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 56 in plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-memcached/src/utils.ts#L56

Added line #L56 was not covered by tests
[SemanticAttributes.NET_PEER_NAME]: host,
};
}
}
return {};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
16 changes: 14 additions & 2 deletions plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6c7e62e

Please sign in to comment.