@cloudcare/browser-rum 2.1.5 → 2.2.0

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 (135) hide show
  1. package/README.md +1 -2
  2. package/bundle/dataflux-rum.js +1 -1
  3. package/cjs/boot/buildEnv.js +1 -1
  4. package/cjs/boot/rum.js +57 -48
  5. package/cjs/boot/rumPublicApi.js +169 -134
  6. package/cjs/domain/assembly.js +58 -44
  7. package/cjs/domain/configuration.js +67 -0
  8. package/cjs/domain/contexts/internalContext.js +45 -0
  9. package/cjs/domain/contexts/urlContexts.js +68 -0
  10. package/cjs/domain/contexts/viewContexts.js +43 -0
  11. package/cjs/domain/domMutationCollection.js +44 -31
  12. package/cjs/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +25 -25
  13. package/cjs/domain/performanceCollection.js +55 -24
  14. package/cjs/domain/requestCollection.js +117 -0
  15. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +22 -4
  16. package/cjs/domain/rumEventsCollection/actions/clickChain.js +82 -0
  17. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +78 -0
  18. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  19. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +163 -0
  20. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +74 -0
  21. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +263 -0
  22. package/cjs/domain/rumEventsCollection/error/errorCollection.js +33 -53
  23. package/cjs/domain/rumEventsCollection/error/trackConsoleError.js +26 -0
  24. package/cjs/domain/rumEventsCollection/error/trackReportError.js +26 -0
  25. package/cjs/domain/rumEventsCollection/longTask/longTaskCollection.js +46 -17
  26. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +49 -9
  27. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js +7 -5
  28. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js +61 -24
  29. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +91 -51
  30. package/cjs/domain/rumEventsCollection/view/trackViews.js +119 -86
  31. package/cjs/domain/rumEventsCollection/view/viewCollection.js +41 -31
  32. package/cjs/domain/rumSessionManager.js +95 -0
  33. package/cjs/domain/tracing/tracer.js +9 -10
  34. package/cjs/domain/trackEventCounts.js +9 -6
  35. package/cjs/domain/usr/index.js +30 -0
  36. package/cjs/domain/waitPageActivityEnd.js +163 -0
  37. package/cjs/transport/startRumBatch.js +38 -0
  38. package/esm/boot/buildEnv.js +1 -1
  39. package/esm/boot/rum.js +55 -48
  40. package/esm/boot/rumPublicApi.js +167 -135
  41. package/esm/domain/assembly.js +58 -44
  42. package/esm/domain/configuration.js +53 -0
  43. package/esm/domain/contexts/internalContext.js +38 -0
  44. package/esm/domain/contexts/urlContexts.js +58 -0
  45. package/esm/domain/contexts/viewContexts.js +32 -0
  46. package/esm/domain/domMutationCollection.js +43 -33
  47. package/esm/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +26 -24
  48. package/esm/domain/performanceCollection.js +56 -26
  49. package/esm/domain/requestCollection.js +102 -0
  50. package/esm/domain/rumEventsCollection/actions/actionCollection.js +23 -5
  51. package/esm/domain/rumEventsCollection/actions/clickChain.js +69 -0
  52. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +64 -0
  53. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  54. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +139 -0
  55. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +66 -0
  56. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +244 -0
  57. package/esm/domain/rumEventsCollection/error/errorCollection.js +32 -54
  58. package/esm/domain/rumEventsCollection/error/trackConsoleError.js +18 -0
  59. package/esm/domain/rumEventsCollection/error/trackReportError.js +18 -0
  60. package/esm/domain/rumEventsCollection/longTask/longTaskCollection.js +30 -18
  61. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +31 -9
  62. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js +8 -6
  63. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js +43 -26
  64. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +75 -51
  65. package/esm/domain/rumEventsCollection/view/trackViews.js +120 -85
  66. package/esm/domain/rumEventsCollection/view/viewCollection.js +42 -32
  67. package/esm/domain/rumSessionManager.js +80 -0
  68. package/esm/domain/tracing/skywalkingTracer.js +1 -1
  69. package/esm/domain/tracing/tracer.js +8 -9
  70. package/esm/domain/trackEventCounts.js +9 -6
  71. package/esm/domain/usr/index.js +17 -0
  72. package/esm/domain/waitPageActivityEnd.js +147 -0
  73. package/esm/transport/startRumBatch.js +30 -0
  74. package/package.json +3 -3
  75. package/src/boot/rum.js +79 -59
  76. package/src/boot/rumPublicApi.js +165 -171
  77. package/src/domain/.DS_Store +0 -0
  78. package/src/domain/assembly.js +77 -54
  79. package/src/domain/configuration.js +69 -0
  80. package/src/domain/contexts/internalContext.js +39 -0
  81. package/src/domain/contexts/urlContexts.js +76 -0
  82. package/src/domain/contexts/viewContexts.js +37 -0
  83. package/src/domain/domMutationCollection.js +48 -32
  84. package/src/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +28 -28
  85. package/src/domain/performanceCollection.js +62 -45
  86. package/src/domain/requestCollection.js +103 -0
  87. package/src/domain/rumEventsCollection/actions/actionCollection.js +34 -11
  88. package/src/domain/rumEventsCollection/actions/clickChain.js +90 -0
  89. package/src/domain/rumEventsCollection/actions/computeFrustration.js +84 -0
  90. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +73 -62
  91. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +157 -0
  92. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +85 -0
  93. package/src/domain/rumEventsCollection/actions/trackClickActions.js +326 -0
  94. package/src/domain/rumEventsCollection/error/errorCollection.js +53 -69
  95. package/src/domain/rumEventsCollection/error/trackConsoleError.js +21 -0
  96. package/src/domain/rumEventsCollection/error/trackReportError.js +22 -0
  97. package/src/domain/rumEventsCollection/longTask/longTaskCollection.js +29 -18
  98. package/src/domain/rumEventsCollection/resource/resourceCollection.js +39 -23
  99. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +9 -6
  100. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +63 -42
  101. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +80 -61
  102. package/src/domain/rumEventsCollection/view/trackViews.js +171 -134
  103. package/src/domain/rumEventsCollection/view/viewCollection.js +69 -49
  104. package/src/domain/rumSessionManager.js +90 -0
  105. package/src/domain/tracing/jaegerTracer.js +5 -5
  106. package/src/domain/tracing/skywalkingTracer.js +3 -3
  107. package/src/domain/tracing/tracer.js +50 -38
  108. package/src/domain/tracing/w3cTraceParentTracer.js +5 -5
  109. package/src/domain/tracing/zipkinMultiTracer.js +5 -5
  110. package/src/domain/tracing/zipkinSingleTracer.js +5 -5
  111. package/src/domain/trackEventCounts.js +10 -6
  112. package/src/domain/usr/index.js +16 -0
  113. package/src/domain/waitPageActivityEnd.js +192 -0
  114. package/src/transport/startRumBatch.js +48 -0
  115. package/cjs/domain/internalContext.js +0 -34
  116. package/cjs/domain/parentContexts.js +0 -143
  117. package/cjs/domain/requestCollections.js +0 -131
  118. package/cjs/domain/rumEventsCollection/actions/trackActions.js +0 -114
  119. package/cjs/domain/rumSession.js +0 -82
  120. package/cjs/domain/trackPageActivities.js +0 -166
  121. package/cjs/transport/batch.js +0 -81
  122. package/esm/domain/internalContext.js +0 -27
  123. package/esm/domain/parentContexts.js +0 -130
  124. package/esm/domain/requestCollections.js +0 -118
  125. package/esm/domain/rumEventsCollection/actions/trackActions.js +0 -103
  126. package/esm/domain/rumSession.js +0 -70
  127. package/esm/domain/trackPageActivities.js +0 -149
  128. package/esm/transport/batch.js +0 -74
  129. package/src/domain/internalContext.js +0 -28
  130. package/src/domain/parentContexts.js +0 -171
  131. package/src/domain/requestCollections.js +0 -118
  132. package/src/domain/rumEventsCollection/actions/trackActions.js +0 -113
  133. package/src/domain/rumSession.js +0 -92
  134. package/src/domain/trackPageActivities.js +0 -170
  135. package/src/transport/batch.js +0 -85
