Skip to content

Commit

Permalink
fix: adding web worker and ssr support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryhinchey committed Mar 11, 2021
1 parent 2f169a5 commit cf60088
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
* Gets the environment variables
*/
export function getEnv(): Required<ENVIRONMENT> {
const _window = window as typeof window & RAW_ENVIRONMENT;
const _window = (typeof window === 'undefined'
? self
: window) as typeof window & RAW_ENVIRONMENT;
const globalEnv = parseEnvironment(_window);
return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv);
}
16 changes: 13 additions & 3 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ import { VERSION } from './version';
// hard to say how long it should really wait, seems like 300ms is
// safe enough
const OBSERVER_WAIT_TIME_MS = 300;
const urlNormalizingA = document.createElement('a');
// Used to normalize relative URLs
const getUrlNormalizingAnchor = () => {
let a: HTMLAnchorElement | undefined;

if (!a) {
a = document.createElement('a');
}

return a;
};
/**
* FetchPlugin Config
*/
Expand Down Expand Up @@ -359,11 +368,12 @@ export class FetchInstrumentation extends InstrumentationBase<

const observer: PerformanceObserver = new PerformanceObserver(list => {
const perfObsEntries = list.getEntries() as PerformanceResourceTiming[];
urlNormalizingA.href = spanUrl;
const urlNormalizingAnchor = getUrlNormalizingAnchor();
urlNormalizingAnchor.href = spanUrl;
perfObsEntries.forEach(entry => {
if (
entry.initiatorType === 'fetch' &&
entry.name === urlNormalizingA.href
entry.name === urlNormalizingAnchor.href
) {
entries.push(entry);
}
Expand Down
15 changes: 12 additions & 3 deletions packages/opentelemetry-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ import {
import { HttpAttribute } from '@opentelemetry/semantic-conventions';

// Used to normalize relative URLs
const urlNormalizingA = document.createElement('a');
const getUrlNormalizingAnchor = () => {
let a: HTMLAnchorElement | undefined;

if (!a) {
a = document.createElement('a');
}

return a;
};

/**
* Helper function to be able to use enum as typed key in type and in interface when using forEach
Expand Down Expand Up @@ -125,8 +133,9 @@ export function getResource(
initiatorType?: string
): PerformanceResourceTimingInfo {
// de-relativize the URL before usage (does no harm to absolute URLs)
urlNormalizingA.href = spanUrl;
spanUrl = urlNormalizingA.href;
const urlNormalizingAnchor = getUrlNormalizingAnchor();
urlNormalizingAnchor.href = spanUrl;
spanUrl = urlNormalizingAnchor.href;

const filteredResources = filterResourcesForSpan(
spanUrl,
Expand Down

0 comments on commit cf60088

Please sign in to comment.