From 80b46a5bfe0e3241a00358f6b026aff79d778389 Mon Sep 17 00:00:00 2001 From: Marten Hennoch Date: Thu, 28 Jan 2021 19:59:29 +0200 Subject: [PATCH 1/2] fix(plugin-doc-load): allow negative fetch start --- .../opentelemetry-plugin-document-load/src/documentLoad.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts index d1f9fa356c..d0ec0dd011 100644 --- a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -173,7 +173,7 @@ export class DocumentLoad extends BasePlugin { keys.forEach((key: string) => { if (hasKey(performanceNavigationTiming, key)) { const value = performanceNavigationTiming[key]; - if (typeof value === 'number' && value >= 0) { + if (typeof value === 'number') { entries[key] = value; } } @@ -187,7 +187,7 @@ export class DocumentLoad extends BasePlugin { keys.forEach((key: string) => { if (hasKey(performanceTiming, key)) { const value = performanceTiming[key]; - if (typeof value === 'number' && value >= 0) { + if (typeof value === 'number') { entries[key] = value; } } From 563afc3d20ebdbcdcf329930d3bb6261060fa150 Mon Sep 17 00:00:00 2001 From: Marten Hennoch Date: Thu, 28 Jan 2021 19:59:29 +0200 Subject: [PATCH 2/2] fix(plugin-doc-load): allow negative fetch start --- .../src/documentLoad.ts | 4 +- .../test/documentLoad.test.ts | 72 ++++++++++++------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts index d1f9fa356c..d0ec0dd011 100644 --- a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -173,7 +173,7 @@ export class DocumentLoad extends BasePlugin { keys.forEach((key: string) => { if (hasKey(performanceNavigationTiming, key)) { const value = performanceNavigationTiming[key]; - if (typeof value === 'number' && value >= 0) { + if (typeof value === 'number') { entries[key] = value; } } @@ -187,7 +187,7 @@ export class DocumentLoad extends BasePlugin { keys.forEach((key: string) => { if (hasKey(performanceTiming, key)) { const value = performanceTiming[key]; - if (typeof value === 'number' && value >= 0) { + if (typeof value === 'number') { entries[key] = value; } } diff --git a/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts b/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts index 2a1752db54..91b95c3bec 100644 --- a/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts +++ b/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts @@ -512,6 +512,12 @@ describe('DocumentLoad Plugin', () => { const spyOnEnd = sinon.spy(dummyExporter, 'export'); plugin.enable(moduleExports, provider, logger, config); setTimeout(() => { + assert.strictEqual( + spyOnEnd.callCount, + 2, + `Unexpected number of spans: ${spyOnEnd.callCount}` + ); + const rootSpan = spyOnEnd.args[0][0][0] as ReadableSpan; const fetchSpan = spyOnEnd.args[1][0][0] as ReadableSpan; const rsEvents = rootSpan.events; @@ -537,60 +543,51 @@ describe('DocumentLoad Plugin', () => { assert.strictEqual(rsEvents.length, 9); assert.strictEqual(fsEvents.length, 9); - assert.strictEqual(spyOnEnd.callCount, 2); done(); }); }); } describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => { - let spyEntries: any; + const sandbox = sinon.createSandbox(); beforeEach(() => { - spyEntries = sinon.stub(window.performance, 'getEntriesByType'); - spyEntries.withArgs('navigation').returns([]); - spyEntries.withArgs('resource').returns([]); - - Object.defineProperty(window.performance, 'timing', { - writable: true, - value: entriesFallback, + sandbox.stub(window.performance, 'getEntriesByType').value(undefined); + sandbox.stub(window.performance, 'timing').get(() => { + return entriesFallback; }); }); afterEach(() => { - spyEntries.restore(); + sandbox.restore(); }); shouldExportCorrectSpan(); }); describe('when getEntriesByType is not defined then fallback to "performance.timing"', () => { + const sandbox = sinon.createSandbox(); beforeEach(() => { - Object.defineProperty(window.performance, 'getEntriesByType', { - writable: true, - value: undefined, - }); - - Object.defineProperty(window.performance, 'timing', { - writable: true, - value: entriesFallback, + sandbox.stub(window.performance, 'getEntriesByType').value(undefined); + sandbox.stub(window.performance, 'timing').get(() => { + return entriesFallback; }); }); + afterEach(() => { + sandbox.restore(); + }); shouldExportCorrectSpan(); }); describe('when navigation entries types and "performance.timing" are NOT available', () => { - let spyEntries: any; + const sandbox = sinon.createSandbox(); beforeEach(() => { - Object.defineProperty(window.performance, 'timing', { - writable: true, - value: undefined, + sandbox.stub(window.performance, 'getEntriesByType').value(undefined); + sandbox.stub(window.performance, 'timing').get(() => { + return undefined; }); - spyEntries = sinon.stub(window.performance, 'getEntriesByType'); - spyEntries.withArgs('navigation').returns([]); - spyEntries.withArgs('resource').returns([]); }); afterEach(() => { - spyEntries.restore(); + sandbox.restore(); }); it('should not create any span', done => { @@ -602,4 +599,27 @@ describe('DocumentLoad Plugin', () => { }); }); }); + + describe('when fetchStart is negative still create spans', () => { + const sandbox = sinon.createSandbox(); + beforeEach(() => { + const navEntriesWithNegativeFetch = Object.assign({}, entries, { + fetchStart: -1, + }); + sandbox + .stub(window.performance, 'getEntriesByType') + .withArgs('navigation') + .returns([navEntriesWithNegativeFetch]) + .withArgs('resource') + .returns([]); + + sandbox.stub(window.performance, 'timing').get(() => { + return undefined; + }); + }); + afterEach(() => { + sandbox.restore(); + }); + shouldExportCorrectSpan(); + }); });