From b0026e1fb3c60bc8ee6d201f88093b1609265f2f Mon Sep 17 00:00:00 2001 From: Daniel Gomez Blanco Date: Tue, 3 May 2022 12:43:12 +0100 Subject: [PATCH] fix(opentelemetry-instrumentation-http): handle null port in getRequestInfo See the issue for details Closes issue #2947 --- experimental/CHANGELOG.md | 1 + .../opentelemetry-instrumentation-http/src/utils.ts | 3 ++- .../test/functionals/utils.test.ts | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4eed8e2217..914bb50635 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 68d7be7588..1722bc2503 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 062f160bdc..52211abadc 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);