Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side rendering support #2010

Merged
merged 4 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 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,17 @@ 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
let a: HTMLAnchorElement | undefined;
const getUrlNormalizingAnchor = () => {
if (!a) {
a = document.createElement('a');
}

return a;
};

/**
* FetchPlugin Config
*/
Expand Down Expand Up @@ -359,11 +369,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
14 changes: 11 additions & 3 deletions packages/opentelemetry-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ import {
import { HttpAttribute } from '@opentelemetry/semantic-conventions';

// Used to normalize relative URLs
const urlNormalizingA = document.createElement('a');
let a: HTMLAnchorElement | undefined;
const getUrlNormalizingAnchor = () => {
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 +132,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