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): handle string ports for Socket.connect #1172

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions plugins/node/opentelemetry-instrumentation-net/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { platform } from 'os';
export const IPC_TRANSPORT =
platform() === 'win32' ? NetTransportValues.PIPE : NetTransportValues.UNIX;

function getHost(args: unknown[]) {
return typeof args[1] === 'string' ? args[1] : 'localhost';
}

export function getNormalizedArgs(
args: unknown[]
): NormalizedOptions | null | undefined {
Expand All @@ -33,17 +37,26 @@ export function getNormalizedArgs(
case 'number':
return {
port: opt,
host: typeof args[1] === 'string' ? args[1] : 'localhost',
host: getHost(args),
};
case 'object':
if (Array.isArray(opt)) {
return getNormalizedArgs(opt);
}
return opt;
case 'string':
case 'string': {
const maybePort = Number(opt);
if (maybePort >= 0) {
return {
port: maybePort,
host: getHost(args),
};
}

return {
path: opt,
};
}
default:
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ describe('NetInstrumentation', () => {
});
});

it('should create a tcp span when port is given as string', done => {
socket = socket.connect(String(PORT) as unknown as number, HOST, () => {
assertTcpSpan(getSpan(), socket);
done();
});
});

it('should produce a span given options', done => {
socket.connect(
{
Expand Down