Skip to content

Commit 4ed223b

Browse files
mhennochobecny
andauthored
fix(plugin-document-load): check if getEntriesByType is available before using it (#259)
* fix(plugin-document-load): check if getEntriesByType is available before using it * fix: lint Co-authored-by: Bartlomiej Obecny <[email protected]>
1 parent d896904 commit 4ed223b

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

plugins/node/opentelemetry-instrumentation-graphql/src/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export interface GraphQLInstrumentationConfig {
6363
/**
6464
* Merged and parsed config of default instrumentation config and GraphQL
6565
*/
66-
export type GraphQLInstrumentationParsedConfig = Required<
67-
GraphQLInstrumentationConfig
68-
> &
66+
export type GraphQLInstrumentationParsedConfig = Required<GraphQLInstrumentationConfig> &
6967
InstrumentationConfig;
7068

7169
export type executeFunctionWithObj = (

plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
7373
* @param rootSpan
7474
*/
7575
private _addResourcesSpans(rootSpan: Span): void {
76-
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType(
76+
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType?.(
7777
'resource'
7878
) as PerformanceResourceTiming[];
7979
if (resources) {
@@ -163,7 +163,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
163163
*/
164164
private _getEntries() {
165165
const entries: PerformanceEntries = {};
166-
const performanceNavigationTiming = (otperformance.getEntriesByType(
166+
const performanceNavigationTiming = (otperformance.getEntriesByType?.(
167167
'navigation'
168168
)[0] as unknown) as PerformanceEntries;
169169

plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts

+36-15
Original file line numberDiff line numberDiff line change
@@ -511,21 +511,7 @@ describe('DocumentLoad Plugin', () => {
511511
});
512512
});
513513

514-
describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
515-
let spyEntries: any;
516-
beforeEach(() => {
517-
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
518-
spyEntries.withArgs('navigation').returns([]);
519-
spyEntries.withArgs('resource').returns([]);
520-
Object.defineProperty(window.performance, 'timing', {
521-
writable: true,
522-
value: entriesFallback,
523-
});
524-
});
525-
afterEach(() => {
526-
spyEntries.restore();
527-
});
528-
514+
function shouldExportCorrectSpan() {
529515
it('should export correct span with events', done => {
530516
const spyOnEnd = sinon.spy(dummyExporter, 'export');
531517
plugin.enable(moduleExports, provider, logger, config);
@@ -557,6 +543,41 @@ describe('DocumentLoad Plugin', () => {
557543
done();
558544
});
559545
});
546+
}
547+
548+
describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
549+
let spyEntries: any;
550+
beforeEach(() => {
551+
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
552+
spyEntries.withArgs('navigation').returns([]);
553+
spyEntries.withArgs('resource').returns([]);
554+
555+
Object.defineProperty(window.performance, 'timing', {
556+
writable: true,
557+
value: entriesFallback,
558+
});
559+
});
560+
afterEach(() => {
561+
spyEntries.restore();
562+
});
563+
564+
shouldExportCorrectSpan();
565+
});
566+
567+
describe('when getEntriesByType is not defined then fallback to "performance.timing"', () => {
568+
beforeEach(() => {
569+
Object.defineProperty(window.performance, 'getEntriesByType', {
570+
writable: true,
571+
value: undefined,
572+
});
573+
574+
Object.defineProperty(window.performance, 'timing', {
575+
writable: true,
576+
value: entriesFallback,
577+
});
578+
});
579+
580+
shouldExportCorrectSpan();
560581
});
561582

562583
describe('when navigation entries types and "performance.timing" are NOT available', () => {

0 commit comments

Comments
 (0)