Skip to content

Commit

Permalink
fix: handle string ports for Socket.connect (#1172)
Browse files Browse the repository at this point in the history
Co-authored-by: Rauno Viskus <[email protected]>
  • Loading branch information
seemk and rauno56 authored Sep 14, 2022
1 parent c1a8622 commit aa6a0dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit aa6a0dd

Please sign in to comment.