Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit a276a58

Browse files
committed
Some update on documentation
1 parent 274d14c commit a276a58

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

packages/opencensus-web-instrumentation-zone/src/perf-resource-timing-selector.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import { alreadyAssignedPerfEntries } from './xhr-interceptor';
2626
* In overall, the algorithm to get this data takes the best fit for the span,
2727
* this means the closest performance resource timing to the span start/end
2828
* performance times is the returned value.
29-
* @param xhrUrl
30-
* @param span
3129
*/
3230
export function getXhrPerfomanceData(
3331
xhrUrl: string,
@@ -50,9 +48,8 @@ export function getXhrPerfomanceData(
5048
* Those are possible because there might be more than two entries that pass the
5149
* filter.
5250
* Additionally, the returned array is sorted by the entries' `startTime` as
53-
* getEntriesByType() already does it (https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType#Return_Value).
54-
* @param xhrUrl
55-
* @param span
51+
* getEntriesByType() already does it.
52+
* (https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType#Return_Value).
5653
*/
5754
export function getPerfSortedResourceEntries(
5855
xhrUrl: string,
@@ -93,7 +90,7 @@ export function getPossiblePerfResourceEntries(
9390
possiblePerfEntries.push({ mainRequest: entryI });
9491
// Compare every performance entry with the perfomance entries in front of
9592
// it. This is possible as the entries are sorted by the startTime. That
96-
// way we to avoid comparing twice the entries and taking the wrong order.
93+
// way, we avoid comparing twice the entries and taking the wrong order.
9794
for (let j = i + 1; j < filteredSortedPerfEntries.length; j++) {
9895
const entryJ = filteredSortedPerfEntries[j];
9996
if (isPossibleCorsPair(entryI, entryJ)) {
@@ -115,9 +112,6 @@ export function getPossiblePerfResourceEntries(
115112
* be the one with the minimum gap to the span start/end timings.
116113
* The performance resource timing entry with the minimum gap to the span
117114
* start/end timings points out that entry is the best fit for the span.
118-
*
119-
* @param perfEntries
120-
* @param span
121115
*/
122116
function getBestPerfResourceTiming(
123117
perfEntries: XhrPerformanceResourceTiming[],
@@ -169,15 +163,12 @@ function isPerfEntryPartOfXhr(
169163
* start/end times.
170164
*/
171165
function isPossibleCorsPair(
172-
entry1: PerformanceResourceTiming,
173-
entry2: PerformanceResourceTiming
166+
maybePreflight: PerformanceResourceTiming,
167+
maybeMainRequest: PerformanceResourceTiming
174168
): boolean {
175-
// To determine if the entries overlap, the minimum responseEnd should be
176-
// less than the maximum startTime (e.g. entry1 with startTime = 1 and
177-
// responseEnd = 3 and entry2 with startTime = 2 and responseEnd = 4, the
178-
// minimum is 3 and the maximum is 2, this tells that the intervals overlap).
179-
return (
180-
Math.min(entry1.responseEnd, entry2.responseEnd) <
181-
Math.max(entry1.startTime, entry2.startTime)
182-
);
169+
// We can be sure that `maybePreflight` startTime is less than
170+
// `maybeMainRequest` startTime because of the sorting done by
171+
// `getEntriesByType`. Thus, to check the timings do not overlap, the
172+
// maybePreflight.respondeEnd must be less than maybeMainRequest.startTime.
173+
return maybePreflight.responseEnd < maybeMainRequest.startTime;
183174
}

packages/opencensus-web-instrumentation-zone/src/xhr-interceptor.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ import {
3535

3636
const TRACEPARENT_HEADER = 'traceparent';
3737

38-
// Map intended to keep track of current XHR objects
39-
// associated to a span.
38+
/**
39+
* Map intended to keep track of current XHR objects associated to a span.
40+
*/
4041
const xhrSpans = new Map<XhrWithUrl, Span>();
4142

4243
// Keeps track of the current xhr tasks that are running. This is
4344
// useful to clear the Performance Resource Timing entries when no
4445
// xhr tasks are being intercepted.
4546
let xhrTasksCount = 0;
4647

47-
// Set to keep track of already assigned performance resource entries to a span.
48-
// This is done as there might be some edge cases where the result might include
49-
// some already assigned entries.
48+
/**
49+
* Set to keep track of already assigned performance resource entries to a span.
50+
* This is done as there might be some edge cases where the result might include
51+
* some already assigned entries.
52+
*/
5053
export const alreadyAssignedPerfEntries = new Set<PerformanceResourceTiming>();
5154

5255
/**
@@ -55,7 +58,6 @@ export const alreadyAssignedPerfEntries = new Set<PerformanceResourceTiming>();
5558
* In case the task is intercepted, sets the Trace Context Header to it and
5659
* creates a child span related to this XHR in case it is OPENED.
5760
* In case the XHR is DONE, end the child span.
58-
* @param task
5961
*/
6062
export function interceptXhrTask(task: AsyncTask) {
6163
if (!isTrackedTask(task)) return;
@@ -109,9 +111,11 @@ function endXhrSpan(xhr: XhrWithUrl): void {
109111
}
110112
}
111113

112-
// If xhr task count is 0, clear the Performance Resource Timings.
113-
// This is done in order to help the browser Performance resource timings
114-
// selector algorithm to take only the data related to the current XHRs running.
114+
/**
115+
* If xhr task count is 0, clear the Performance Resource Timings.
116+
* This is done in order to help the browser Performance resource timings
117+
* selector algorithm to take only the data related to the current XHRs running.
118+
*/
115119
function maybeClearPerfResourceBuffer(): void {
116120
if (xhrTasksCount === 0) {
117121
performance.clearResourceTimings();

0 commit comments

Comments
 (0)