From e229969c7f811e35f694a6295fbdae1811353b31 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Sun, 20 Aug 2023 13:09:06 -0400 Subject: [PATCH 1/7] Allow fetch to be used with NodeJS native fetch --- .../opentelemetry-instrumentation-fetch/src/fetch.ts | 4 +++- packages/opentelemetry-sdk-trace-web/src/utils.ts | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index d02269c440..a15406e6ec 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -132,7 +132,9 @@ export class FetchInstrumentation extends InstrumentationBase< SemanticAttributes.HTTP_SCHEME, parsedUrl.protocol.replace(':', '') ); - span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent); + if (typeof navigator !== 'undefined') { + span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent); + } } /** diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index b3f583676f..a69163dcf6 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -131,6 +131,11 @@ export function sortResources( }); } +/** Returns the origin if present (if in browser context) */ +function getOrigin(): string | undefined{ + return typeof location !== 'undefined' ? location.origin : undefined; +} + /** * Get closest performance resource ignoring the resources that have been * already used. @@ -174,7 +179,7 @@ export function getResource( } const sorted = sortResources(filteredResources); - if (parsedSpanUrl.origin !== location.origin && sorted.length > 1) { + if (parsedSpanUrl.origin !== getOrigin() && sorted.length > 1) { let corsPreFlightRequest: PerformanceResourceTiming | undefined = sorted[0]; let mainRequest: PerformanceResourceTiming = findMainRequest( sorted, @@ -438,7 +443,7 @@ export function shouldPropagateTraceHeaders( } const parsedSpanUrl = parseUrl(spanUrl); - if (parsedSpanUrl.origin === location.origin) { + if (parsedSpanUrl.origin === getOrigin()) { return true; } else { return propagateTraceHeaderUrls.some(propagateTraceHeaderUrl => From d24e132885963669cb86dfd1c9ea19d7e8fd4b14 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Sun, 20 Aug 2023 13:15:57 -0400 Subject: [PATCH 2/7] Allow fetch to be used with NodeJS native fetch --- .../opentelemetry-instrumentation-fetch/src/fetch.ts | 5 ++++- packages/opentelemetry-sdk-trace-web/src/utils.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index a15406e6ec..90213a8c69 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -133,7 +133,10 @@ export class FetchInstrumentation extends InstrumentationBase< parsedUrl.protocol.replace(':', '') ); if (typeof navigator !== 'undefined') { - span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent); + span.setAttribute( + SemanticAttributes.HTTP_USER_AGENT, + navigator.userAgent + ); } } diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index a69163dcf6..beb93572ca 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -131,8 +131,8 @@ export function sortResources( }); } -/** Returns the origin if present (if in browser context) */ -function getOrigin(): string | undefined{ +/** Returns the origin if present (if in browser context). */ +function getOrigin(): string | undefined { return typeof location !== 'undefined' ? location.origin : undefined; } From 4795cda296361c29b42aca4470a4026778dfade4 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Wed, 18 Oct 2023 08:57:21 -0400 Subject: [PATCH 3/7] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40b17374f7..e171953d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-trace): Allow fetch instrumentation to be used with native NodeJS fetch [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) + ### :books: (Refine Doc) ### :house: (Internal) From 850897c5284e3917c0a7abc97d5c1767bec438d9 Mon Sep 17 00:00:00 2001 From: drewcorlin1 <82601620+drewcorlin1@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:09:23 -0500 Subject: [PATCH 4/7] Update CHANGELOG.md Co-authored-by: Marc Pichler --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2406bb50c..0532d2f9cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) * chore: type reference on zone.js [#4257](https://github.com/open-telemetry/opentelemetry-js/pull/4257) @legendecas * chore: no need for 'packages' in lerna.json [#4264](https://github.com/open-telemetry/opentelemetry-js/pull/4264) @trentm -* fix(sdk-trace): Allow fetch instrumentation to be used with native NodeJS fetch [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) +* fix(sdk-trace-web): only access location if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) ## 1.18.1 From 15ca47fc5fdff7074d88872b199c49cd08b00ff2 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Mon, 13 Nov 2023 10:10:57 -0500 Subject: [PATCH 5/7] Update experimental changelog --- experimental/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index bb1fd6aca0..b9935e26c5 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -11,6 +11,8 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) * fix(sdk-logs): avoid map attribute set when count limit exceeded +* fix(instrumentation-fetch): only access navigator if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) + * allows for experimental usage of this instrumentation with non-browser runtimes ### :books: (Refine Doc) From 31fa4fca3e7349ffceed0c2d150a5dc15aa67447 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Mon, 13 Nov 2023 10:12:33 -0500 Subject: [PATCH 6/7] Fix changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0532d2f9cb..7a159d8021 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,10 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :house: (Internal) -### :bug: (Bug Fix) * chore: type reference on zone.js [#4257](https://github.com/open-telemetry/opentelemetry-js/pull/4257) @legendecas * chore: no need for 'packages' in lerna.json [#4264](https://github.com/open-telemetry/opentelemetry-js/pull/4264) @trentm + +### :bug: (Bug Fix) * fix(sdk-trace-web): only access location if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) ## 1.18.1 From b81bed4addf318929a0e916f1203f908533b8221 Mon Sep 17 00:00:00 2001 From: drewcorlin1 Date: Mon, 13 Nov 2023 10:12:58 -0500 Subject: [PATCH 7/7] Fix spacing --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a159d8021..0ca3380047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ * chore: no need for 'packages' in lerna.json [#4264](https://github.com/open-telemetry/opentelemetry-js/pull/4264) @trentm ### :bug: (Bug Fix) + * fix(sdk-trace-web): only access location if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) ## 1.18.1