Skip to content

Commit

Permalink
Allow fetch to be used with NodeJS native fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcorlin1 committed Aug 20, 2023
1 parent 853a7b6 commit f91b440
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 =>
Expand Down

0 comments on commit f91b440

Please sign in to comment.