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

Commit 274d14c

Browse files
committed
Refactoring
1 parent 5c3bf64 commit 274d14c

2 files changed

Lines changed: 49 additions & 31 deletions

File tree

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,20 @@ export function getXhrPerfomanceData(
3737
const possibleEntries = getPossiblePerfResourceEntries(
3838
filteredSortedPerfEntries
3939
);
40-
const bestEntry = getBestPerfResourceTiming(possibleEntries, span);
41-
return bestEntry;
40+
return getBestPerfResourceTiming(possibleEntries, span);
4241
}
4342

4443
/**
4544
* First step for the algorithm. Filter the Performance Resource Timings by the
4645
* name (it should match the XHR URL), additionally, the start/end timings of
4746
* every performance entry should fit within the span start/end timings. Also,
48-
* the entry should be already assigned to a span.
47+
* the entry should not be already assigned to a span.
4948
* These filtered performance resource entries are considered as possible
5049
* entries associated to the xhr.
5150
* Those are possible because there might be more than two entries that pass the
5251
* filter.
5352
* Additionally, the returned array is sorted by the entries' `startTime` as
54-
* getEntriesByType() already does it.
53+
* getEntriesByType() already does it (https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType#Return_Value).
5554
* @param xhrUrl
5655
* @param span
5756
*/
@@ -71,9 +70,8 @@ export function getPerfSortedResourceEntries(
7170
* As the XHR could cause a CORS pre-flight request, we have to look for
7271
* possible performance entries either containing cors preflight timings or not.
7372
* A possible entry with cors data is when a resource timing entry does not
74-
* overlap timings with other resource timing entry. Also, a possible entry
75-
* without cors resource timing is when that resource timing entry is not
76-
* 'paired' with any other entry.
73+
* overlap timings with other resource timing entry. Also, every entry is
74+
* considered as possible XHR performance entry.
7775
* Thus, for this step traverse the array of resource entries and for every
7876
* entry check if it is a possible performance resource entry.
7977
* @param filteredSortedPerfEntries Sorted array of Performance Resource
@@ -83,9 +81,10 @@ export function getPossiblePerfResourceEntries(
8381
filteredSortedPerfEntries: PerformanceResourceTiming[]
8482
): XhrPerformanceResourceTiming[] {
8583
const possiblePerfEntries = new Array<XhrPerformanceResourceTiming>();
86-
// As this part of the algorithm traverses the array twice, although,
87-
// this array is not large as the performance resource entries buffer is
88-
// cleared when there are no more running XHRs.
84+
// As this part of the algorithm uses nested for loops to examine pairs of
85+
// entries, although, this array is not large as the performance resource
86+
// entries buffer is cleared when there are no more running XHRs. Also, this
87+
// data is already filtered by URL and start/end time to be within the span.
8988
for (let i = 0; i < filteredSortedPerfEntries.length; i++) {
9089
const entryI = filteredSortedPerfEntries[i];
9190
// Consider the current entry as a possible entry without cors preflight
@@ -97,7 +96,7 @@ export function getPossiblePerfResourceEntries(
9796
// way we to avoid comparing twice the entries and taking the wrong order.
9897
for (let j = i + 1; j < filteredSortedPerfEntries.length; j++) {
9998
const entryJ = filteredSortedPerfEntries[j];
100-
if (!isPossibleCorsPair(entryI, entryJ)) {
99+
if (isPossibleCorsPair(entryI, entryJ)) {
101100
// As the entries are not overlapping, that means those timings
102101
// are possible perfomance timings related to the XHR.
103102
possiblePerfEntries.push({
@@ -110,17 +109,22 @@ export function getPossiblePerfResourceEntries(
110109
return possiblePerfEntries;
111110
}
112111

113-
// Pick the best performance resource timing for the XHR: Using the possible
114-
// performance resource timing entries from previous step, the best entry will
115-
// be the one with the minimum gap to the span start/end timings.
116-
// The performance resource timing entry with the minimum gap to the span
117-
// start/end timings points out that entry is the best fit for the span.
112+
/**
113+
* Pick the best performance resource timing for the XHR: Using the possible
114+
* performance resource timing entries from previous step, the best entry will
115+
* be the one with the minimum gap to the span start/end timings.
116+
* The performance resource timing entry with the minimum gap to the span
117+
* start/end timings points out that entry is the best fit for the span.
118+
*
119+
* @param perfEntries
120+
* @param span
121+
*/
118122
function getBestPerfResourceTiming(
119123
perfEntries: XhrPerformanceResourceTiming[],
120124
span: Span
121125
): XhrPerformanceResourceTiming | undefined {
122126
let minimumGapToSpan = Number.MAX_VALUE;
123-
let bestPerfEntry: XhrPerformanceResourceTiming | undefined = undefined;
127+
let bestPerfEntry: XhrPerformanceResourceTiming | undefined;
124128
for (const perfEntry of perfEntries) {
125129
// If the current entry has cors preflight data use its `startTime` to
126130
// calculate the gap to the span.
@@ -142,6 +146,11 @@ function getBestPerfResourceTiming(
142146
return bestPerfEntry;
143147
}
144148

149+
/**
150+
* A Performance entry is part of a XHR if entry has not been assigned
151+
* previously to another XHR and the URL is the same as the XHR and the
152+
* entry's start/end times are within the span's start/end times.
153+
*/
145154
function isPerfEntryPartOfXhr(
146155
entry: PerformanceResourceTiming,
147156
xhrUrl: string,
@@ -155,12 +164,20 @@ function isPerfEntryPartOfXhr(
155164
);
156165
}
157166

167+
/**
168+
* A possible CORS pair is defined when the entries does not overlap in their
169+
* start/end times.
170+
*/
158171
function isPossibleCorsPair(
159172
entry1: PerformanceResourceTiming,
160173
entry2: PerformanceResourceTiming
161174
): 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).
162179
return (
163-
Math.min(entry1.responseEnd, entry2.responseEnd) >=
180+
Math.min(entry1.responseEnd, entry2.responseEnd) <
164181
Math.max(entry1.startTime, entry2.startTime)
165182
);
166183
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,18 @@ function maybeClearPerfResourceBuffer(): void {
122122

123123
function joinPerfResourceDataToSpan(xhr: XhrWithUrl, span: Span) {
124124
const xhrPerfResourceTiming = getXhrPerfomanceData(xhr.responseURL, span);
125-
if (xhrPerfResourceTiming) {
126-
alreadyAssignedPerfEntries.add(xhrPerfResourceTiming.mainRequest);
127-
if (xhrPerfResourceTiming.corsPreFlightRequest) {
128-
alreadyAssignedPerfEntries.add(
129-
xhrPerfResourceTiming.corsPreFlightRequest
130-
);
131-
const corsPerfTiming = xhrPerfResourceTiming.corsPreFlightRequest as PerformanceResourceTimingExtended;
132-
setCorsPerfTimingAsChildSpan(corsPerfTiming, span);
133-
}
134-
span.annotations = annotationsForPerfTimeFields(
135-
xhrPerfResourceTiming.mainRequest as PerformanceResourceTimingExtended,
136-
PERFORMANCE_ENTRY_EVENTS
137-
);
125+
if (!xhrPerfResourceTiming) return;
126+
127+
alreadyAssignedPerfEntries.add(xhrPerfResourceTiming.mainRequest);
128+
if (xhrPerfResourceTiming.corsPreFlightRequest) {
129+
alreadyAssignedPerfEntries.add(xhrPerfResourceTiming.corsPreFlightRequest);
130+
const corsPerfTiming = xhrPerfResourceTiming.corsPreFlightRequest as PerformanceResourceTimingExtended;
131+
setCorsPerfTimingAsChildSpan(corsPerfTiming, span);
138132
}
133+
span.annotations = annotationsForPerfTimeFields(
134+
xhrPerfResourceTiming.mainRequest as PerformanceResourceTimingExtended,
135+
PERFORMANCE_ENTRY_EVENTS
136+
);
139137
}
140138

141139
function setCorsPerfTimingAsChildSpan(
@@ -152,6 +150,9 @@ function incrementXhrTaskCount(): void {
152150
}
153151

154152
function decrementXhrTaskCount(): void {
153+
// Check the xhr tasks count should be greater than 0 to avoid do negative
154+
// counting. However, this case should never happen, this is just to make
155+
// clear it is not possible to happen.
155156
if (xhrTasksCount > 0) xhrTasksCount--;
156157
maybeClearPerfResourceBuffer();
157158
}

0 commit comments

Comments
 (0)