Skip to content

Commit 5ed54c8

Browse files
Update fetch instrumentation to be runtime agnostic (#4063)
Co-authored-by: Marc Pichler <[email protected]>
1 parent 10f6c46 commit 5ed54c8

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
2020

2121
### :bug: (Bug Fix)
2222

23+
* fix(sdk-trace-web): only access location if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063)
2324
* fix(sdk-trace-base): processor onStart called with a span having empty attributes
2425

2526
## 1.18.1

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All notable changes to experimental packages in this project will be documented
1111
### :bug: (Bug Fix)
1212

1313
* fix(sdk-logs): avoid map attribute set when count limit exceeded
14+
* fix(instrumentation-fetch): only access navigator if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063)
15+
* allows for experimental usage of this instrumentation with non-browser runtimes
1416

1517
### :books: (Refine Doc)
1618

experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ export class FetchInstrumentation extends InstrumentationBase<
132132
SemanticAttributes.HTTP_SCHEME,
133133
parsedUrl.protocol.replace(':', '')
134134
);
135-
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);
135+
if (typeof navigator !== 'undefined') {
136+
span.setAttribute(
137+
SemanticAttributes.HTTP_USER_AGENT,
138+
navigator.userAgent
139+
);
140+
}
136141
}
137142

138143
/**

packages/opentelemetry-sdk-trace-web/src/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ export function sortResources(
131131
});
132132
}
133133

134+
/** Returns the origin if present (if in browser context). */
135+
function getOrigin(): string | undefined {
136+
return typeof location !== 'undefined' ? location.origin : undefined;
137+
}
138+
134139
/**
135140
* Get closest performance resource ignoring the resources that have been
136141
* already used.
@@ -174,7 +179,7 @@ export function getResource(
174179
}
175180
const sorted = sortResources(filteredResources);
176181

177-
if (parsedSpanUrl.origin !== location.origin && sorted.length > 1) {
182+
if (parsedSpanUrl.origin !== getOrigin() && sorted.length > 1) {
178183
let corsPreFlightRequest: PerformanceResourceTiming | undefined = sorted[0];
179184
let mainRequest: PerformanceResourceTiming = findMainRequest(
180185
sorted,
@@ -438,7 +443,7 @@ export function shouldPropagateTraceHeaders(
438443
}
439444
const parsedSpanUrl = parseUrl(spanUrl);
440445

441-
if (parsedSpanUrl.origin === location.origin) {
446+
if (parsedSpanUrl.origin === getOrigin()) {
442447
return true;
443448
} else {
444449
return propagateTraceHeaderUrls.some(propagateTraceHeaderUrl =>

0 commit comments

Comments
 (0)