Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net.peer.port): net.peer.port needs to be a number not a string #1916

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@

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
Loading