From b0026e1fb3c60bc8ee6d201f88093b1609265f2f Mon Sep 17 00:00:00 2001 From: Daniel Gomez Blanco Date: Tue, 3 May 2022 12:43:12 +0100 Subject: [PATCH 1/6] 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 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); From 3b8aa8e6362e3c42bf402c30c2baf65dd7e10483 Mon Sep 17 00:00:00 2001 From: Daniel Gomez Blanco Date: Tue, 3 May 2022 14:45:13 +0100 Subject: [PATCH 2/6] chore(experimental): Fix linting issues --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 914bb50635b..052eed8b4cd 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) From 1750d139d64c4bd9ac31c470f77e6544f7afaf61 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 11 May 2022 11:06:48 -0400 Subject: [PATCH 3/6] Apply suggestions from code review --- experimental/CHANGELOG.md | 2 +- .../packages/opentelemetry-instrumentation-http/src/utils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 052eed8b4cd..e8494a22086 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) -* fix(opentelemetry-instrumentation-http): handle null ports in options #2948 @danielgblanco +* fix(opentelemetry-instrumentation-http): use correct origin when port is `null` #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 1722bc25034..004253ed89e 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -265,7 +265,7 @@ export const getRequestInfo = ( } origin = `${optionsParsed.protocol || 'http:'}//${ optionsParsed.host || - `${optionsParsed.hostname}${optionsParsed.port ? `:${optionsParsed.port}` : ''}` + optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostName }`; } From 26d2d2a56729971072067bbbafc0f12df3e15f0f Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 11 May 2022 11:12:46 -0400 Subject: [PATCH 4/6] Update experimental/packages/opentelemetry-instrumentation-http/src/utils.ts --- .../packages/opentelemetry-instrumentation-http/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 004253ed89e..90469a8dfd7 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -265,7 +265,7 @@ export const getRequestInfo = ( } origin = `${optionsParsed.protocol || 'http:'}//${ optionsParsed.host || - optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostName + optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname }`; } From da0708447a9a2b364b5e5d7a4741b61f5d4bc717 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 11 May 2022 15:24:20 -0400 Subject: [PATCH 5/6] Readability --- .../opentelemetry-instrumentation-http/src/utils.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 90469a8dfd7..4c1f3ee85b8 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -263,10 +263,8 @@ export const getRequestInfo = ( if (!pathname && optionsParsed.path) { pathname = url.parse(optionsParsed.path).pathname || '/'; } - origin = `${optionsParsed.protocol || 'http:'}//${ - optionsParsed.host || - optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname - }`; + const hostname = optionsParsed.host || (optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname) + origin = `${optionsParsed.protocol || 'http:'}//${hostname}`; } const headers = optionsParsed.headers ?? {}; From 6bb46a9395e1d9ab9286a7e3d44f3b0ef96514f3 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 11 May 2022 15:39:03 -0400 Subject: [PATCH 6/6] Lint --- .../packages/opentelemetry-instrumentation-http/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 4c1f3ee85b8..e1e98209dba 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -263,7 +263,7 @@ export const getRequestInfo = ( if (!pathname && optionsParsed.path) { pathname = url.parse(optionsParsed.path).pathname || '/'; } - const hostname = optionsParsed.host || (optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname) + const hostname = optionsParsed.host || (optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname); origin = `${optionsParsed.protocol || 'http:'}//${hostname}`; }