Skip to content

Commit

Permalink
feat(instrumentation-document-load): documentLoad attributes enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
kkruk-sumo committed Apr 21, 2021
1 parent b128dae commit c644641
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export class DocumentLoadInstrumentation extends InstrumentationBase<unknown> {
}
});

rootSpan.setAttribute(HttpAttribute.HTTP_URL, location.href);
rootSpan.setAttribute(HttpAttribute.HTTP_USER_AGENT, navigator.userAgent);
rootSpan.setAttribute(AttributeNames.PAGE_TITLE, document.title);

this._addResourcesSpans(rootSpan);

addSpanNetworkEvent(rootSpan, PTN.FETCH_START, entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export enum AttributeNames {
DOCUMENT_LOAD = 'documentLoad',
DOCUMENT_FETCH = 'documentFetch',
RESOURCE_FETCH = 'resourceFetch',
PAGE_TITLE = 'page_title',
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ const entriesFallback = {
loadEventEnd: 1571078170394,
} as any;

const userAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36';

const documentTitle = 'OpenTelemetry Blog';

function ensureNetworkEventsExists(events: TimedEvent[]) {
assert.strictEqual(events[0].name, PTN.FETCH_START);
assert.strictEqual(events[1].name, PTN.DOMAIN_LOOKUP_START);
Expand All @@ -198,6 +203,8 @@ describe('DocumentLoad Instrumentation', () => {
writable: true,
value: 'complete',
});
sandbox.replaceGetter(navigator, 'userAgent', () => userAgent);
sandbox.replaceGetter(document, 'title', () => documentTitle);
plugin = new DocumentLoadInstrumentation({
enabled: false,
});
Expand Down Expand Up @@ -492,31 +499,38 @@ describe('DocumentLoad Instrumentation', () => {
it('should export correct span with events', done => {
plugin.enable();
setTimeout(() => {
const rootSpan = exporter.getFinishedSpans()[0] as ReadableSpan;
const fetchSpan = exporter.getFinishedSpans()[1] as ReadableSpan;
const rsEvents = rootSpan.events;
const fetchSpan = exporter.getFinishedSpans()[0] as ReadableSpan;
const rootSpan = exporter.getFinishedSpans()[1] as ReadableSpan;
const fsEvents = fetchSpan.events;
const rsEvents = rootSpan.events;

assert.strictEqual(rootSpan.name, 'documentFetch');
assert.strictEqual(fetchSpan.name, 'documentLoad');
assert.strictEqual(fetchSpan.name, 'documentFetch');
assert.strictEqual(rootSpan.name, 'documentLoad');

ensureNetworkEventsExists(rsEvents);
assert.strictEqual(
rootSpan.attributes['http.url'],
'http://localhost:9876/context.html'
);
assert.strictEqual(rootSpan.attributes['http.user_agent'], userAgent);
assert.strictEqual(rootSpan.attributes.page_title, documentTitle);

assert.strictEqual(fsEvents[0].name, PTN.FETCH_START);
assert.strictEqual(fsEvents[1].name, PTN.UNLOAD_EVENT_START);
assert.strictEqual(fsEvents[2].name, PTN.UNLOAD_EVENT_END);
assert.strictEqual(fsEvents[3].name, PTN.DOM_INTERACTIVE);
ensureNetworkEventsExists(fsEvents);

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(
fsEvents[4].name,
rsEvents[4].name,
PTN.DOM_CONTENT_LOADED_EVENT_START
);
assert.strictEqual(fsEvents[5].name, PTN.DOM_CONTENT_LOADED_EVENT_END);
assert.strictEqual(fsEvents[6].name, PTN.DOM_COMPLETE);
assert.strictEqual(fsEvents[7].name, PTN.LOAD_EVENT_START);
assert.strictEqual(fsEvents[8].name, PTN.LOAD_EVENT_END);
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);

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

0 comments on commit c644641

Please sign in to comment.