Skip to content

Commit

Permalink
fix(sdk-web): parse url with relative url string
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed May 16, 2022
1 parent 22085fc commit b1d8af0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import { FetchError, FetchResponse, SpanData } from './types';
import { VERSION } from './version';
import { _globalThis } from '@opentelemetry/core';

function parseUrl(url: string): web.URLLike {
const element = document.createElement('a');
element.href = url;
return element;
}

// how long to wait for observer to collect information about resources
// this is needed as event "load" is called before observer
// hard to say how long it should really wait, seems like 300ms is
Expand Down Expand Up @@ -126,7 +120,7 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
span: api.Span,
response: FetchResponse
): void {
const parsedUrl = parseUrl(response.url);
const parsedUrl = web.parseUrl(response.url);
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
if (response.statusText != null) {
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
Expand Down Expand Up @@ -301,7 +295,7 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
...args: Parameters<typeof fetch>
): Promise<Response> {
const self = this;
const url = parseUrl(args[0] instanceof Request ? args[0].url : args[0]).href;
const url = web.parseUrl(args[0] instanceof Request ? args[0].url : args[0]).href;

const options = args[0] instanceof Request ? args[0] : args[1] || {};
const createdSpan = plugin._createSpan(url, options);
Expand Down Expand Up @@ -437,17 +431,16 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
private _prepareSpanData(spanUrl: string): SpanData {
const startTime = core.hrTime();
const entries: PerformanceResourceTiming[] = [];
if (PerformanceObserver == null) {
if (typeof PerformanceObserver !== 'function') {
return { entries, startTime, spanUrl };
}

const observer: PerformanceObserver = new PerformanceObserver(list => {
const observer = new PerformanceObserver(list => {
const perfObsEntries = list.getEntries() as PerformanceResourceTiming[];
const parsedUrl = parseUrl(spanUrl);
perfObsEntries.forEach(entry => {
if (
entry.initiatorType === 'fetch' &&
entry.name === parsedUrl.href
entry.name === spanUrl
) {
entries.push(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
const xhrMem = this._xhrMem.get(xhr);
if (
!xhrMem ||
PerformanceObserver == null ||
PerformanceResourceTiming == null
typeof PerformanceObserver !== 'function' ||
typeof PerformanceResourceTiming !== 'function'
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export interface URLLike {
*/
export function parseUrl(url: string): URLLike {
if (typeof URL === 'function') {
return new URL(url);
return new URL(url, location.href);
}
const element = getUrlNormalizingAnchor();
element.href = url;
Expand Down
13 changes: 13 additions & 0 deletions packages/opentelemetry-sdk-trace-web/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,25 @@ describe('utils', () => {
assert.strictEqual(typeof url[field], 'string');
});
});

it('should parse relative url', () => {
const url = parseUrl('/foo');
urlFields.forEach(field => {
assert.strictEqual(typeof url[field], 'string');
});
});
});

describe('normalizeUrl', () => {
it('should normalize url', () => {
const url = normalizeUrl('https://opentelemetry.io/你好');
assert.strictEqual(url, 'https://opentelemetry.io/%E4%BD%A0%E5%A5%BD');
});

it('should normalize relative url', () => {
const url = normalizeUrl('/你好');
const urlObj = new URL(url);
assert.strictEqual(urlObj.pathname, '/%E4%BD%A0%E5%A5%BD');
});
});
});

0 comments on commit b1d8af0

Please sign in to comment.