Skip to content

Commit def89c4

Browse files
authored
feat: Add more inspection events (#1494)
1 parent 82b9f78 commit def89c4

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

src/common/drain/drain.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import { dispatchGlobalEvent } from '../../common/dispatch/global-event'
67
import { ee } from '../event-emitter/contextual-ee'
78
import { registerHandler as defaultRegister } from '../event-emitter/register-handler'
89
import { featurePriority } from '../../loaders/features/features'
@@ -92,6 +93,13 @@ function drainGroup (agentIdentifier, group, activateGroup = true) {
9293
const handlers = defaultRegister.handlers // other storage in registerHandler
9394
if (baseEE.aborted || !baseEE.backlog || !handlers) return
9495

96+
dispatchGlobalEvent({
97+
agentIdentifier,
98+
type: 'lifecycle',
99+
name: 'drain',
100+
feature: group
101+
})
102+
95103
// Only activated features being drained should run queued listeners on buffered events. Deactivated features only need to release memory.
96104
if (activateGroup) {
97105
const bufferedEventsInGroup = baseEE.backlog[group]

src/common/window/load.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ export function onDOMContentLoaded (cb) {
1717
if (checkState()) return cb()
1818
documentAddEventListener('DOMContentLoaded', cb)
1919
}
20+
21+
export function onPopstateChange (cb) {
22+
if (checkState()) return cb()
23+
windowAddEventListener('popstate', cb)
24+
}

src/features/page_view_event/instrument/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import { handle } from '../../../common/event-emitter/handle'
66
import { setupSetPageViewNameAPI } from '../../../loaders/api/setPageViewName'
77
import { InstrumentBase } from '../../utils/instrument-base'
88
import * as CONSTANTS from '../constants'
9+
import { SESSION_EVENTS } from '../../../common/session/constants'
10+
import { dispatchGlobalEvent } from '../../../common/dispatch/global-event'
11+
import { onDOMContentLoaded, onPopstateChange, onWindowLoad } from '../../../common/window/load'
912

1013
export class Instrument extends InstrumentBase {
1114
static featureName = CONSTANTS.FEATURE_NAME
1215
constructor (agentRef) {
1316
super(agentRef, CONSTANTS.FEATURE_NAME)
1417

18+
/** setup inspection events for window lifecycle */
19+
this.setupInspectionEvents(agentRef.agentIdentifier)
20+
1521
/** feature specific APIs */
1622
setupSetPageViewNameAPI(agentRef)
1723

@@ -20,6 +26,41 @@ export class Instrument extends InstrumentBase {
2026

2127
this.importAggregator(agentRef, () => import(/* webpackChunkName: "page_view_event-aggregate" */ '../aggregate'))
2228
}
29+
30+
setupInspectionEvents (agentIdentifier) {
31+
const dispatch = (evt, name) => {
32+
if (!evt) return
33+
dispatchGlobalEvent({
34+
agentIdentifier,
35+
timeStamp: evt.timeStamp,
36+
loaded: evt.target.readyState === 'complete',
37+
type: 'window',
38+
name,
39+
data: evt.target.location + ''
40+
})
41+
}
42+
43+
onDOMContentLoaded((evt) => {
44+
dispatch(evt, 'DOMContentLoaded')
45+
})
46+
47+
onWindowLoad((evt) => {
48+
dispatch(evt, 'load')
49+
})
50+
51+
onPopstateChange((evt) => {
52+
dispatch(evt, 'navigate')
53+
})
54+
55+
this.ee.on(SESSION_EVENTS.UPDATE, (_, data) => {
56+
dispatchGlobalEvent({
57+
agentIdentifier,
58+
type: 'lifecycle',
59+
name: 'session',
60+
data
61+
})
62+
})
63+
}
2364
}
2465

2566
export const PageViewEvent = Instrument

tests/assets/inspection-events.html

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
window.addEventListener('newrelic', ({ detail }) => {
1919
if (!detail || !detail.agentIdentifier) return
20-
const { name, drained, type, feature, data } = detail
20+
const { name, drained, type, feature, loaded, timeStamp, data } = detail
2121

2222
if (
2323
name == 'initialize' &&
@@ -59,7 +59,7 @@
5959
window.inspectionEvents.harvest = true
6060
}
6161

62-
// The API call in this test is called before the agent is loaded, so loaded should be false
62+
// The API call in this test is called before the agent is loaded, so drained should be false
6363
if (
6464
name === 'api' &&
6565
drained === false &&
@@ -69,7 +69,63 @@
6969
) {
7070
window.inspectionEvents.api = true
7171
}
72+
73+
if (
74+
name === 'drain' &&
75+
type === 'lifecycle' &&
76+
feature
77+
) {
78+
window.inspectionEvents.drain = true
79+
}
80+
81+
if (
82+
name === 'navigate' &&
83+
type === 'window' &&
84+
loaded == false &&
85+
feature === undefined &&
86+
timeStamp &&
87+
data
88+
) {
89+
window.inspectionEvents.navigate = true
90+
}
91+
92+
if (
93+
name === 'load' &&
94+
type === 'window' &&
95+
loaded == true &&
96+
feature === undefined &&
97+
timeStamp &&
98+
data
99+
) {
100+
window.inspectionEvents.windowLoad = true
101+
}
102+
103+
if (
104+
name === 'DOMContentLoaded' &&
105+
type === 'window' &&
106+
loaded == false &&
107+
feature === undefined &&
108+
timeStamp &&
109+
data
110+
) {
111+
window.inspectionEvents.domLoad = true
112+
}
113+
114+
if (
115+
name === 'session' &&
116+
type === 'lifecycle' &&
117+
feature === undefined &&
118+
data
119+
) {
120+
window.inspectionEvents.session = true
121+
}
72122
})
123+
window.setTimeout(() => {
124+
// Trigger the session event
125+
newrelic.setUserId('1')
126+
// Trigger the navigation event
127+
window.location += '#a=1'
128+
}, 2000)
73129
</script>
74130
{init} {config} {loader}
75131
</head>

tests/specs/inspection-events.e2e.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ describe('inspection events', () => {
88
() => browser.execute(function () {
99
return window?.inspectionEvents?.buffer &&
1010
window.inspectionEvents.harvest &&
11-
window.inspectionEvents.api
11+
window.inspectionEvents.api &&
12+
window.inspectionEvents.navigate
1213
}),
1314
{
1415
timeout: 30000,
@@ -26,5 +27,10 @@ describe('inspection events', () => {
2627
expect(inspectionEvents.buffer).toBe(true)
2728
expect(inspectionEvents.harvest).toBe(true)
2829
expect(inspectionEvents.api).toBe(true)
30+
expect(inspectionEvents.drain).toBe(true)
31+
expect(inspectionEvents.navigate).toBe(true)
32+
expect(inspectionEvents.windowLoad).toBe(true)
33+
expect(inspectionEvents.domLoad).toBe(true)
34+
expect(inspectionEvents.session).toBe(true)
2935
})
3036
})

0 commit comments

Comments
 (0)