|
| 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 {Annotation, ATTRIBUTE_HTTP_URL, ATTRIBUTE_HTTP_USER_AGENT, ATTRIBUTE_LONG_TASK_ATTRIBUTION, ATTRIBUTE_NAV_TYPE, parseUrl, RootSpan, Span, SpanKind, Tracer} from '@opencensus/web-core'; |
| 18 | +import {GroupedPerfEntries} from './perf-recorder'; |
| 19 | +import {PerformanceLongTaskTiming, PerformanceNavigationTimingExtended} from './perf-types'; |
| 20 | +import {getResourceSpan} from './resource-span'; |
| 21 | +import {annotationsForPerfTimeFields} from './util'; |
| 22 | + |
| 23 | +/** |
| 24 | + * These are properties of PerformanceNavigationTiming that will be turned |
| 25 | + * into span annotations on the navigation span. |
| 26 | + */ |
| 27 | +const NAVIGATION_TIMING_EVENTS = [ |
| 28 | + 'domLoading', |
| 29 | + 'domInteractive', |
| 30 | + 'domContentLoaded', |
| 31 | + 'domComplete', |
| 32 | + 'loadEventStart', |
| 33 | + 'loadEventEnd', |
| 34 | + 'unloadEventStart', |
| 35 | + 'unloadEventEnd', |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Returns a root span for the initial page load along with child spans for |
| 40 | + * resource timings and long tasks that were part of the load. The root span |
| 41 | + * represents the full time between when the navigation was initiated in the |
| 42 | + * browser to when the `load` event fires. |
| 43 | + * @param tracer The tracer to associate the root span with |
| 44 | + * @param perfEntries Performance timing entries grouped by type. These should |
| 45 | + * include the entries up to soon after the `load` browser event fires. |
| 46 | + * @param navigationFetchSpanId This is the ID for the span that represents the |
| 47 | + * HTTP request to get the initial HTML. This should be sent back from the |
| 48 | + * server to enable linking the server and client side spans for the initial |
| 49 | + * HTML fetch. |
| 50 | + * @param traceId The trace ID that all spans returned should have. This should |
| 51 | + * also be specified by the server to enable linking between the server and |
| 52 | + * client spans for the initial HTML fetch. |
| 53 | + */ |
| 54 | +export function getInitialLoadRootSpan( |
| 55 | + tracer: Tracer, perfEntries: GroupedPerfEntries, |
| 56 | + navigationFetchSpanId: string, traceId: string): RootSpan { |
| 57 | + const navTiming = perfEntries.navigationTiming; |
| 58 | + const navigationUrl = navTiming ? navTiming.name : location.href; |
| 59 | + const parsedNavigationUrl = parseUrl(navigationUrl); |
| 60 | + const navigationPath = parsedNavigationUrl.pathname; |
| 61 | + const root = new RootSpan(tracer, { |
| 62 | + name: `Nav.${navigationPath}`, |
| 63 | + spanContext: { |
| 64 | + traceId, |
| 65 | + // This becomes the parentSpanId field of the root span, and the actual |
| 66 | + // span ID for the root span gets assigned to a random number. |
| 67 | + spanId: '', |
| 68 | + }, |
| 69 | + kind: SpanKind.UNSPECIFIED, |
| 70 | + }); |
| 71 | + root.startPerfTime = 0; |
| 72 | + root.annotations = getNavigationAnnotations(perfEntries); |
| 73 | + root.attributes[ATTRIBUTE_HTTP_URL] = navigationUrl; |
| 74 | + root.attributes[ATTRIBUTE_HTTP_USER_AGENT] = navigator.userAgent; |
| 75 | + |
| 76 | + if (navTiming) { |
| 77 | + root.endPerfTime = navTiming.loadEventEnd; |
| 78 | + root.attributes[ATTRIBUTE_NAV_TYPE] = navTiming.type; |
| 79 | + const navFetchSpan = getNavigationFetchSpan( |
| 80 | + navTiming, navigationUrl, traceId, root.id, navigationFetchSpanId); |
| 81 | + root.spans.push(navFetchSpan); |
| 82 | + } |
| 83 | + |
| 84 | + const resourceSpans = perfEntries.resourceTimings.map( |
| 85 | + (resourceTiming) => getResourceSpan(resourceTiming, traceId, root.id)); |
| 86 | + const longTaskSpans = perfEntries.longTasks.map( |
| 87 | + (longTaskTiming) => getLongTaskSpan(longTaskTiming, traceId, root.id)); |
| 88 | + |
| 89 | + root.spans = root.spans.concat(resourceSpans, longTaskSpans); |
| 90 | + return root; |
| 91 | +} |
| 92 | + |
| 93 | +/** Returns a parent span for the HTTP request to retrieve the initial HTML. */ |
| 94 | +function getNavigationFetchSpan( |
| 95 | + navigationTiming: PerformanceNavigationTimingExtended, |
| 96 | + navigationName: string, traceId: string, parentSpanId: string, |
| 97 | + spanId: string): Span { |
| 98 | + const span = getResourceSpan(navigationTiming, traceId, parentSpanId, spanId); |
| 99 | + span.startPerfTime = navigationTiming.fetchStart; |
| 100 | + return span; |
| 101 | +} |
| 102 | + |
| 103 | +/** Formats a performance long task event as a span. */ |
| 104 | +function getLongTaskSpan( |
| 105 | + longTask: PerformanceLongTaskTiming, traceId: string, |
| 106 | + parentSpanId: string): Span { |
| 107 | + const span = new Span(); |
| 108 | + span.traceId = traceId; |
| 109 | + span.parentSpanId = parentSpanId; |
| 110 | + span.name = 'Long JS task'; |
| 111 | + span.startPerfTime = longTask.startTime; |
| 112 | + span.endPerfTime = longTask.startTime + longTask.duration; |
| 113 | + span.attributes[ATTRIBUTE_LONG_TASK_ATTRIBUTION] = |
| 114 | + JSON.stringify(longTask.attribution); |
| 115 | + return span; |
| 116 | +} |
| 117 | + |
| 118 | +/** Gets annotations for a navigation span including paint timings. */ |
| 119 | +function getNavigationAnnotations(perfEntries: GroupedPerfEntries): |
| 120 | + Annotation[] { |
| 121 | + const navigation = perfEntries.navigationTiming; |
| 122 | + if (!navigation) return []; |
| 123 | + |
| 124 | + const navAnnotations = |
| 125 | + annotationsForPerfTimeFields(navigation, NAVIGATION_TIMING_EVENTS); |
| 126 | + |
| 127 | + for (const paintTiming of perfEntries.paintTimings) { |
| 128 | + navAnnotations.push({ |
| 129 | + timestamp: paintTiming.startTime, |
| 130 | + description: paintTiming.name, |
| 131 | + attributes: {}, |
| 132 | + }); |
| 133 | + } |
| 134 | + return navAnnotations; |
| 135 | +} |
0 commit comments