|
| 1 | +/** |
| 2 | + * Copyright 2019, OpenCensus Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Span } from '@opencensus/web-core'; |
| 18 | +import { XhrPerformanceResourceTiming } from './zone-types'; |
| 19 | +import { alreadyAssignedPerfEntries } from './xhr-interceptor'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Get Browser's performance resource timing data associated to a XHR. |
| 23 | + * Some XHRs might have two or one performance resource timings as one of them |
| 24 | + * is the CORS pre-flight request and the second is related to the actual HTTP |
| 25 | + * request. |
| 26 | + * In overall, the algorithm to get this data takes the best fit for the span, |
| 27 | + * this means the closest performance resource timing to the span start/end |
| 28 | + * performance times is the returned value. |
| 29 | + */ |
| 30 | +export function getXhrPerfomanceData( |
| 31 | + xhrUrl: string, |
| 32 | + span: Span |
| 33 | +): XhrPerformanceResourceTiming | undefined { |
| 34 | + const filteredSortedPerfEntries = getPerfSortedResourceEntries(xhrUrl, span); |
| 35 | + const possibleEntries = getPossiblePerfResourceEntries( |
| 36 | + filteredSortedPerfEntries |
| 37 | + ); |
| 38 | + return getBestPerfResourceTiming(possibleEntries, span); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * First step for the algorithm. Filter the Performance Resource Timings by the |
| 43 | + * name (it should match the XHR URL), additionally, the start/end timings of |
| 44 | + * every performance entry should fit within the span start/end timings. Also, |
| 45 | + * the entry should not be already assigned to a span. |
| 46 | + * These filtered performance resource entries are considered as possible |
| 47 | + * entries associated to the xhr. |
| 48 | + * Those are possible because there might be more than two entries that pass the |
| 49 | + * filter. |
| 50 | + * Additionally, the returned array is sorted by the entries' `startTime` as |
| 51 | + * getEntriesByType() already does it. |
| 52 | + * (https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntriesByType#Return_Value). |
| 53 | + */ |
| 54 | +export function getPerfSortedResourceEntries( |
| 55 | + xhrUrl: string, |
| 56 | + span: Span |
| 57 | +): PerformanceResourceTiming[] { |
| 58 | + return performance |
| 59 | + .getEntriesByType('resource') |
| 60 | + .filter(entry => |
| 61 | + isPerfEntryPartOfXhr(entry as PerformanceResourceTiming, xhrUrl, span) |
| 62 | + ) as PerformanceResourceTiming[]; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Second step for the 'Performance resource timings selector algorithm'. |
| 67 | + * As the XHR could cause a CORS pre-flight request, we have to look for |
| 68 | + * possible performance entries either containing cors preflight timings or not. |
| 69 | + * A possible entry with cors data is when a resource timing entry does not |
| 70 | + * overlap timings with other resource timing entry. Also, every entry is |
| 71 | + * considered as possible XHR performance entry. |
| 72 | + * Thus, for this step traverse the array of resource entries and for every |
| 73 | + * entry check if it is a possible performance resource entry. |
| 74 | + * @param filteredSortedPerfEntries Sorted array of Performance Resource |
| 75 | + * Entries. This is sorted by the entries' `startTime`. |
| 76 | + */ |
| 77 | +export function getPossiblePerfResourceEntries( |
| 78 | + filteredSortedPerfEntries: PerformanceResourceTiming[] |
| 79 | +): XhrPerformanceResourceTiming[] { |
| 80 | + const possiblePerfEntries = new Array<XhrPerformanceResourceTiming>(); |
| 81 | + // As this part of the algorithm uses nested for loops to examine pairs of |
| 82 | + // entries, although, this array is not large as the performance resource |
| 83 | + // entries buffer is cleared when there are no more running XHRs. Also, this |
| 84 | + // data is already filtered by URL and start/end time to be within the span. |
| 85 | + for (let i = 0; i < filteredSortedPerfEntries.length; i++) { |
| 86 | + const entryI = filteredSortedPerfEntries[i]; |
| 87 | + // Consider the current entry as a possible entry without cors preflight |
| 88 | + // request. This is valid as this entry might be better than other possible |
| 89 | + // entries. |
| 90 | + possiblePerfEntries.push({ mainRequest: entryI }); |
| 91 | + // Compare every performance entry with the perfomance entries in front of |
| 92 | + // it. This is possible as the entries are sorted by the startTime. That |
| 93 | + // way, we avoid comparing twice the entries and taking the wrong order. |
| 94 | + for (let j = i + 1; j < filteredSortedPerfEntries.length; j++) { |
| 95 | + const entryJ = filteredSortedPerfEntries[j]; |
| 96 | + if (isPossibleCorsPair(entryI, entryJ)) { |
| 97 | + // As the entries are not overlapping, that means those timings |
| 98 | + // are possible perfomance timings related to the XHR. |
| 99 | + possiblePerfEntries.push({ |
| 100 | + corsPreFlightRequest: entryI, |
| 101 | + mainRequest: entryJ, |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return possiblePerfEntries; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Pick the best performance resource timing for the XHR: Using the possible |
| 111 | + * performance resource timing entries from previous step, the best entry will |
| 112 | + * be the one with the minimum gap to the span start/end timings. |
| 113 | + * The performance resource timing entry with the minimum gap to the span |
| 114 | + * start/end timings points out that entry is the best fit for the span. |
| 115 | + */ |
| 116 | +function getBestPerfResourceTiming( |
| 117 | + perfEntries: XhrPerformanceResourceTiming[], |
| 118 | + span: Span |
| 119 | +): XhrPerformanceResourceTiming | undefined { |
| 120 | + let minimumGapToSpan = Number.MAX_VALUE; |
| 121 | + let bestPerfEntry: XhrPerformanceResourceTiming | undefined; |
| 122 | + for (const perfEntry of perfEntries) { |
| 123 | + // If the current entry has cors preflight data use its `startTime` to |
| 124 | + // calculate the gap to the span. |
| 125 | + const perfEntryStartTime = perfEntry.corsPreFlightRequest |
| 126 | + ? perfEntry.corsPreFlightRequest.startTime |
| 127 | + : perfEntry.mainRequest.startTime; |
| 128 | + const gapToSpan = |
| 129 | + span.endPerfTime - |
| 130 | + perfEntry.mainRequest.responseEnd + |
| 131 | + (perfEntryStartTime - span.startPerfTime); |
| 132 | + |
| 133 | + // If there is a new minimum gap to the span, update the minimum and pick |
| 134 | + // the current performance entry as the best at this point. |
| 135 | + if (gapToSpan < minimumGapToSpan) { |
| 136 | + minimumGapToSpan = gapToSpan; |
| 137 | + bestPerfEntry = perfEntry; |
| 138 | + } |
| 139 | + } |
| 140 | + return bestPerfEntry; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * A Performance entry is part of a XHR if entry has not been assigned |
| 145 | + * previously to another XHR and the URL is the same as the XHR and the |
| 146 | + * entry's start/end times are within the span's start/end times. |
| 147 | + */ |
| 148 | +function isPerfEntryPartOfXhr( |
| 149 | + entry: PerformanceResourceTiming, |
| 150 | + xhrUrl: string, |
| 151 | + span: Span |
| 152 | +): boolean { |
| 153 | + return ( |
| 154 | + !alreadyAssignedPerfEntries.has(entry) && |
| 155 | + entry.name === xhrUrl && |
| 156 | + entry.startTime >= span.startPerfTime && |
| 157 | + entry.responseEnd <= span.endPerfTime |
| 158 | + ); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * A possible CORS pair is defined when the entries does not overlap in their |
| 163 | + * start/end times. |
| 164 | + */ |
| 165 | +function isPossibleCorsPair( |
| 166 | + maybePreflight: PerformanceResourceTiming, |
| 167 | + maybeMainRequest: PerformanceResourceTiming |
| 168 | +): boolean { |
| 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; |
| 174 | +} |
0 commit comments