Skip to content

Commit

Permalink
fix(plugin-document-load): check if getEntriesByType is available bef…
Browse files Browse the repository at this point in the history
…ore using it (open-telemetry#259)

* fix(plugin-document-load): check if
getEntriesByType is available before using it

* fix: lint

Co-authored-by: Bartlomiej Obecny <bobecny@gmail.com>
  • Loading branch information
mhennoch and obecny authored Dec 10, 2020
1 parent d896904 commit 4ed223b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export interface GraphQLInstrumentationConfig {
/**
* Merged and parsed config of default instrumentation config and GraphQL
*/
export type GraphQLInstrumentationParsedConfig = Required<
GraphQLInstrumentationConfig
> &
export type GraphQLInstrumentationParsedConfig = Required<GraphQLInstrumentationConfig> &
InstrumentationConfig;

export type executeFunctionWithObj = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
* @param rootSpan
*/
private _addResourcesSpans(rootSpan: Span): void {
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType(
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType?.(
'resource'
) as PerformanceResourceTiming[];
if (resources) {
Expand Down Expand Up @@ -163,7 +163,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
*/
private _getEntries() {
const entries: PerformanceEntries = {};
const performanceNavigationTiming = (otperformance.getEntriesByType(
const performanceNavigationTiming = (otperformance.getEntriesByType?.(
'navigation'
)[0] as unknown) as PerformanceEntries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,7 @@ describe('DocumentLoad Plugin', () => {
});
});

describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
let spyEntries: any;
beforeEach(() => {
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
spyEntries.withArgs('navigation').returns([]);
spyEntries.withArgs('resource').returns([]);
Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});
afterEach(() => {
spyEntries.restore();
});

function shouldExportCorrectSpan() {
it('should export correct span with events', done => {
const spyOnEnd = sinon.spy(dummyExporter, 'export');
plugin.enable(moduleExports, provider, logger, config);
Expand Down Expand Up @@ -557,6 +543,41 @@ describe('DocumentLoad Plugin', () => {
done();
});
});
}

describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
let spyEntries: any;
beforeEach(() => {
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
spyEntries.withArgs('navigation').returns([]);
spyEntries.withArgs('resource').returns([]);

Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});
afterEach(() => {
spyEntries.restore();
});

shouldExportCorrectSpan();
});

describe('when getEntriesByType is not defined then fallback to "performance.timing"', () => {
beforeEach(() => {
Object.defineProperty(window.performance, 'getEntriesByType', {
writable: true,
value: undefined,
});

Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});

shouldExportCorrectSpan();
});

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

0 comments on commit 4ed223b

Please sign in to comment.