From a457d941c20fab1a920c8eb43e47d741f02c0550 Mon Sep 17 00:00:00 2001 From: kobi-co <112798986+kobi-co@users.noreply.github.com> Date: Wed, 1 Mar 2023 23:38:13 +0200 Subject: [PATCH] fix(instrumentation-http): include query params in http.target (#3646) * fix: set http.target as path (like spec) and not pathname * fix: set http.target as path (like spec) and not pathname * fix: set http.target as path (like spec) and not pathname --- CHANGELOG.md | 1 + .../src/utils.ts | 2 +- .../test/functionals/utils.test.ts | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 607cb9544db..a5014e77373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) * fix(core): added falsy check to make otel core work with browser where webpack config had process as false or null [#3613](https://github.com/open-telemetry/opentelemetry-js/issues/3613) @ravindra-dyte +* fix(instrumentation-http): include query params in http.target [#3646](https://github.com/open-telemetry/opentelemetry-js/pull/3646) @kobi-co ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 7801a7626c8..a563e294715 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -484,7 +484,7 @@ export const getIncomingRequestAttributes = ( } if (requestUrl) { - attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.pathname || '/'; + attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.path || '/'; } if (userAgent !== undefined) { 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 700498cfb66..35b3c1ab5bc 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -503,6 +503,23 @@ describe('Utility', () => { }); assert.strictEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined); }); + + it('should set http.target as path in http span attributes', () => { + const request = { + url: 'http://hostname/user/?q=val', + method: 'GET', + } as IncomingMessage; + request.headers = { + 'user-agent': 'chrome', + }; + const attributes = utils.getIncomingRequestAttributes(request, { + component: 'http', + }); + assert.strictEqual( + attributes[SemanticAttributes.HTTP_TARGET], + '/user/?q=val' + ); + }); }); describe('headers to span attributes capture', () => {