diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4eed8e22176..914bb50635b 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) ### :bug: (Bug Fix) +* fix(opentelemetry-instrumentation-http): handle null ports in options #2948 @danielgblanco ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 68d7be75886..1722bc25034 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -264,7 +264,8 @@ export const getRequestInfo = ( pathname = url.parse(optionsParsed.path).pathname || '/'; } origin = `${optionsParsed.protocol || 'http:'}//${ - optionsParsed.host || `${optionsParsed.hostname}:${optionsParsed.port}` + optionsParsed.host || + `${optionsParsed.hostname}${optionsParsed.port ? `:${optionsParsed.port}` : ''}` }`; } diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index 062f160bdc5..52211abadc1 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -72,11 +72,23 @@ describe('Utility', () => { ...urlParsed, pathname: undefined, }; + const urlParsedWithUndefinedHostAndPort = { + ...urlParsed, + host: undefined, + port: undefined, + }; + const urlParsedWithUndefinedHostAndNullPort = { + ...urlParsed, + host: undefined, + port: null, + }; const whatWgUrl = new url.URL(webUrl); for (const param of [ webUrl, urlParsed, urlParsedWithoutPathname, + urlParsedWithUndefinedHostAndPort, + urlParsedWithUndefinedHostAndNullPort, whatWgUrl, ]) { const result = utils.getRequestInfo(param);