|
| 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 {adjustPerfTimeOrigin, tracing} from '@opencensus/web-core'; |
| 18 | +import {OCAgentExporter} from '@opencensus/web-exporter-ocagent'; |
| 19 | +import {clearPerfEntries, getInitialLoadRootSpan, getPerfEntries} from '@opencensus/web-instrumentation-perf'; |
| 20 | + |
| 21 | +import {OpenCensusWebConfig, WindowWithOcwGlobals} from './types'; |
| 22 | + |
| 23 | +const windowWithOcwGlobals = window as WindowWithOcwGlobals; |
| 24 | + |
| 25 | +/** |
| 26 | + * How long to wait after `load` event to export initial load spans. This allows |
| 27 | + * time for any other post-load handlers to run first so that the work to export |
| 28 | + * spans does not slow down the user experience. |
| 29 | + */ |
| 30 | +const WAIT_TIME_AFTER_LOAD_MS = 2000; // 2 seconds |
| 31 | + |
| 32 | +/** Trace endpoint in the OC agent. */ |
| 33 | +const TRACE_ENDPOINT = '/v1/trace'; |
| 34 | + |
| 35 | +/** |
| 36 | + * Waits until after the document `load` event fires, and then uses the |
| 37 | + * `window.ocwConfig` settings to configure an OpenCensus agent exporter and |
| 38 | + * export the spans for the initial page load. |
| 39 | + */ |
| 40 | +export function exportRootSpanAfterLoadEvent() { |
| 41 | + const config = windowWithOcwGlobals.ocwConfig; |
| 42 | + if (!config || !config.agent || !config.sampled) { |
| 43 | + console.log('Not configured to export page load spans.'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + tracing.registerExporter( |
| 48 | + new OCAgentExporter({agentEndpoint: `${config.agent}${TRACE_ENDPOINT}`})); |
| 49 | + |
| 50 | + if (document.readyState === 'complete') { |
| 51 | + exportInitialLoadSpans(config); |
| 52 | + } else { |
| 53 | + window.addEventListener('load', () => { |
| 54 | + exportInitialLoadSpans(config); |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function exportInitialLoadSpans(config: OpenCensusWebConfig) { |
| 60 | + setTimeout(() => { |
| 61 | + const perfEntries = getPerfEntries(); |
| 62 | + |
| 63 | + // Adjust the performance time origin with server time if we have it. |
| 64 | + if (perfEntries.navigationTiming && config.reqStartTime && |
| 65 | + config.reqDuration) { |
| 66 | + adjustPerfTimeOrigin( |
| 67 | + config.reqStartTime, config.reqDuration, |
| 68 | + perfEntries.navigationTiming); |
| 69 | + } |
| 70 | + |
| 71 | + const root = getInitialLoadRootSpan( |
| 72 | + tracing.tracer, perfEntries, config.spanId, config.traceId); |
| 73 | + |
| 74 | + clearPerfEntries(); |
| 75 | + // Notify that the span has ended to trigger export. |
| 76 | + tracing.tracer.onEndSpan(root); |
| 77 | + }, WAIT_TIME_AFTER_LOAD_MS); |
| 78 | +} |
0 commit comments