Skip to content

Commit b50f837

Browse files
ryhincheyvmarchaud
andauthored
Server side rendering support (#2010)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent e5fedbd commit b50f837

File tree

2 files changed

+25
-6
lines changed
  • packages

2 files changed

+25
-6
lines changed

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ import { VERSION } from './version';
3232
// hard to say how long it should really wait, seems like 300ms is
3333
// safe enough
3434
const OBSERVER_WAIT_TIME_MS = 300;
35-
const urlNormalizingA = document.createElement('a');
35+
36+
// Used to normalize relative URLs
37+
let a: HTMLAnchorElement | undefined;
38+
const getUrlNormalizingAnchor = () => {
39+
if (!a) {
40+
a = document.createElement('a');
41+
}
42+
43+
return a;
44+
};
45+
3646
/**
3747
* FetchPlugin Config
3848
*/
@@ -359,11 +369,12 @@ export class FetchInstrumentation extends InstrumentationBase<
359369

360370
const observer: PerformanceObserver = new PerformanceObserver(list => {
361371
const perfObsEntries = list.getEntries() as PerformanceResourceTiming[];
362-
urlNormalizingA.href = spanUrl;
372+
const urlNormalizingAnchor = getUrlNormalizingAnchor();
373+
urlNormalizingAnchor.href = spanUrl;
363374
perfObsEntries.forEach(entry => {
364375
if (
365376
entry.initiatorType === 'fetch' &&
366-
entry.name === urlNormalizingA.href
377+
entry.name === urlNormalizingAnchor.href
367378
) {
368379
entries.push(entry);
369380
}

packages/opentelemetry-web/src/utils.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ import {
2929
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
3030

3131
// Used to normalize relative URLs
32-
const urlNormalizingA = document.createElement('a');
32+
let a: HTMLAnchorElement | undefined;
33+
const getUrlNormalizingAnchor = () => {
34+
if (!a) {
35+
a = document.createElement('a');
36+
}
37+
38+
return a;
39+
};
3340

3441
/**
3542
* Helper function to be able to use enum as typed key in type and in interface when using forEach
@@ -125,8 +132,9 @@ export function getResource(
125132
initiatorType?: string
126133
): PerformanceResourceTimingInfo {
127134
// de-relativize the URL before usage (does no harm to absolute URLs)
128-
urlNormalizingA.href = spanUrl;
129-
spanUrl = urlNormalizingA.href;
135+
const urlNormalizingAnchor = getUrlNormalizingAnchor();
136+
urlNormalizingAnchor.href = spanUrl;
137+
spanUrl = urlNormalizingAnchor.href;
130138

131139
const filteredResources = filterResourcesForSpan(
132140
spanUrl,

0 commit comments

Comments
 (0)