UNPKG

@opentelemetry/instrumentation-document-load

Version:
54 lines 1.95 kB
/* * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ import { otperformance } from '@opentelemetry/core'; import { hasKey, PerformanceTimingNames as PTN, } from '@opentelemetry/sdk-trace-web'; import { EventNames } from './enums/EventNames'; export const getPerformanceNavigationEntries = () => { const entries = {}; const performanceNavigationTiming = otperformance.getEntriesByType?.('navigation')[0]; if (performanceNavigationTiming) { const keys = Object.values(PTN); keys.forEach((key) => { if (hasKey(performanceNavigationTiming, key)) { const value = performanceNavigationTiming[key]; if (typeof value === 'number') { entries[key] = value; } } }); } else { // // fallback to previous version const perf = otperformance; const performanceTiming = perf.timing; if (performanceTiming) { const keys = Object.values(PTN); keys.forEach((key) => { if (hasKey(performanceTiming, key)) { const value = performanceTiming[key]; if (typeof value === 'number') { entries[key] = value; } } }); } } return entries; }; const performancePaintNames = { 'first-paint': EventNames.FIRST_PAINT, 'first-contentful-paint': EventNames.FIRST_CONTENTFUL_PAINT, }; export const addSpanPerformancePaintEvents = (span) => { const performancePaintTiming = otperformance.getEntriesByType?.('paint'); if (performancePaintTiming) { performancePaintTiming.forEach(({ name, startTime }) => { if (hasKey(performancePaintNames, name)) { span.addEvent(performancePaintNames[name], startTime); } }); } }; //# sourceMappingURL=utils.js.map