Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative performance timings #330

Merged
merged 6 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
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;
}
}
Expand All @@ -187,7 +187,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 => {
Expand All @@ -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();
});
});