@cloudcare/browser-rum 3.1.19 → 3.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/rum.js +11 -10
  4. package/cjs/boot/rum.js.map +1 -1
  5. package/cjs/domain/assembly.js.map +1 -1
  6. package/cjs/domain/contexts/pageStateHistory.js +4 -1
  7. package/cjs/domain/contexts/pageStateHistory.js.map +1 -1
  8. package/cjs/domain/contexts/urlContexts.js +3 -1
  9. package/cjs/domain/contexts/urlContexts.js.map +1 -1
  10. package/cjs/domain/contexts/viewContexts.js +9 -1
  11. package/cjs/domain/contexts/viewContexts.js.map +1 -1
  12. package/cjs/domain/locationChangeObservable.js +6 -4
  13. package/cjs/domain/locationChangeObservable.js.map +1 -1
  14. package/cjs/domain/performanceCollection.js +40 -90
  15. package/cjs/domain/performanceCollection.js.map +1 -1
  16. package/cjs/domain/performanceObservable.js +113 -0
  17. package/cjs/domain/performanceObservable.js.map +1 -0
  18. package/cjs/domain/performanceUtils.js +20 -0
  19. package/cjs/domain/performanceUtils.js.map +1 -0
  20. package/cjs/domain/replay/deflate/deflateWorker.js +1 -1
  21. package/cjs/domain/replay/record/observers.js +58 -54
  22. package/cjs/domain/replay/record/observers.js.map +1 -1
  23. package/cjs/domain/requestCollection.js +6 -2
  24. package/cjs/domain/requestCollection.js.map +1 -1
  25. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +3 -1
  26. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js.map +1 -1
  27. package/cjs/domain/rumEventsCollection/longTask/longTaskCollection.js +5 -7
  28. package/cjs/domain/rumEventsCollection/longTask/longTaskCollection.js.map +1 -1
  29. package/cjs/domain/rumEventsCollection/resource/{matchRequestTiming.js → matchRequestResourceEntry.js} +4 -4
  30. package/cjs/domain/rumEventsCollection/resource/matchRequestResourceEntry.js.map +1 -0
  31. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +64 -66
  32. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  33. package/cjs/domain/rumEventsCollection/resource/resourceUtils.js +18 -14
  34. package/cjs/domain/rumEventsCollection/resource/resourceUtils.js.map +1 -1
  35. package/cjs/domain/rumEventsCollection/resource/retrieveInitialDocumentResourceTiming.js +41 -0
  36. package/cjs/domain/rumEventsCollection/resource/retrieveInitialDocumentResourceTiming.js.map +1 -0
  37. package/cjs/domain/rumEventsCollection/view/trackCommonViewMetrics.js +1 -1
  38. package/cjs/domain/rumEventsCollection/view/trackCommonViewMetrics.js.map +1 -1
  39. package/cjs/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js +8 -5
  40. package/cjs/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js.map +1 -1
  41. package/cjs/domain/rumEventsCollection/view/trackFirstContentfulPaint.js +2 -1
  42. package/cjs/domain/rumEventsCollection/view/trackFirstContentfulPaint.js.map +1 -1
  43. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js +1 -1
  44. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js.map +1 -1
  45. package/cjs/domain/rumEventsCollection/view/trackFirstInput.js +2 -1
  46. package/cjs/domain/rumEventsCollection/view/trackFirstInput.js.map +1 -1
  47. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js +1 -1
  48. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js.map +1 -1
  49. package/cjs/domain/rumEventsCollection/view/trackInteractionToNextPaint.js +3 -19
  50. package/cjs/domain/rumEventsCollection/view/trackInteractionToNextPaint.js.map +1 -1
  51. package/cjs/domain/rumEventsCollection/view/trackLargestContentfulPaint.js +2 -1
  52. package/cjs/domain/rumEventsCollection/view/trackLargestContentfulPaint.js.map +1 -1
  53. package/cjs/domain/rumEventsCollection/view/trackLoadingTime.js +7 -7
  54. package/cjs/domain/rumEventsCollection/view/trackLoadingTime.js.map +1 -1
  55. package/cjs/domain/rumEventsCollection/view/trackNavigationTimings.js +50 -25
  56. package/cjs/domain/rumEventsCollection/view/trackNavigationTimings.js.map +1 -1
  57. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -22
  58. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js.map +1 -1
  59. package/cjs/domain/rumEventsCollection/view/trackViews.js +1 -1
  60. package/cjs/domain/rumEventsCollection/view/trackViews.js.map +1 -1
  61. package/cjs/domain/rumEventsCollection/view/viewCollection.js +1 -0
  62. package/cjs/domain/rumEventsCollection/view/viewCollection.js.map +1 -1
  63. package/cjs/domain/waitPageActivityEnd.js +6 -5
  64. package/cjs/domain/waitPageActivityEnd.js.map +1 -1
  65. package/esm/boot/buildEnv.js +1 -1
  66. package/esm/boot/rum.js +11 -10
  67. package/esm/boot/rum.js.map +1 -1
  68. package/esm/domain/assembly.js.map +1 -1
  69. package/esm/domain/contexts/pageStateHistory.js +5 -2
  70. package/esm/domain/contexts/pageStateHistory.js.map +1 -1
  71. package/esm/domain/contexts/urlContexts.js +4 -2
  72. package/esm/domain/contexts/urlContexts.js.map +1 -1
  73. package/esm/domain/contexts/viewContexts.js +10 -2
  74. package/esm/domain/contexts/viewContexts.js.map +1 -1
  75. package/esm/domain/locationChangeObservable.js +7 -5
  76. package/esm/domain/locationChangeObservable.js.map +1 -1
  77. package/esm/domain/performanceCollection.js +41 -89
  78. package/esm/domain/performanceCollection.js.map +1 -1
  79. package/esm/domain/performanceObservable.js +105 -0
  80. package/esm/domain/performanceObservable.js.map +1 -0
  81. package/esm/domain/performanceUtils.js +14 -0
  82. package/esm/domain/performanceUtils.js.map +1 -0
  83. package/esm/domain/replay/deflate/deflateWorker.js +1 -1
  84. package/esm/domain/replay/record/observers.js +59 -55
  85. package/esm/domain/replay/record/observers.js.map +1 -1
  86. package/esm/domain/requestCollection.js +6 -2
  87. package/esm/domain/requestCollection.js.map +1 -1
  88. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +4 -2
  89. package/esm/domain/rumEventsCollection/actions/trackClickActions.js.map +1 -1
  90. package/esm/domain/rumEventsCollection/longTask/longTaskCollection.js +6 -8
  91. package/esm/domain/rumEventsCollection/longTask/longTaskCollection.js.map +1 -1
  92. package/esm/domain/rumEventsCollection/resource/{matchRequestTiming.js → matchRequestResourceEntry.js} +5 -5
  93. package/esm/domain/rumEventsCollection/resource/matchRequestResourceEntry.js.map +1 -0
  94. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +66 -68
  95. package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  96. package/esm/domain/rumEventsCollection/resource/resourceUtils.js +12 -9
  97. package/esm/domain/rumEventsCollection/resource/resourceUtils.js.map +1 -1
  98. package/esm/domain/rumEventsCollection/resource/retrieveInitialDocumentResourceTiming.js +35 -0
  99. package/esm/domain/rumEventsCollection/resource/retrieveInitialDocumentResourceTiming.js.map +1 -0
  100. package/esm/domain/rumEventsCollection/view/trackCommonViewMetrics.js +1 -1
  101. package/esm/domain/rumEventsCollection/view/trackCommonViewMetrics.js.map +1 -1
  102. package/esm/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js +9 -7
  103. package/esm/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js.map +1 -1
  104. package/esm/domain/rumEventsCollection/view/trackFirstContentfulPaint.js +2 -1
  105. package/esm/domain/rumEventsCollection/view/trackFirstContentfulPaint.js.map +1 -1
  106. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js +1 -1
  107. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js.map +1 -1
  108. package/esm/domain/rumEventsCollection/view/trackFirstInput.js +2 -2
  109. package/esm/domain/rumEventsCollection/view/trackFirstInput.js.map +1 -1
  110. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js +1 -1
  111. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js.map +1 -1
  112. package/esm/domain/rumEventsCollection/view/trackInteractionToNextPaint.js +2 -19
  113. package/esm/domain/rumEventsCollection/view/trackInteractionToNextPaint.js.map +1 -1
  114. package/esm/domain/rumEventsCollection/view/trackLargestContentfulPaint.js +2 -2
  115. package/esm/domain/rumEventsCollection/view/trackLargestContentfulPaint.js.map +1 -1
  116. package/esm/domain/rumEventsCollection/view/trackLoadingTime.js +7 -7
  117. package/esm/domain/rumEventsCollection/view/trackLoadingTime.js.map +1 -1
  118. package/esm/domain/rumEventsCollection/view/trackNavigationTimings.js +51 -26
  119. package/esm/domain/rumEventsCollection/view/trackNavigationTimings.js.map +1 -1
  120. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -21
  121. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js.map +1 -1
  122. package/esm/domain/rumEventsCollection/view/trackViews.js +1 -1
  123. package/esm/domain/rumEventsCollection/view/trackViews.js.map +1 -1
  124. package/esm/domain/rumEventsCollection/view/viewCollection.js +1 -0
  125. package/esm/domain/rumEventsCollection/view/viewCollection.js.map +1 -1
  126. package/esm/domain/waitPageActivityEnd.js +7 -7
  127. package/esm/domain/waitPageActivityEnd.js.map +1 -1
  128. package/package.json +4 -4
  129. package/src/boot/rum.js +18 -9
  130. package/src/domain/assembly.js +0 -1
  131. package/src/domain/contexts/pageStateHistory.js +5 -5
  132. package/src/domain/contexts/urlContexts.js +4 -2
  133. package/src/domain/contexts/viewContexts.js +10 -3
  134. package/src/domain/locationChangeObservable.js +17 -7
  135. package/src/domain/performanceCollection.js +55 -138
  136. package/src/domain/performanceObservable.js +148 -0
  137. package/src/domain/performanceUtils.js +16 -0
  138. package/src/domain/replay/record/observers.js +39 -35
  139. package/src/domain/requestCollection.js +6 -2
  140. package/src/domain/rumEventsCollection/actions/trackClickActions.js +4 -2
  141. package/src/domain/rumEventsCollection/longTask/longTaskCollection.js +7 -8
  142. package/src/domain/rumEventsCollection/resource/{matchRequestTiming.js → matchRequestResourceEntry.js} +10 -4
  143. package/src/domain/rumEventsCollection/resource/resourceCollection.js +74 -117
  144. package/src/domain/rumEventsCollection/resource/resourceUtils.js +13 -11
  145. package/src/domain/rumEventsCollection/resource/retrieveInitialDocumentResourceTiming.js +45 -0
  146. package/src/domain/rumEventsCollection/view/trackCommonViewMetrics.js +2 -1
  147. package/src/domain/rumEventsCollection/view/trackCumulativeLayoutShift.js +24 -7
  148. package/src/domain/rumEventsCollection/view/trackFirstContentfulPaint.js +3 -2
  149. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +1 -1
  150. package/src/domain/rumEventsCollection/view/trackFirstInput.js +3 -3
  151. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +1 -1
  152. package/src/domain/rumEventsCollection/view/trackInteractionToNextPaint.js +7 -19
  153. package/src/domain/rumEventsCollection/view/trackLargestContentfulPaint.js +4 -3
  154. package/src/domain/rumEventsCollection/view/trackLoadingTime.js +8 -8
  155. package/src/domain/rumEventsCollection/view/trackNavigationTimings.js +66 -32
  156. package/src/domain/rumEventsCollection/view/trackViewEventCounts.js +1 -24
  157. package/src/domain/rumEventsCollection/view/trackViews.js +1 -1
  158. package/src/domain/rumEventsCollection/view/viewCollection.js +4 -0
  159. package/src/domain/waitPageActivityEnd.js +16 -17
  160. package/cjs/domain/rumEventsCollection/resource/matchRequestTiming.js.map +0 -1
  161. package/esm/domain/rumEventsCollection/resource/matchRequestTiming.js.map +0 -1
