diff --git a/CHANGELOG.md b/CHANGELOG.md index bb156b4016..336e211d6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-trace-web): only access location if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) * fix(sdk-trace-base): processor onStart called with a span having empty attributes ## 1.18.1 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) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index d02269c440..90213a8c69 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -132,7 +132,12 @@ 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..beb93572ca 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 =>