Skip to content

Commit

Permalink
test(instr-document-load): fix test to allow missing network span eve…
Browse files Browse the repository at this point in the history
…nts (#2145)

The change in open-telemetry/opentelemetry-js#4486
means that a addSpanNetworkEvent() in  v1.24.0 and
later might get dropped -- if its time value is before the fetchStart time.
Typically this happens if the event time value is 0.
  • Loading branch information
trentm authored Apr 25, 2024
1 parent 931318c commit 46b6775
Showing 1 changed file with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,43 +555,44 @@ describe('DocumentLoad Instrumentation', () => {
assert.strictEqual(rootSpan.attributes['http.user_agent'], userAgent);

ensureNetworkEventsExists(fsEvents);
assert.strictEqual(fsEvents.length, 8);

assert.strictEqual(rsEvents[0].name, PTN.FETCH_START);
assert.strictEqual(rsEvents[1].name, PTN.UNLOAD_EVENT_START);
assert.strictEqual(rsEvents[2].name, PTN.UNLOAD_EVENT_END);
assert.strictEqual(rsEvents[3].name, PTN.DOM_INTERACTIVE);
assert.strictEqual(
rsEvents[4].name,
PTN.DOM_CONTENT_LOADED_EVENT_START
);
assert.strictEqual(rsEvents[5].name, PTN.DOM_CONTENT_LOADED_EVENT_END);
assert.strictEqual(rsEvents[6].name, PTN.DOM_COMPLETE);
assert.strictEqual(rsEvents[7].name, PTN.LOAD_EVENT_START);
assert.strictEqual(rsEvents[8].name, PTN.LOAD_EVENT_END);
const rsEventNames = rsEvents.map(e => e.name);
// Allow the unloadEvent{Start,End} events to be missing. Tests that
// are simulating a fallback to window.performance.timing are using
// values (entriesFallback) for that result in those network span
// events being dropped after https://github.com/open-telemetry/opentelemetry-js/pull/4486
// (@opentelemetry/sdk-trace-web@1.24.0).
const expectedRsEventNames =
rsEventNames[1] === PTN.UNLOAD_EVENT_START
? [
PTN.FETCH_START,
PTN.UNLOAD_EVENT_START,
PTN.UNLOAD_EVENT_END,
PTN.DOM_INTERACTIVE,
PTN.DOM_CONTENT_LOADED_EVENT_START,
PTN.DOM_CONTENT_LOADED_EVENT_END,
PTN.DOM_COMPLETE,
PTN.LOAD_EVENT_START,
PTN.LOAD_EVENT_END,
]
: [
PTN.FETCH_START,
PTN.DOM_INTERACTIVE,
PTN.DOM_CONTENT_LOADED_EVENT_START,
PTN.DOM_CONTENT_LOADED_EVENT_END,
PTN.DOM_COMPLETE,
PTN.LOAD_EVENT_START,
PTN.LOAD_EVENT_END,
];
assert.deepStrictEqual(rsEventNames, expectedRsEventNames);

assert.strictEqual(fsEvents.length, 8);
assert.strictEqual(rsEvents.length, 9);
assert.strictEqual(exporter.getFinishedSpans().length, 2);
done();
});
});
}

describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
const sandbox = sinon.createSandbox();
beforeEach(() => {
sandbox.stub(window.performance, 'getEntriesByType').value(undefined);
sandbox.stub(window.performance, 'timing').get(() => {
return entriesFallback;
});
});
afterEach(() => {
sandbox.restore();
});

shouldExportCorrectSpan();
});

describe('when getEntriesByType is not defined then fallback to "performance.timing"', () => {
const sandbox = sinon.createSandbox();
beforeEach(() => {
Expand Down

0 comments on commit 46b6775

Please sign in to comment.