@@ -1,6 +1,7 @@
1
1
  import {
2
+ shallowClone,
3
+ assign,
2
4
  elapsed,
3
- extend,
4
5
  UUID,
5
6
  ONE_MINUTE,
6
7
  throttle,
@@ -8,218 +9,238 @@ import {
8
9
  clocksOrigin,
9
10
  timeStampNow,
10
11
  looksLikeRelativeTime,
12
+ ViewLoadingType,
11
13
  LifeCycleEventType
12
14
  } from '@cloudcare/browser-core'
15
+
13
16
  import { trackInitialViewTimings } from './trackInitialViewTimings'
14
- import {
15
- trackLocationChanges,
16
- areDifferentLocation
17
- } from './trackLocationChanges'
18
17
  import { trackViewMetrics } from './trackViewMetrics'
19
18
 
20
19
  export var THROTTLE_VIEW_UPDATE_PERIOD = 3000
21
20
  export var SESSION_KEEP_ALIVE_INTERVAL = 5 * ONE_MINUTE
22
- export var ViewLoadingType = {
23
- INITIAL_LOAD: 'initial_load',
24
- ROUTE_CHANGE: 'route_change'
25
- }
26
- export function trackViews(location, lifeCycle) {
27
- var isRecording = false
28
- // eslint-disable-next-line prefer-const
29
- var _trackInitialView = trackInitialView()
21
+
22
+
23
+ export function trackViews(
24
+ location,
25
+ lifeCycle,
26
+ domMutationObservable,
27
+ configuration,
28
+ locationChangeObservable,
29
+ areViewsTrackedAutomatically,
30
+ initialViewOptions
31
+ ) {
32
+ var _trackInitialView = trackInitialView(initialViewOptions)
30
33
  var stopInitialViewTracking = _trackInitialView.stop
31
- var currentView = _trackInitialView.initialView
32
- var _trackLocationChanges = trackLocationChanges(function () {
33
- if (areDifferentLocation(currentView.getLocation(), location)) {
34
- // Renew view on location changes
35
- currentView.end()
36
- currentView.triggerUpdate()
37
- currentView = trackViewChange()
38
- return
39
- }
40
- currentView.updateLocation(location)
41
- currentView.triggerUpdate()
42
- })
43
- var stopLocationChangesTracking = _trackLocationChanges.stop
44
- // Renew view on session renewal
45
- lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
46
- // do not trigger view update to avoid wrong data
47
- currentView.end()
48
- currentView = trackViewChange()
49
- })
50
-
51
- // End the current view on page unload
52
- lifeCycle.subscribe(LifeCycleEventType.BEFORE_UNLOAD, function () {
53
- currentView.end()
54
- currentView.triggerUpdate()
55
- })
56
-
57
- lifeCycle.subscribe(LifeCycleEventType.RECORD_STARTED, function () {
58
- isRecording = true
59
- currentView.updateHasReplay(true)
60
- })
61
-
62
- lifeCycle.subscribe(LifeCycleEventType.RECORD_STOPPED, function () {
63
- isRecording = false
64
- })
65
-
66
- // Session keep alive
67
- var keepAliveInterval = window.setInterval(function () {
68
- currentView.triggerUpdate()
69
- }, SESSION_KEEP_ALIVE_INTERVAL)
70
-
71
- function trackInitialView() {
34
+ var initialView = _trackInitialView.initialView
35
+ var currentView = initialView
36
+
37
+ var _startViewLifeCycle= startViewLifeCycle()
38
+ var stopViewLifeCycle = _startViewLifeCycle.stop
39
+ var locationChangeSubscription
40
+ if (areViewsTrackedAutomatically) {
41
+ locationChangeSubscription = renewViewOnLocationChange(locationChangeObservable)
42
+ }
43
+
44
+ function trackInitialView(options) {
72
45
  var initialView = newView(
73
46
  lifeCycle,
47
+ domMutationObservable,
48
+ configuration,
74
49
  location,
75
- isRecording,
76
50
  ViewLoadingType.INITIAL_LOAD,
77
- document.referrer,
78
- clocksOrigin()
79
- )
80
- var _trackInitialViewTimings = trackInitialViewTimings(
81
- lifeCycle,
82
- function (timings) {
83
- initialView.updateTimings(timings)
84
- initialView.scheduleUpdate()
85
- }
51
+ clocksOrigin(),
52
+ options
86
53
  )
87
- var stop = _trackInitialViewTimings.stop
88
- return { initialView: initialView, stop: stop }
54
+ var _trackInitialViewTimings = trackInitialViewTimings(lifeCycle, function(timings) {
55
+ initialView.updateTimings(timings)
56
+ initialView.scheduleUpdate()
57
+ })
58
+ return { initialView: initialView, stop: _trackInitialViewTimings.stop }
89
59
  }
90
60
 
91
- function trackViewChange() {
61
+ function trackViewChange(startClocks, viewOptions) {
92
62
  return newView(
93
63
  lifeCycle,
64
+ domMutationObservable,
65
+ configuration,
94
66
  location,
95
- isRecording,
96
67
  ViewLoadingType.ROUTE_CHANGE,
97
- currentView.url
68
+ startClocks,
69
+ viewOptions
98
70
  )
99
71
  }
100
72
 
73
+ function startViewLifeCycle() {
74
+ lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function() {
75
+ // do not trigger view update to avoid wrong data
76
+ currentView.end()
77
+ // Renew view on session renewal
78
+ currentView = trackViewChange(undefined, {
79
+ name: currentView.name,
80
+ service: currentView.service,
81
+ version: currentView.version,
82
+ })
83
+ })
84
+
85
+ // End the current view on page unload
86
+ lifeCycle.subscribe(LifeCycleEventType.BEFORE_UNLOAD, function() {
87
+ currentView.end()
88
+ currentView.triggerUpdate()
89
+ })
90
+
91
+ // Session keep alive
92
+ var keepAliveInterval = window.setInterval(
93
+ function() {
94
+ currentView.triggerUpdate()
95
+ },
96
+ SESSION_KEEP_ALIVE_INTERVAL
97
+ )
98
+
99
+ return {
100
+ stop: function() {
101
+ clearInterval(keepAliveInterval)
102
+ },
103
+ }
104
+ }
105
+
106
+ function renewViewOnLocationChange(locationChangeObservable) {
107
+ return locationChangeObservable.subscribe(function (params){
108
+ var oldLocation = params.oldLocation
109
+ var newLocation = params.newLocation
110
+ if (areDifferentLocation(oldLocation, newLocation)) {
111
+ currentView.end()
112
+ currentView.triggerUpdate()
113
+ currentView = trackViewChange()
114
+ return
115
+ }
116
+ })
117
+ }
118
+
101
119
  return {
102
- addTiming: function (name, time) {
120
+ addTiming: function(name, time) {
103
121
  if (typeof time === 'undefined') {
104
122
  time = timeStampNow()
105
123
  }
106
124
  currentView.addTiming(name, time)
125
+ currentView.scheduleUpdate()
126
+ },
127
+ startView: function(options, startClocks){
128
+ currentView.end(startClocks)
107
129
  currentView.triggerUpdate()
130
+ currentView = trackViewChange(startClocks, options)
108
131
  },
109
- stop: function () {
132
+ stop: function(){
133
+ if (locationChangeSubscription) {
134
+ locationChangeSubscription.unsubscribe()
135
+ }
110
136
  stopInitialViewTracking()
111
- stopLocationChangesTracking()
137
+ stopViewLifeCycle()
112
138
  currentView.end()
113
- clearInterval(keepAliveInterval)
114
- }
139
+ },
115
140
  }
116
141
  }
117
142
 
118
143
  function newView(
119
144
  lifeCycle,
145
+ domMutationObservable,
146
+ configuration,
120
147
  initialLocation,
121
- initialHasReplay,
122
148
  loadingType,
123
- referrer,
124
149
  startClocks,
125
- name
150
+ viewOptions
126
151
  ) {
152
+ // Setup initial values
127
153
  if (typeof startClocks === 'undefined') {
128
154
  startClocks = clocksNow()
129
155
  }
130
- // Setup initial values
131
156
  var id = UUID()
132
157
  var timings = {}
133
158
  var customTimings = {}
134
159
  var documentVersion = 0
135
160
  var endClocks
136
- var location = extend({}, initialLocation)
137
- var hasReplay = initialHasReplay
138
-
139
- lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, {
140
- id: id,
141
- startClocks: startClocks,
142
- location: location,
143
- referrer: referrer
144
- })
161
+ var location = shallowClone(initialLocation)
162
+
163
+ var name
164
+ var service
165
+ var version
166
+ if (viewOptions) {
167
+ name = viewOptions.name
168
+ service = viewOptions.service
169
+ version = viewOptions.version
170
+ }
171
+
172
+ lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, { id: id, name: name, startClocks: startClocks, service: service, version:version })
173
+
174
+ // Update the view every time the measures are changing
145
175
  var scheduleViewUpdate = throttle(
146
176
  triggerViewUpdate,
147
177
  THROTTLE_VIEW_UPDATE_PERIOD,
148
178
  {
149
- leading: false
179
+ leading: false,
150
180
  }
151
181
  )
152
- // Update the view every time the measures are changing
153
182
  var cancelScheduleViewUpdate = scheduleViewUpdate.cancel
154
-
155
- var _trackViewMetrics = trackViewMetrics(
156
- lifeCycle,
157
- scheduleViewUpdate,
158
- loadingType
159
- )
160
-
183
+
184
+ var _trackViewMetrics = trackViewMetrics(lifeCycle, domMutationObservable, configuration, scheduleViewUpdate, loadingType, startClocks)
161
185
  var setLoadEvent = _trackViewMetrics.setLoadEvent
162
- var viewMetrics = _trackViewMetrics.viewMetrics
163
186
  var stopViewMetricsTracking = _trackViewMetrics.stop
187
+ var viewMetrics = _trackViewMetrics.viewMetrics
164
188
  // Initial view update
165
189
  triggerViewUpdate()
190
+
166
191
  function triggerViewUpdate() {
167
192
  documentVersion += 1
168
193
  var currentEnd = endClocks === undefined ? timeStampNow() : endClocks.timeStamp
169
194
  lifeCycle.notify(
170
195
  LifeCycleEventType.VIEW_UPDATED,
171
- extend({}, viewMetrics, {
172
- customTimings: customTimings,
173
- documentVersion: documentVersion,
174
- id: id,
175
- name: name,
176
- loadingType: loadingType,
177
- location: location,
178
- hasReplay: hasReplay,
179
- referrer: referrer,
180
- startClocks: startClocks,
181
- timings: timings,
182
- duration: elapsed(
183
- startClocks.timeStamp, currentEnd
184
- ),
185
- isActive: endClocks === undefined
186
- })
196
+ assign(
197
+ {
198
+ customTimings:customTimings,
199
+ documentVersion:documentVersion,
200
+ id:id,
201
+ name:name,
202
+ service:service,
203
+ version:version,
204
+ loadingType:loadingType,
205
+ location:location,
206
+ startClocks:startClocks,
207
+ timings:timings,
208
+ duration: elapsed(startClocks.timeStamp, currentEnd),
209
+ isActive: endClocks === undefined,
210
+ },
211
+ viewMetrics
212
+ )
187
213
  )
188
214
  }
189
215
 
190
216
  return {
217
+ name:name,
218
+ service:service,
219
+ version:version,
191
220
  scheduleUpdate: scheduleViewUpdate,
192
- end: function () {
193
- endClocks = clocksNow()
221
+ end: function(clocks) {
222
+ if (typeof clocks === 'undefined') {
223
+ clocks = clocksNow()
224
+ }
225
+ endClocks = clocks
226
+ lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks })
194
227
  stopViewMetricsTracking()
195
- lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, { endClocks: endClocks })
196
228
  },