package/src/boot/rum.js CHANGED
@@ -91,12 +91,14 @@ export function startRum(
91
91
  var userSession = startCacheUsrCache(configuration)
92
92
  var domMutationObservable = createDOMMutationObservable()
93
93
  var locationChangeObservable = createLocationChangeObservable(location)
94
+ var pageStateHistory = startPageStateHistory()
94
95
  var _startRumEventCollection = startRumEventCollection(
95
96
  lifeCycle,
96
97
  configuration,
97
98
  location,
98
99
  session,
99
100
  userSession,
101
+ pageStateHistory,
100
102
  locationChangeObservable,
101
103
  domMutationObservable,
102
104
  function () {
@@ -111,15 +113,18 @@ export function startRum(
111
113
  var viewContexts = _startRumEventCollection.viewContexts
112
114
  var urlContexts = _startRumEventCollection.urlContexts
113
115
  var actionContexts = _startRumEventCollection.actionContexts
114
- var pageStateHistory = _startRumEventCollection.pageStateHistory
115
116
  var stopRumEventCollection = _startRumEventCollection.stop
116
117
  var addAction = _startRumEventCollection.addAction
117
118
 
118
119
  cleanupTasks.push(stopRumEventCollection)
119
120
 
120
- startLongTaskCollection(lifeCycle, session)
121
- startResourceCollection(lifeCycle, configuration, session, pageStateHistory)
122
-
121
+ startLongTaskCollection(lifeCycle)
122
+ const _startResourceCollection = startResourceCollection(
123
+ lifeCycle,
124
+ configuration,
125
+ pageStateHistory
126
+ )
127
+ cleanupTasks.push(_startResourceCollection.stop)
123
128
  var _startViewCollection = startViewCollection(
124
129
  lifeCycle,
125
130
  configuration,
@@ -142,7 +147,11 @@ export function startRum(
142
147
  )
143
148
  var addError = _startErrorCollection.addError
144
149
  startRequestCollection(lifeCycle, configuration, session)
145
- startPerformanceCollection(lifeCycle, configuration)
150
+ const _startPerformanceCollection = startPerformanceCollection(
151
+ lifeCycle,
152
+ configuration
153
+ )
154
+ cleanupTasks.push(_startPerformanceCollection.stop)
146
155
  var internalContext = startInternalContext(
147
156
  configuration.applicationId,
148
157
  session,
@@ -187,6 +196,7 @@ export function startRumEventCollection(
187
196
  location,
188
197
  sessionManager,
189
198
  userSessionManager,
199
+ pageStateHistory,
190
200
  locationChangeObservable,
191
201
  domMutationObservable,
192
202
  buildCommonContext,
@@ -199,7 +209,6 @@ export function startRumEventCollection(
199
209
  location
200
210
  )
201
211
 
202
- var pageStateHistory = startPageStateHistory()
203
212
  var _startActionCollection = startActionCollection(
204
213
  lifeCycle,
205
214
  domMutationObservable,
@@ -229,10 +238,10 @@ export function startRumEventCollection(
229
238
  addAction: addAction,
230
239
  actionContexts: actionContexts,
231
240
  stop: function () {
232
- viewContexts.stop()
233
- urlContexts.stop()
234
- pageStateHistory.stop()
235
241
  displayContext.stop()
242
+ pageStateHistory.stop()
243
+ urlContexts.stop()
244
+ viewContexts.stop()
236
245
  }
237
246
  }
238
247
  }
@@ -150,7 +150,6 @@ export function startRumAssembly(
150
150
  : undefined,
151
151
  display: displayContext.get()
152
152
  }
153
-
154
153
  var rumEvent = extend2Lev(rumContext, viewContext, rawRumEvent)
155
154
  var serverRumEvent = withSnakeCaseKeys(rumEvent)
156
155
  var context = extend2Lev({}, commonContext.context, customerContext)
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  elapsed,
3
- ContextHistory,
3
+ createValueHistory,
4
4
  SESSION_TIME_OUT_DELAY,
5
5
  toServerDuration,
6
6
  addEventListeners,
@@ -27,10 +27,10 @@ export function startPageStateHistory(maxPageStateEntriesSelectable) {
27
27
  if (maxPageStateEntriesSelectable === undefined) {
28
28
  maxPageStateEntriesSelectable = MAX_PAGE_STATE_ENTRIES_SELECTABLE
29
29
  }
30
- var pageStateEntryHistory = new ContextHistory(
31
- PAGE_STATE_CONTEXT_TIME_OUT_DELAY,
32
- MAX_PAGE_STATE_ENTRIES
33
- )
30
+ var pageStateEntryHistory = createValueHistory({
31
+ expireDelay: PAGE_STATE_CONTEXT_TIME_OUT_DELAY,
32
+ maxEntries: MAX_PAGE_STATE_ENTRIES
33
+ })
34
34
 
35
35
  var currentPageState
36
36
  addPageState(getPageState(), relativeNow())
@@ -2,7 +2,7 @@ import {
2
2
  LifeCycleEventType,
3
3
  SESSION_TIME_OUT_DELAY,
4
4
  relativeNow,
5
- ContextHistory,
5
+ createValueHistory,
6
6
  replaceNumberCharByPath,
7
7
  getQueryParamsFromUrl,
8
8
  isHashAnAnchor,
@@ -22,7 +22,9 @@ export function startUrlContexts(
22
22
  locationChangeObservable,
23
23
  location
24
24
  ) {
25
- var urlContextHistory = new ContextHistory(URL_CONTEXT_TIME_OUT_DELAY)
25
+ var urlContextHistory = createValueHistory({
26
+ expireDelay: URL_CONTEXT_TIME_OUT_DELAY
27
+ })
26
28
 
27
29
  var previousViewUrl
28
30
 
@@ -1,13 +1,15 @@
1
1
  import {
2
2
  SESSION_TIME_OUT_DELAY,
3
- ContextHistory,
3
+ createValueHistory,
4
4
  LifeCycleEventType
5
5
  } from '@cloudcare/browser-core'
6
6
 
7
7
  export var VIEW_CONTEXT_TIME_OUT_DELAY = SESSION_TIME_OUT_DELAY
8
8
 
9
9
  export function startViewContexts(lifeCycle) {
10
- var viewContextHistory = new ContextHistory(VIEW_CONTEXT_TIME_OUT_DELAY)
10
+ var viewContextHistory = createValueHistory({
11
+ expireDelay: VIEW_CONTEXT_TIME_OUT_DELAY
12
+ })
11
13
 
12
14
  lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, function (view) {
13
15
  viewContextHistory.add(buildViewContext(view), view.startClocks.relative)
@@ -16,7 +18,12 @@ export function startViewContexts(lifeCycle) {
16
18
  lifeCycle.subscribe(LifeCycleEventType.AFTER_VIEW_ENDED, function (data) {
17
19
  viewContextHistory.closeActive(data.endClocks.relative)
18
20
  })
19
-
21
+ lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, function (viewUpdate) {
22
+ var currentView = viewContextHistory.find(viewUpdate.startClocks.relative)
23
+ if (currentView && viewUpdate.name) {
24
+ currentView.name = viewUpdate.name
25
+ }
26
+ })
20
27
  lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
21
28
  viewContextHistory.reset()
22
29
  })
@@ -3,7 +3,7 @@ import {
3
3
  DOM_EVENT,
4
4
  Observable,
5
5
  shallowClone,
6
- instrumentMethodAndCallOriginal
6
+ instrumentMethod
7
7
  } from '@cloudcare/browser-core'
8
8
 
9
9
  export function createLocationChangeObservable(location) {
@@ -31,12 +31,22 @@ export function createLocationChangeObservable(location) {
31
31
 
32
32
  function trackHistory(onHistoryChange) {
33
33
  // eslint-disable-next-line @typescript-eslint/unbound-method
34
- var pushState = instrumentMethodAndCallOriginal(history, 'pushState', {
35
- after: onHistoryChange
36
- })
37
- var replaceState = instrumentMethodAndCallOriginal(history, 'replaceState', {
38
- after: onHistoryChange
39
- })
34
+ var pushState = instrumentMethod(
35
+ History.prototype,
36
+ 'pushState',
37
+ function (params) {
38
+ var onPostCall = params.onPostCall
39
+ onPostCall(onHistoryChange)
40
+ }
41
+ )
42
+ var replaceState = instrumentMethod(
43
+ History.prototype,
44
+ 'replaceState',
45
+ function (params) {
46
+ var onPostCall = params.onPostCall
47
+ onPostCall(onHistoryChange)
48
+ }
49
+ )
40
50
  var popState = addEventListener(window, DOM_EVENT.POP_STATE, onHistoryChange)
41
51
 
42
52
  return {
@@ -1,70 +1,56 @@
1
1
  import {
2
2
  addEventListeners,
3
- getRelativeTime,
4
- isNumber,
5
- includes,
6
3
  each,
7
- extend,
8
- filter,
9
4
  dateNow,
10
5
  relativeNow,
11
6
  DOM_EVENT,
12
7
  LifeCycleEventType,
13
- runOnReadyState,
14
8
  setTimeout,
15
9
  addEventListener,
16
10
  monitor,
17
- assign
11
+ objectHasValue
18
12
  } from '@cloudcare/browser-core'
13
+
19
14
  import {
20
- FAKE_INITIAL_DOCUMENT,
21
- isAllowedRequestUrl
22
- } from './rumEventsCollection/resource/resourceUtils'
15
+ RumPerformanceEntryType,
16
+ supportPerformanceTimingEvent
17
+ } from './performanceObservable'
23
18
  function supportPerformanceObject() {
24
19
  return window.performance !== undefined && 'getEntries' in performance
25
20
  }
26
21
 
27
- export function supportPerformanceTimingEvent(entryType) {
28
- return (
29
- window.PerformanceObserver &&
30
- PerformanceObserver.supportedEntryTypes !== undefined &&
31
- includes(PerformanceObserver.supportedEntryTypes, entryType)
32
- )
33
- }
34
22
  export function startPerformanceCollection(lifeCycle, configuration) {
35
- retrieveInitialDocumentResourceTiming(function (timing) {
36
- handleRumPerformanceEntries(lifeCycle, configuration, [timing])
37
- })
38
-
23
+ var cleanupTasks = []
39
24
  if (supportPerformanceObject()) {
40
25
  var performanceEntries = performance.getEntries()
41
26
  // Because the performance entry list can be quite large
42
27
  // delay the computation to prevent the SDK from blocking the main thread on init
43
28
  setTimeout(function () {
44
- handleRumPerformanceEntries(lifeCycle, configuration, performanceEntries)
29
+ handleRumPerformanceEntries(lifeCycle, performanceEntries)
45
30
  })
46
31
  }
47
32
  if (window.PerformanceObserver) {
48
33
  var handlePerformanceEntryList = monitor(function (entries) {
49
- handleRumPerformanceEntries(
50
- lifeCycle,
51
- configuration,
52
- entries.getEntries()
53
- )
34
+ handleRumPerformanceEntries(lifeCycle, entries.getEntries())
54
35
  })
55
- var mainEntries = ['resource', 'navigation', 'longtask', 'paint']
36
+ var mainEntries = [
37
+ RumPerformanceEntryType.LONG_TASK,
38
+ RumPerformanceEntryType.PAINT
39
+ ]
56
40
  var experimentalEntries = [
57
- 'largest-contentful-paint',
58
- 'first-input',
59
- 'layout-shift',
60
- 'event'
41
+ RumPerformanceEntryType.LARGEST_CONTENTFUL_PAINT,
42
+ RumPerformanceEntryType.FIRST_INPUT,
43
+ RumPerformanceEntryType.LAYOUT_SHIFT,
44
+ RumPerformanceEntryType.EVENT
61
45
  ]
62
46
  try {
63
47
  // Experimental entries are not retrieved by performance.getEntries()
64
48
  // use a single PerformanceObserver with buffered flag by type
65
49
  // to get values that could happen before SDK init
66
50
  each(experimentalEntries, function (type) {
67
- var observer = new PerformanceObserver(handlePerformanceEntryList)
51
+ var observer = new window.PerformanceObserver(
52
+ handlePerformanceEntryList
53
+ )
68
54
  observer.observe({
69
55
  type: type,
70
56
  buffered: true,
@@ -72,6 +58,9 @@ export function startPerformanceCollection(lifeCycle, configuration) {
72
58
  // cf: https://github.com/GoogleChrome/web-vitals/blob/3806160ffbc93c3c4abf210a167b81228172b31c/src/onINP.ts#L209
73
59
  durationThreshold: 40
74
60
  })
61
+ cleanupTasks.push(function () {
62
+ observer.disconnect()
63
+ })
75
64
  })
76
65
  } catch (e) {
77
66
  // Some old browser versions (ex: chrome 67) don't support the PerformanceObserver type and buffered options
@@ -81,72 +70,39 @@ export function startPerformanceCollection(lifeCycle, configuration) {
81
70
  })
82
71
  }
83
72
  var mainObserver = new PerformanceObserver(handlePerformanceEntryList)
84
- mainObserver.observe({ entryTypes: mainEntries })
73
+ // mainObserver.observe({ entryTypes: mainEntries })
74
+ try {
75
+ mainObserver.observe({ entryTypes: mainEntries })
76
+ cleanupTasks.push(function () {
77
+ mainObserver.disconnect()
78
+ })
79
+ } catch {}
85
80
  if (supportPerformanceObject() && 'addEventListener' in performance) {
86
81
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1559377
87
- addEventListener(performance, 'resourcetimingbufferfull', function () {
88
- performance.clearResourceTimings()
89
- })
82
+ var resourncetimingbufferfullListener = addEventListener(
83
+ performance,
84
+ 'resourcetimingbufferfull',
85
+ function () {
86
+ performance.clearResourceTimings()
87
+ }
88
+ )
89
+ cleanupTasks.push(resourncetimingbufferfullListener.stop)
90
90
  }
91
91
  }
92
- if (!supportPerformanceTimingEvent('navigation')) {
93
- retrieveNavigationTiming(function (timing) {
94
- handleRumPerformanceEntries(lifeCycle, configuration, [timing])
95
- })
96
- }
97
- if (!supportPerformanceTimingEvent('first-input')) {
98
- retrieveFirstInputTiming(function (timing) {
99
- return handleRumPerformanceEntries(lifeCycle, configuration, [timing])
92
+
93
+ if (!supportPerformanceTimingEvent(RumPerformanceEntryType.FIRST_INPUT)) {
94
+ var _retrieveFirstInputTiming = retrieveFirstInputTiming(function (timing) {
95
+ return handleRumPerformanceEntries(lifeCycle, [timing])
100
96
  })
97
+ cleanupTasks.push(_retrieveFirstInputTiming.stop)
101
98
  }
102
- }
103
-
104
- export function retrieveInitialDocumentResourceTiming(callback) {
105
- runOnReadyState('interactive', function () {
106
- var timing
107
-
108
- var forcedAttributes = {
109
- entryType: 'resource',
110
- initiatorType: FAKE_INITIAL_DOCUMENT,
111
- traceId: '',
112
- toJSON: function () {
113
- return assign({}, timing, { toJSON: undefined })
114
- }
115
- }
116
- if (
117
- supportPerformanceTimingEvent('navigation') &&
118
- performance.getEntriesByType('navigation').length > 0
119
- ) {
120
- var navigationEntry = performance.getEntriesByType('navigation')[0]
121
- timing = extend(navigationEntry.toJSON(), forcedAttributes)
122
- } else {
123
- var relativePerformanceTiming = computeRelativePerformanceTiming()
124
- timing = extend(
125
- relativePerformanceTiming,
126
- {
127
- decodedBodySize: 0,
128
- duration: relativePerformanceTiming.responseEnd,
129
- name: window.location.href,
130
- startTime: 0
131
- },
132
- forcedAttributes
133
- )
99
+ return {
100
+ stop: function () {
101
+ each(cleanupTasks, function (task) {
102
+ return task()
103
+ })
134
104
  }
135
- callback(timing)
136
- })
137
- }
138
-
139
- function retrieveNavigationTiming(callback) {
140
- function sendFakeTiming() {
141
- callback(
142
- extend(computeRelativePerformanceTiming(), { entryType: 'navigation' })
143
- )
144
105
  }
145
-
146
- runOnReadyState('complete', function () {
147
- // Send it a bit after the actual load event, so the "loadEventEnd" timing is accurate
148
- setTimeout(sendFakeTiming)
149
- })
150
106
  }
151
107
 
152
108
  /**
@@ -176,7 +132,7 @@ function retrieveFirstInputTiming(callback) {
176
132
  // when the system received the event (e.g. evt.timeStamp) and when it could run the callback
177
133
  // (e.g. performance.now()).
178
134
  var timing = {
179
- entryType: 'first-input',
135
+ entryType: RumPerformanceEntryType.FIRST_INPUT,
180
136
  processingStart: relativeNow(),
181
137
  processingEnd: relativeNow(),
182
138
  startTime: evt.timeStamp,
@@ -193,7 +149,6 @@ function retrieveFirstInputTiming(callback) {
193
149
  { passive: true, capture: true }
194
150
  )
195
151
  var removeEventListeners = listeners.stop
196
-
197
152
  /**
198
153
  * Pointer events are a special case, because they can trigger main or compositor thread behavior.
199
154
  * We differenciate these cases based on whether or not we see a pointercancel event, which are
@@ -228,58 +183,20 @@ function retrieveFirstInputTiming(callback) {
228
183
  }
229
184
  }
230
185
  }
231
- }
232
-
233
- function computeRelativePerformanceTiming() {
234
- var result = {}
235
- var timing = performance.timing
236
- for (var key in timing) {
237
- if (isNumber(timing[key])) {
238
- result[key] = timing[key] === 0 ? 0 : getRelativeTime(timing[key])
239
- }
186
+ return {
187
+ stop: removeEventListeners
240
188
  }
241
- return result
242
189
  }
243
190
 
244
- function handleRumPerformanceEntries(lifeCycle, configuration, entries) {
245
- var rumPerformanceEntries = filter(entries, function (entry) {
246
- return (
247
- entry.entryType === 'resource' ||
248
- entry.entryType === 'navigation' ||
249
- entry.entryType === 'paint' ||
250
- entry.entryType === 'longtask' ||
251
- entry.entryType === 'largest-contentful-paint' ||
252
- entry.entryType === 'first-input' ||
253
- entry.entryType === 'layout-shift' ||
254
- entry.entryType === 'event'
255
- )
191
+ function handleRumPerformanceEntries(lifeCycle, entries) {
192
+ var rumPerformanceEntries = entries.filter(function (entry) {
193
+ return objectHasValue(RumPerformanceEntryType, entry.entryType)
256
194
  })
257
195
 
258
- var rumAllowedPerformanceEntries = filter(
259
- rumPerformanceEntries,
260
- function (entry) {
261
- return (
262
- !isIncompleteNavigation(entry) &&
263
- !isForbiddenResource(configuration, entry)
264
- )
265
- }
266
- )
267
-
268
- if (rumAllowedPerformanceEntries.length) {
196
+ if (rumPerformanceEntries.length) {
269
197
  lifeCycle.notify(
270
198
  LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED,
271
- rumAllowedPerformanceEntries
199
+ rumPerformanceEntries
272
200
  )
273
201
  }
274
202
  }
275
-
276
- function isIncompleteNavigation(entry) {
277
- return entry.entryType === 'navigation' && entry.loadEventEnd <= 0
278
- }
279
-
280
- function isForbiddenResource(configuration, entry) {
281
- return (
282
- entry.entryType === 'resource' &&
283
- !isAllowedRequestUrl(configuration, entry.name)
284
- )
285
- }
@@ -0,0 +1,148 @@
1
+ import {
2
+ addEventListener,
3
+ Observable,
4
+ setTimeout,
5
+ clearTimeout,
6
+ monitor,
7
+ includes
8
+ } from '@cloudcare/browser-core'
9
+ import {
10
+ isAllowedRequestUrl,
11
+ hasValidResourceEntryDuration
12
+ } from './rumEventsCollection/resource/resourceUtils'
13
+
14
+ // We want to use a real enum (i.e. not a const enum) here, to be able to check whether an arbitrary
15
+ // string is an expected performance entry
16
+ // eslint-disable-next-line no-restricted-syntax
17
+ export var RumPerformanceEntryType = {
18
+ EVENT: 'event',
19
+ FIRST_INPUT: 'first-input',
20
+ LARGEST_CONTENTFUL_PAINT: 'largest-contentful-paint',
21
+ LAYOUT_SHIFT: 'layout-shift',
22
+ LONG_TASK: 'longtask',
23
+ LONG_ANIMATION_FRAME: 'long-animation-frame',
24
+ NAVIGATION: 'navigation',
25
+ PAINT: 'paint',
26
+ RESOURCE: 'resource'
27
+ }
28
+
29
+ export function createPerformanceObservable(configuration, options) {
30
+ return new Observable(function (observable) {
31
+ if (!window.PerformanceObserver) {
32
+ return
33
+ }
34
+
35
+ var handlePerformanceEntries = function (entries) {
36
+ var rumPerformanceEntries = filterRumPerformanceEntries(
37
+ configuration,
38
+ entries
39
+ )
40
+ if (rumPerformanceEntries.length > 0) {
41
+ observable.notify(rumPerformanceEntries)
42
+ }
43
+ }
44
+
45
+ var timeoutId
46
+ var isObserverInitializing = true
47
+ const observer = new PerformanceObserver(
48
+ monitor(function (entries) {
49
+ // In Safari the performance observer callback is synchronous.
50
+ // Because the buffered performance entry list can be quite large we delay the computation to prevent the SDK from blocking the main thread on init
51
+ if (isObserverInitializing) {
52
+ timeoutId = setTimeout(function () {
53
+ handlePerformanceEntries(entries.getEntries())
54
+ })
55
+ } else {
56
+ handlePerformanceEntries(entries.getEntries())
57
+ }
58
+ })
59
+ )
60
+ try {
61
+ observer.observe(options)
62
+ } catch {
63
+ // Some old browser versions (<= chrome 74 ) don't support the PerformanceObserver type and buffered options
64
+ // In these cases, fallback to getEntriesByType and PerformanceObserver with entryTypes
65
+ // TODO: remove this fallback in the next major version
66
+ var fallbackSupportedEntryTypes = [
67
+ RumPerformanceEntryType.RESOURCE,
68
+ RumPerformanceEntryType.NAVIGATION,
69
+ RumPerformanceEntryType.LONG_TASK,
70
+ RumPerformanceEntryType.PAINT
71
+ ]
72
+ if (includes(fallbackSupportedEntryTypes, options.type)) {
73
+ if (options.buffered) {
74
+ timeoutId = setTimeout(function () {
75
+ handlePerformanceEntries(performance.getEntriesByType(options.type))
76
+ })
77
+ }
78
+ try {
79
+ observer.observe({ entryTypes: [options.type] })
80
+ } catch {
81
+ // Old versions of Safari are throwing "entryTypes contained only unsupported types"
82
+ // errors when observing only unsupported entry types.
83
+ //
84
+ // We could use `supportPerformanceTimingEvent` to make sure we don't invoke
85
+ // `observer.observe` with an unsupported entry type, but Safari 11 and 12 don't support
86
+ // `Performance.supportedEntryTypes`, so doing so would lose support for these versions
87
+ // even if they do support the entry type.
88
+ return
89
+ }
90
+ }
91
+ }
92
+ isObserverInitializing = false
93
+
94
+ manageResourceTimingBufferFull(configuration)
95
+
96
+ return function () {
97
+ observer.disconnect()
98
+ clearTimeout(timeoutId)
99
+ }
100
+ })
101
+ }
102
+
103
+ var resourceTimingBufferFullListener
104
+ function manageResourceTimingBufferFull(configuration) {
105
+ if (
106
+ !resourceTimingBufferFullListener &&
107
+ supportPerformanceObject() &&
108
+ 'addEventListener' in performance
109
+ ) {
110
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1559377
111
+ resourceTimingBufferFullListener = addEventListener(
112
+ performance,
113
+ 'resourcetimingbufferfull',
114
+ function () {
115
+ performance.clearResourceTimings()
116
+ }
117
+ )
118
+ }
119
+ return function () {
120
+ resourceTimingBufferFullListener && resourceTimingBufferFullListener.stop()
121
+ }
122
+ }
123
+
124
+ function supportPerformanceObject() {
125
+ return window.performance !== undefined && 'getEntries' in performance
126
+ }
127
+
128
+ export function supportPerformanceTimingEvent(entryType) {
129
+ return (
130
+ window.PerformanceObserver &&
131
+ PerformanceObserver.supportedEntryTypes !== undefined &&
132
+ PerformanceObserver.supportedEntryTypes.includes(entryType)
133
+ )
134
+ }
135
+
136
+ function filterRumPerformanceEntries(configuration, entries) {
137
+ return entries.filter(function (entry) {
138
+ return !isForbiddenResource(configuration, entry)
139
+ })
140
+ }
141
+
142
+ function isForbiddenResource(configuration, entry) {
143
+ return (
144
+ entry.entryType === RumPerformanceEntryType.RESOURCE &&
145
+ (!isAllowedRequestUrl(configuration, entry.name) ||
146
+ !hasValidResourceEntryDuration(entry))
147
+ )
148
+ }
@@ -0,0 +1,16 @@
1
+ import { getRelativeTime, isNumber } from '@cloudcare/browser-core'
2
+
3
+ export function computeRelativePerformanceTiming() {
4
+ var result = {}
5
+ var timing = performance.timing
6
+
7
+ for (var key in timing) {
8
+ if (isNumber(timing[key])) {
9
+ var numberKey = key
10
+ var timingElement = timing[numberKey]
11
+ result[numberKey] =
12
+ timingElement === 0 ? 0 : getRelativeTime(timingElement)
13
+ }
14
+ }
15
+ return result
16
+ }