197
- getLocation: function () {
198
- return location
199
- },
200
- triggerUpdate: function () {
229
+ triggerUpdate: function() {
201
230
  // cancel any pending view updates execution
202
231
  cancelScheduleViewUpdate()
203
232
  triggerViewUpdate()
204
233
  },
205
- updateTimings: function (newTimings) {
234
+ updateTimings: function(newTimings) {
206
235
  timings = newTimings
207
236
  if (newTimings.loadEvent !== undefined) {
208
237
  setLoadEvent(newTimings.loadEvent)
209
238
  }
210
239
  },
211
- addTiming: function (name, time) {
212
- const relativeTime = looksLikeRelativeTime(time) ? time : elapsed(startClocks.timeStamp, time)
240
+ addTiming: function(name, time) {
241
+ var relativeTime = looksLikeRelativeTime(time) ? time : elapsed(startClocks.timeStamp, time)
213
242
  customTimings[sanitizeTiming(name)] = relativeTime
214
-
215
- },
216
- updateLocation: function (newLocation) {
217
- location = extend({}, newLocation)
218
243
  },
219
- updateHasReplay: function (newHasReplay) {
220
- hasReplay = newHasReplay
221
- },
222
- url: location.href
223
244
  }
224
245
  }
225
246
 
@@ -229,9 +250,25 @@ function newView(
229
250
  function sanitizeTiming(name) {
230
251
  var sanitized = name.replace(/[^a-zA-Z0-9-_.@$]/g, '_')
231
252
  if (sanitized !== name) {
232
- console.warn(
233
- 'Invalid timing name: ' + name + ', sanitized to: ' + sanitized
234
- )
253
+ console.warn('Invalid timing name: ' + name + ', sanitized to: '+ sanitized)
235
254
  }
236
255
  return sanitized
237
256
  }
257
+
258
+ function areDifferentLocation(currentLocation, otherLocation) {
259
+ return (
260
+ currentLocation.pathname !== otherLocation.pathname ||
261
+ (!isHashAnAnchor(otherLocation.hash) &&
262
+ getPathFromHash(otherLocation.hash) !== getPathFromHash(currentLocation.hash))
263
+ )
264
+ }
265
+
266
+ function isHashAnAnchor(hash) {
267
+ var correspondingId = hash.substr(1)
268
+ return !!document.getElementById(correspondingId)
269
+ }
270
+
271
+ function getPathFromHash(hash) {
272
+ var index = hash.indexOf('?')
273
+ return index < 0 ? hash : hash.slice(0, index)
274
+ }
@@ -1,23 +1,31 @@
1
- import {
2
- isEmptyObject,
3
- msToNs,
4
- extend2Lev,
5
- toServerDuration,
6
- mapValues,
7
- RumEventType,
8
- LifeCycleEventType
9
- } from '@cloudcare/browser-core'
1
+ import { isEmptyObject, mapValues, toServerDuration, isNumber, RumEventType, LifeCycleEventType, extend2Lev } from '@cloudcare/browser-core'
10
2
  import { trackViews } from './trackViews'
11
3
  import { toValidEntry } from '../resource/resourceUtils'
12
- export function startViewCollection(lifeCycle, configuration, location) {
13
- lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, function (view) {
14
- lifeCycle.notify(
15
- LifeCycleEventType.RAW_RUM_EVENT_COLLECTED,
16
- processViewUpdate(view)
17
- )
18
- })
4
+ export function startViewCollection(
5
+ lifeCycle,
6
+ configuration,
7
+ location,
8
+ domMutationObservable,
9
+ locationChangeObservable,
10
+ initialViewOptions
11
+ ) {
12
+ lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, function(view) {
13
+ lifeCycle.notify(
14
+ LifeCycleEventType.RAW_RUM_EVENT_COLLECTED,
15
+ processViewUpdate(view)
16
+ )
17
+ }
18
+ )
19
19
 
20
- return trackViews(location, lifeCycle)
20
+ return trackViews(
21
+ location,
22
+ lifeCycle,
23
+ domMutationObservable,
24
+ configuration,
25
+ locationChangeObservable,
26
+ !configuration.trackViewsManually,
27
+ initialViewOptions
28
+ )
21
29
  }
22
30
  function computePerformanceViewDetails(entry) {
23
31
  var validEntry = toValidEntry(entry)
@@ -33,70 +41,75 @@ function computePerformanceViewDetails(entry) {
33
41
  loadEventStart = validEntry.loadEventStart,
34
42
  domContentLoadedEventEnd = validEntry.domContentLoadedEventEnd
35
43
  var details = {
36
- fmp: msToNs(validEntry.largestContentfulPaint)
44
+ fmp: toServerDuration(validEntry.largestContentfulPaint)
37
45
  }
38
46
  if (responseEnd !== fetchStart) {
39
- details.fpt = msToNs(responseEnd - fetchStart)
47
+ details.fpt = toServerDuration(responseEnd - fetchStart)
40
48
  var apdexLevel = parseInt((responseEnd - fetchStart) / 1000) // 秒数取整
41
49
  details.apdexLevel = apdexLevel > 9 ? 9 : apdexLevel
42
50
  }
43
51
  if (domInteractive !== fetchStart) {
44
- details.tti = msToNs(domInteractive - fetchStart)
52
+ details.tti = toServerDuration(domInteractive - fetchStart)
45
53
  }
46
54
  if (domContentLoaded !== fetchStart) {
47
- details.domReady = msToNs(domContentLoaded - fetchStart)
55
+ details.dom_ready = toServerDuration(domContentLoaded - fetchStart)
48
56
  }
49
57
  // Make sure a connection occurred
50
58
  if (loadEventEnd !== fetchStart) {
51
- details.load = msToNs(loadEventEnd - fetchStart)
59
+ details.load = toServerDuration(loadEventEnd - fetchStart)
52
60
  }
53
61
  if (loadEventStart !== domContentLoadedEventEnd) {
54
- details.resourceLoadTime = msToNs(loadEventStart - domContentLoadedEventEnd)
62
+ details.resource_load_time = toServerDuration(loadEventStart - domContentLoadedEventEnd)
55
63
  }
56
64
  if (domComplete !== domInteractive) {
57
- details.dom = msToNs(domComplete - domInteractive)
65
+ details.dom = toServerDuration(domComplete - domInteractive)
58
66
  }
59
67
  return details
60
68
  }
61
- function processViewUpdate(view) {
69
+ function processViewUpdate(
70
+ view,
71
+ ){
62
72
  var viewEvent = {
63
73
  _dd: {
64
- documentVersion: view.documentVersion
74
+ document_version: view.documentVersion,
65
75
  },
66
76
  date: view.startClocks.timeStamp,
67
77
  type: RumEventType.VIEW,
68
78
  view: {
69
79
  action: {
70
- count: view.eventCounts.userActionCount
80
+ count: view.eventCounts.actionCount,
81
+ },
82
+ frustration: {
83
+ count: view.eventCounts.frustrationCount,
71
84
  },
72
- cumulativeLayoutShift: view.cumulativeLayoutShift,
73
- domComplete: msToNs(view.timings.domComplete),
74
- domContentLoaded: msToNs(view.timings.domContentLoaded),
75
- domInteractive: msToNs(view.timings.domInteractive),
85
+ cumulative_layout_shift: view.cumulativeLayoutShift,
86
+ first_byte: toServerDuration(view.timings.firstByte),
87
+ dom_complete: toServerDuration(view.timings.domComplete),
88
+ dom_content_loaded: toServerDuration(view.timings.domContentLoaded),
89
+ dom_interactive: toServerDuration(view.timings.domInteractive),
76
90
  error: {
77
- count: view.eventCounts.errorCount
91
+ count: view.eventCounts.errorCount,
78
92
  },
79
- firstContentfulPaint: msToNs(view.timings.firstContentfulPaint),
80
- firstInputDelay: msToNs(view.timings.firstInputDelay),
81
- firstInputTime: msToNs(view.timings.firstInputTime),
82
- largestContentfulPaint: msToNs(view.timings.largestContentfulPaint),
83
- loadEventEnd: msToNs(view.timings.loadEventEnd),
84
- load_event: msToNs(view.timings.load_event),
85
- loadingTime: msToNs(view.loadingTime),
86
- loadingType: view.loadingType,
87
- isActive: view.isActive,
93
+ first_contentful_paint: toServerDuration(view.timings.firstContentfulPaint),
94
+ first_input_delay: toServerDuration(view.timings.firstInputDelay),
95
+ first_input_time: toServerDuration(view.timings.firstInputTime),
96
+ is_active: view.isActive,
88
97
  name: view.name,
89
- longTask: {
90
- count: view.eventCounts.longTaskCount
98
+ largest_contentful_paint: toServerDuration(view.timings.largestContentfulPaint),
99
+ load_event: toServerDuration(view.timings.loadEvent),
100
+ loading_time: discardNegativeDuration(toServerDuration(view.loadingTime)),
101
+ loading_type: view.loadingType,
102
+ long_task: {
103
+ count: view.eventCounts.longTaskCount,
91
104
  },
92
105
  resource: {
93
- count: view.eventCounts.resourceCount
106
+ count: view.eventCounts.resourceCount,
94
107
  },
95
- timeSpent: msToNs(view.duration)
108
+ time_spent: toServerDuration(view.duration),
96
109
  },
97
- session: {
98
- hasReplay: view.hasReplay || undefined
99
- }
110
+ // session: {
111
+ // has_replay: replayStats ? true : undefined,
112
+ // },
100
113
  }
101
114
  if (!isEmptyObject(view.customTimings)) {
102
115
  viewEvent.view.custom_timings = mapValues(
@@ -109,6 +122,13 @@ function processViewUpdate(view) {
109
122
  })
110
123
  return {
111
124
  rawRumEvent: viewEvent,
112
- startTime: view.startClocks.relative
125
+ startTime: view.startClocks.relative,
126
+ domainContext: {
127
+ location: view.location,
128
+ },
113
129
  }
114
130
  }
131
+
132
+ function discardNegativeDuration(duration){
133
+ return isNumber(duration) && duration < 0 ? undefined : duration
134
+ }
@@ -0,0 +1,90 @@
1
+ import { performDraw, startSessionManager, LifeCycleEventType } from '@cloudcare/browser-core'
2
+
3
+ export var RUM_SESSION_KEY = 'rum'
4
+
5
+
6
+ export var RumSessionPlan = {
7
+ LITE: 1,
8
+ PREMIUM: 2,
9
+ }
10
+
11
+ export var RumTrackingType = {
12
+ NOT_TRACKED : '0',
13
+ // Note: the "tracking type" value (stored in the session cookie) does not match the "session
14
+ // plan" value (sent in RUM events). This is expected, and was done to keep retrocompatibility
15
+ // with active sessions when upgrading the SDK.
16
+ TRACKED_WITH_SESSION_REPLAY: '1',
17
+ TRACKED_WITHOUT_SESSION_REPLAY: '2',
18
+ }
19
+
20
+ export function startRumSessionManager(configuration, lifeCycle) {
21
+ var sessionManager = startSessionManager(configuration.cookieOptions, RUM_SESSION_KEY, function(rawTrackingType) {
22
+ return computeSessionState(configuration, rawTrackingType)
23
+ }
24
+ )
25
+
26
+ sessionManager.expireObservable.subscribe(function() {
27
+ lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
28
+ })
29
+
30
+ sessionManager.renewObservable.subscribe(function () {
31
+ lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED)
32
+ })
33
+
34
+ return {
35
+ findTrackedSession: function(startTime) {
36
+ var session = sessionManager.findActiveSession(startTime)
37
+ if (!session || !isTypeTracked(session.trackingType)) {
38
+ return
39
+ }
40
+ return {
41
+ id: session.id,
42
+ }
43
+ },
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Start a tracked replay session stub
49
+ * It needs to be a premium plan in order to get long tasks
50
+ */
51
+ export function startRumSessionManagerStub() {
52
+ var session = {
53
+ id: '00000000-aaaa-0000-aaaa-000000000000',
54
+ }
55
+ return {
56
+ findTrackedSession: function() {
57
+ return session
58
+ },
59
+ }
60
+ }
61
+
62
+ function computeSessionState(configuration, rawTrackingType) {
63
+ var trackingType
64
+ if (hasValidRumSession(rawTrackingType)) {
65
+ trackingType = rawTrackingType
66
+ } else if (!performDraw(configuration.sampleRate)) {
67
+ trackingType = RumTrackingType.NOT_TRACKED
68
+ } else if (!performDraw(configuration.sessionReplaySampleRate)) {
69
+ trackingType = RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY
70
+ } else {
71
+ trackingType = RumTrackingType.TRACKED_WITH_SESSION_REPLAY
72
+ }
73
+ return {
74
+ trackingType: trackingType,
75
+ isTracked: isTypeTracked(trackingType),
76
+ }
77
+ }
78
+
79
+ function hasValidRumSession(trackingType) {
80
+ return (
81
+ trackingType === RumTrackingType.NOT_TRACKED ||
82
+ trackingType === RumTrackingType.TRACKED_WITH_SESSION_REPLAY ||
83
+ trackingType === RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY
84
+ )
85
+ }
86
+
87
+ function isTypeTracked(rumSessionType) {
88
+ return rumSessionType === RumTrackingType.TRACKED_WITHOUT_SESSION_REPLAY ||
89
+ rumSessionType === RumTrackingType.TRACKED_WITH_SESSION_REPLAY
90
+ }
@@ -1,9 +1,9 @@
1
1
  // === Generate a random 64-bit number in fixed-length hex format
2
2
  function randomTraceId() {
3
- const digits = '0123456789abcdef';
4
- let n = '';
5
- for (let i = 0; i < 16; i += 1) {
6
- const rand = Math.floor(Math.random() * 16);
3
+ var digits = '0123456789abcdef';
4
+ var n = '';
5
+ for (var i = 0; i < 16; i += 1) {
6
+ var rand = Math.floor(Math.random() * 16);
7
7
  n += digits[rand];
8
8
  }
9
9
  return n;
@@ -14,7 +14,7 @@ function randomTraceId() {
14
14
  * @param {*} configuration 配置信息
15
15
  */
16
16
  export function JaegerTracer(configuration) {
17
- const rootSpanId = randomTraceId();
17
+ var rootSpanId = randomTraceId();
18
18
  // this._traceId = randomTraceId() + rootSpanId // 默认用128bit,兼容其他配置
19
19
  if (configuration.traceId128Bit) {
20
20
  // 128bit生成traceid