@cloudcare/browser-rum 3.3.4 → 3.3.6

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 (79) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/perStartRum.js +42 -6
  4. package/cjs/boot/perStartRum.js.map +1 -1
  5. package/cjs/boot/recorderApi.js +35 -10
  6. package/cjs/boot/recorderApi.js.map +1 -1
  7. package/cjs/boot/rum.js +8 -0
  8. package/cjs/boot/rum.js.map +1 -1
  9. package/cjs/boot/rumPublicApi.js +36 -3
  10. package/cjs/boot/rumPublicApi.js.map +1 -1
  11. package/cjs/domain/configuration.js +7 -0
  12. package/cjs/domain/configuration.js.map +1 -1
  13. package/cjs/domain/contexts/urlContexts.js +11 -2
  14. package/cjs/domain/contexts/urlContexts.js.map +1 -1
  15. package/cjs/domain/contexts/viewContexts.js +2 -2
  16. package/cjs/domain/contexts/viewContexts.js.map +1 -1
  17. package/cjs/domain/deflate/deflateWorker.js +1 -1
  18. package/cjs/domain/plugins.js +67 -0
  19. package/cjs/domain/plugins.js.map +1 -0
  20. package/cjs/domain/replay/record/canvasEncodeWorker.js +1 -1
  21. package/cjs/domain/rumEventsCollection/error/errorCollection.js +17 -5
  22. package/cjs/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  23. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +56 -0
  24. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  25. package/cjs/domain/rumEventsCollection/resource/webSocketCollection.js +196 -0
  26. package/cjs/domain/rumEventsCollection/resource/webSocketCollection.js.map +1 -0
  27. package/cjs/domain/rumEventsCollection/view/trackLoadingTime.js +8 -3
  28. package/cjs/domain/rumEventsCollection/view/trackLoadingTime.js.map +1 -1
  29. package/cjs/domain/rumEventsCollection/view/trackViews.js +2 -1
  30. package/cjs/domain/rumEventsCollection/view/trackViews.js.map +1 -1
  31. package/cjs/domain/rumSessionManager.js +23 -3
  32. package/cjs/domain/rumSessionManager.js.map +1 -1
  33. package/esm/boot/buildEnv.js +1 -1
  34. package/esm/boot/perStartRum.js +42 -6
  35. package/esm/boot/perStartRum.js.map +1 -1
  36. package/esm/boot/recorderApi.js +35 -10
  37. package/esm/boot/recorderApi.js.map +1 -1
  38. package/esm/boot/rum.js +8 -0
  39. package/esm/boot/rum.js.map +1 -1
  40. package/esm/boot/rumPublicApi.js +36 -3
  41. package/esm/boot/rumPublicApi.js.map +1 -1
  42. package/esm/domain/configuration.js +8 -1
  43. package/esm/domain/configuration.js.map +1 -1
  44. package/esm/domain/contexts/urlContexts.js +12 -3
  45. package/esm/domain/contexts/urlContexts.js.map +1 -1
  46. package/esm/domain/contexts/viewContexts.js +2 -2
  47. package/esm/domain/contexts/viewContexts.js.map +1 -1
  48. package/esm/domain/deflate/deflateWorker.js +1 -1
  49. package/esm/domain/plugins.js +59 -0
  50. package/esm/domain/plugins.js.map +1 -0
  51. package/esm/domain/replay/record/canvasEncodeWorker.js +1 -1
  52. package/esm/domain/rumEventsCollection/error/errorCollection.js +17 -5
  53. package/esm/domain/rumEventsCollection/error/errorCollection.js.map +1 -1
  54. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +55 -0
  55. package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  56. package/esm/domain/rumEventsCollection/resource/webSocketCollection.js +189 -0
  57. package/esm/domain/rumEventsCollection/resource/webSocketCollection.js.map +1 -0
  58. package/esm/domain/rumEventsCollection/view/trackLoadingTime.js +8 -3
  59. package/esm/domain/rumEventsCollection/view/trackLoadingTime.js.map +1 -1
  60. package/esm/domain/rumEventsCollection/view/trackViews.js +2 -1
  61. package/esm/domain/rumEventsCollection/view/trackViews.js.map +1 -1
  62. package/esm/domain/rumSessionManager.js +24 -4
  63. package/esm/domain/rumSessionManager.js.map +1 -1
  64. package/package.json +4 -4
  65. package/src/boot/perStartRum.js +47 -7
  66. package/src/boot/recorderApi.js +36 -12
  67. package/src/boot/rum.js +11 -0
  68. package/src/boot/rumPublicApi.js +38 -3
  69. package/src/domain/configuration.js +13 -1
  70. package/src/domain/contexts/urlContexts.js +17 -3
  71. package/src/domain/contexts/viewContexts.js +2 -2
  72. package/src/domain/plugins.js +94 -0
  73. package/src/domain/rumEventsCollection/error/errorCollection.js +22 -6
  74. package/src/domain/rumEventsCollection/resource/resourceCollection.js +70 -0
  75. package/src/domain/rumEventsCollection/resource/webSocketCollection.js +271 -0
  76. package/src/domain/rumEventsCollection/view/trackLoadingTime.js +12 -3
  77. package/src/domain/rumEventsCollection/view/trackViews.js +2 -1
  78. package/src/domain/rumSessionManager.js +33 -2
  79. package/types/index.d.ts +85 -6
@@ -22,6 +22,7 @@ import {
22
22
  getType
23
23
  } from '@cloudcare/browser-core'
24
24
  import { buildCommonContext } from '../domain/contexts/commonContext'
25
+ import { callPluginsMethod } from '../domain/plugins'
25
26
  import { createPreStartStrategy } from './perStartRum'
26
27
  import { createResourceTrackerManager } from '../domain/rumEventsCollection/resource/resourceTrackerManager'
27
28
  var RUM_STORAGE_KEY = 'rum'
@@ -55,9 +56,30 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
55
56
  recorderApi
56
57
  )
57
58
  }
58
- let strategy = createPreStartStrategy(
59
+ let strategy
60
+ function addPluginError(providedError) {
61
+ callMonitored(function () {
62
+ providedError = providedError || {}
63
+ strategy.addError(
64
+ assign({}, providedError, {
65
+ context: sanitize(providedError.context)
66
+ }),
67
+ getCommonContext()
68
+ )
69
+ addTelemetryUsage({ feature: 'add-error' })
70
+ })
71
+ }
72
+ strategy = createPreStartStrategy(
59
73
  assign({}, options, {
60
- resourceTrackerManager: resourceTrackerManager
74
+ resourceTrackerManager: resourceTrackerManager,
75
+ onInit: function (initConfiguration, publicApi) {
76
+ callPluginsMethod(initConfiguration.plugins, 'onInit', {
77
+ addError: addPluginError,
78
+ publicApi: publicApi,
79
+ rum: publicApi,
80
+ initConfiguration: initConfiguration
81
+ })
82
+ }
61
83
  }),
62
84
  getCommonContext,
63
85
  function (configuration, compressionWorker, initialViewOptions) {
@@ -106,6 +128,12 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
106
128
 
107
129
  strategy = createPostStartStrategy(strategy, startRumResult)
108
130
 
131
+ callPluginsMethod(configuration.plugins, 'onRumStart', {
132
+ addError: addPluginError,
133
+ publicApi: rumPublicApi,
134
+ rum: rumPublicApi
135
+ })
136
+
109
137
  return startRumResult
110
138
  }
111
139
  )
@@ -128,7 +156,7 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
128
156
 
129
157
  var rumPublicApi = makePublicApi(assign({
130
158
  init: monitor(function (initConfiguration) {
131
- strategy.init(initConfiguration)
159
+ strategy.init(initConfiguration, rumPublicApi)
132
160
  }),
133
161
  setViewName: monitor((name) => {
134
162
  strategy.setViewName(name)
@@ -289,6 +317,13 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
289
317
  strategy.stopSession()
290
318
  addTelemetryUsage({ feature: 'stop-session' })
291
319
  }),
320
+ startSession: monitor(function (options) {
321
+ strategy.startSession(options)
322
+ addTelemetryUsage({
323
+ feature: 'start-session',
324
+ sessionSampleRate: options && options.sessionSampleRate
325
+ })
326
+ }),
292
327
  setForcedSession: monitor(function () {
293
328
  strategy.setForcedSession()
294
329
  addTelemetryUsage({ feature: 'set-forced-session' })
@@ -13,10 +13,12 @@ import {
13
13
  catchUserErrors,
14
14
  getOrigin,
15
15
  each,
16
- isNumber
16
+ isNumber,
17
+ ExperimentalFeature
17
18
  } from '@cloudcare/browser-core'
18
19
  import { buildEnv } from '../boot/buildEnv'
19
20
  import { isTracingOption } from '../domain/tracing/tracer'
21
+ import { validatePlugins } from './plugins'
20
22
 
21
23
  var REPLAY_CANVAS_QUALITY_PRESETS = {
22
24
  low: {
@@ -121,6 +123,9 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
121
123
  display.error('Excluded Activity Urls should be an array')
122
124
  return
123
125
  }
126
+ if (!validatePlugins(initConfiguration.plugins)) {
127
+ return
128
+ }
124
129
  if (
125
130
  initConfiguration.replayCanvasMode !== undefined &&
126
131
  initConfiguration.replayCanvasMode !== 'manual' &&
@@ -291,8 +296,14 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
291
296
  replayCanvasWorkerUrl: initConfiguration.replayCanvasWorkerUrl,
292
297
  compressIntakeRequests: !!initConfiguration.compressIntakeRequests,
293
298
  trackUserInteractions: trackUserInteractions,
299
+ trackWebSockets:
300
+ isArray(initConfiguration.enableExperimentalFeatures) &&
301
+ initConfiguration.enableExperimentalFeatures.indexOf(
302
+ ExperimentalFeature.TRACK_WEBSOCKETS
303
+ ) !== -1,
294
304
  enableLongAnimationFrame: !!initConfiguration.enableLongAnimationFrame,
295
305
  trackViewsManually: !!initConfiguration.trackViewsManually,
306
+ plugins: initConfiguration.plugins || [],
296
307
  replayCanvasEnabled: !!initConfiguration.replayCanvasEnabled,
297
308
  replayCanvasMode: replayCanvasMode,
298
309
  replayCanvasSampling: replayCanvasSampling,
@@ -385,6 +396,7 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
385
396
  buildEnv
386
397
  )
387
398
  }
399
+
388
400
  /**
389
401
  * Handles allowedTracingUrls and processes legacy allowedTracingOrigins
390
402
  */
@@ -5,8 +5,10 @@ import {
5
5
  createValueHistory,
6
6
  replaceNumberCharByPath,
7
7
  getQueryParamsFromUrl,
8
+ getURLSearchParams,
8
9
  isHashAnAnchor,
9
- getPathFromHash
10
+ getPathFromHash,
11
+ buildUrl
10
12
  } from '@cloudcare/browser-core'
11
13
 
12
14
  /**
@@ -30,10 +32,19 @@ export function startUrlContexts(
30
32
 
31
33
  lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, function (data) {
32
34
  var viewUrl = location.href
35
+ var viewLocation = location
36
+ if (data.url) {
37
+ try {
38
+ viewLocation = buildUrl(data.url, location.href)
39
+ viewUrl = viewLocation.href
40
+ } catch (error) {
41
+ // Fall back to the browser location for invalid framework URLs.
42
+ }
43
+ }
33
44
  urlContextHistory.add(
34
45
  buildUrlContext({
35
46
  url: viewUrl,
36
- location: location,
47
+ location: viewLocation,
37
48
  referrer: !previousViewUrl ? document.referrer : previousViewUrl
38
49
  }),
39
50
  data.startClocks.relative
@@ -77,7 +88,10 @@ export function startUrlContexts(
77
88
  host: data.location.host,
78
89
  path: path,
79
90
  pathGroup: replaceNumberCharByPath(path),
80
- urlQuery: getQueryParamsFromUrl(data.location.href),
91
+ urlQuery:
92
+ typeof data.location.search === 'string'
93
+ ? getURLSearchParams(data.location.search)
94
+ : getQueryParamsFromUrl(data.location.href),
81
95
  pathname: pathname
82
96
  }
83
97
  }
@@ -43,8 +43,8 @@ export function startViewContexts(lifeCycle) {
43
43
  }
44
44
 
45
45
  return {
46
- findView: function (startTime) {
47
- return viewContextHistory.find(startTime)
46
+ findView: function (startTime, options) {
47
+ return viewContextHistory.find(startTime, options)
48
48
  },
49
49
  stop: function () {
50
50
  viewContextHistory.stop()
@@ -0,0 +1,94 @@
1
+ import {
2
+ assign,
3
+ catchUserErrors,
4
+ display,
5
+ each,
6
+ getType,
7
+ isArray
8
+ } from '@cloudcare/browser-core'
9
+
10
+ var PLUGIN_METHODS = [
11
+ 'onInit',
12
+ 'onRumStart',
13
+ 'getConfigurationTelemetry'
14
+ ]
15
+
16
+ export function validatePlugins(plugins) {
17
+ if (plugins === undefined) {
18
+ return true
19
+ }
20
+ if (!isArray(plugins)) {
21
+ display.error('Plugins should be an array')
22
+ return false
23
+ }
24
+
25
+ for (var i = 0; i < plugins.length; i += 1) {
26
+ var plugin = plugins[i]
27
+ if (
28
+ !plugin ||
29
+ getType(plugin.name) !== 'string' ||
30
+ plugin.name.length === 0
31
+ ) {
32
+ display.error('Plugins should have a non-empty string name')
33
+ return false
34
+ }
35
+
36
+ for (
37
+ var methodIndex = 0;
38
+ methodIndex < PLUGIN_METHODS.length;
39
+ methodIndex += 1
40
+ ) {
41
+ var methodName = PLUGIN_METHODS[methodIndex]
42
+ if (
43
+ plugin[methodName] !== undefined &&
44
+ getType(plugin[methodName]) !== 'function'
45
+ ) {
46
+ display.error(
47
+ 'Plugin ' + plugin.name + ' has an invalid ' + methodName + ' method'
48
+ )
49
+ return false
50
+ }
51
+ }
52
+ }
53
+
54
+ return true
55
+ }
56
+
57
+ export function callPluginsMethod(plugins, methodName, parameter) {
58
+ if (!plugins) {
59
+ return
60
+ }
61
+
62
+ each(plugins, function (plugin) {
63
+ var method = plugin[methodName]
64
+ if (getType(method) !== 'function') {
65
+ return
66
+ }
67
+ catchUserErrors(
68
+ function () {
69
+ return method.call(plugin, parameter)
70
+ },
71
+ plugin.name + ' plugin ' + methodName + ' threw an error:'
72
+ )()
73
+ })
74
+ }
75
+
76
+ export function getPluginsTelemetry(plugins) {
77
+ var telemetry = []
78
+ each(plugins || [], function (plugin) {
79
+ var pluginTelemetry = { name: plugin.name }
80
+ if (getType(plugin.getConfigurationTelemetry) === 'function') {
81
+ var configurationTelemetry = catchUserErrors(
82
+ function () {
83
+ return plugin.getConfigurationTelemetry()
84
+ },
85
+ plugin.name + ' plugin getConfigurationTelemetry threw an error:'
86
+ )()
87
+ if (getType(configurationTelemetry) === 'object') {
88
+ pluginTelemetry = assign(pluginTelemetry, configurationTelemetry)
89
+ }
90
+ }
91
+ telemetry.push(pluginTelemetry)
92
+ })
93
+ return telemetry
94
+ }
@@ -27,22 +27,31 @@ export function startErrorCollection(
27
27
  trackRuntimeError(errorObservable)
28
28
  trackReportError(configuration, errorObservable)
29
29
  var session = sessionManager.findTrackedSession()
30
- let hasError = session && session.isErrorSession && session.sessionHasError
31
- if (session && session.isErrorSession) {
30
+ let hasError = shouldMarkSessionOnError(session) && session.sessionHasError
31
+ if (shouldMarkSessionOnError(session)) {
32
32
  lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, function () {
33
33
  hasError = false
34
34
  })
35
35
  }
36
-
37
- errorObservable.subscribe(function (error) {
38
- if (session && session.isErrorSession && !hasError) {
36
+ var markSessionOnError = function () {
37
+ if (shouldMarkSessionOnError(session) && !hasError) {
39
38
  sessionManager.setErrorForSession()
40
39
  hasError = true
41
40
  }
41
+ }
42
+
43
+ errorObservable.subscribe(function (error) {
44
+ markSessionOnError()
42
45
  lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, { error: error })
43
46
  })
44
47
 
45
- return doStartErrorCollection(lifeCycle, pageStateHistory)
48
+ var errorCollection = doStartErrorCollection(lifeCycle, pageStateHistory)
49
+ return {
50
+ addError: function (providedError, savedCommonContext) {
51
+ markSessionOnError()
52
+ errorCollection.addError(providedError, savedCommonContext)
53
+ }
54
+ }
46
55
  }
47
56
 
48
57
  export function doStartErrorCollection(lifeCycle, pageStateHistory) {
@@ -113,3 +122,10 @@ function processError(error, pageStateHistory) {
113
122
  }
114
123
  }
115
124
  }
125
+
126
+ function shouldMarkSessionOnError(session) {
127
+ return (
128
+ session &&
129
+ (session.isErrorSession || session.errorSessionReplayAllowed === true)
130
+ )
131
+ }
@@ -55,6 +55,19 @@ export function startResourceCollection(
55
55
  })
56
56
  }
57
57
  var requestResourceEntryMatcher = createRequestResourceEntryMatcher()
58
+ var webSocketSubscription = lifeCycle.subscribe(
59
+ LifeCycleEventType.WEBSOCKET_COMPLETED,
60
+ function (event) {
61
+ try {
62
+ lifeCycle.notify(
63
+ LifeCycleEventType.RAW_RUM_EVENT_COLLECTED,
64
+ processWebSocket(event)
65
+ )
66
+ } catch (error) {
67
+ addTelemetryError(error)
68
+ }
69
+ }
70
+ )
58
71
  lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, function (request) {
59
72
  handleResource(function () {
60
73
  return processRequest(request, pageStateHistory, requestResourceEntryMatcher)
@@ -113,6 +126,7 @@ export function startResourceCollection(
113
126
  }
114
127
  return {
115
128
  stop: function () {
129
+ webSocketSubscription.unsubscribe()
116
130
  performanceResourceSubscription.unsubscribe()
117
131
  },
118
132
  addResource: (resource, savedCommonContext) => {
@@ -126,6 +140,62 @@ export function startResourceCollection(
126
140
  }
127
141
  }
128
142
 
143
+ export function processWebSocket(event) {
144
+ var duration = event.endClocks.timeStamp - event.startClocks.timeStamp
145
+ var urlObj = urlParse(event.url).getParse()
146
+ return {
147
+ startTime: event.startClocks.relative,
148
+ rawRumEvent: {
149
+ date: event.startClocks.timeStamp,
150
+ type: RumEventType.RESOURCE,
151
+ resource: {
152
+ id: UUID(),
153
+ type: ResourceType.WEBSOCKET,
154
+ url: event.url,
155
+ urlHost: urlObj.Host,
156
+ urlPath: urlObj.Path,
157
+ urlPathGroup: replaceNumberCharByPath(urlObj.Path),
158
+ urlQuery: getQueryParamsFromUrl(event.url),
159
+ duration: toServerDuration(duration),
160
+ websocket: {
161
+ connectionId: event.connectionId,
162
+ handshakeSucceeded: event.handshakeSucceeded,
163
+ startTime: event.startClocks.timeStamp,
164
+ endTime: event.endClocks.timeStamp,
165
+ startViewId: event.startViewId,
166
+ endViewId: event.endViewId,
167
+ trackingEndReason: event.trackingEndReason,
168
+ closeCode: event.closeCode,
169
+ closeReason: event.closeReason,
170
+ wasClean: event.wasClean,
171
+ messagesIn: event.messagesIn,
172
+ messagesOut: event.messagesOut,
173
+ timeToFirstMessageIn: toServerDuration(
174
+ event.firstMessageInOffset
175
+ ),
176
+ timeToFirstMessageOut: toServerDuration(
177
+ event.firstMessageOutOffset
178
+ ),
179
+ lastMessageInAt: event.lastMessageInAt,
180
+ longestInboundSilence: toServerDuration(
181
+ event.longestInboundSilence
182
+ ),
183
+ inboundIdleDurationBeforeClose: toServerDuration(
184
+ event.inboundIdleDurationBeforeClose
185
+ ),
186
+ bufferedAmountMax: event.bufferedAmountMax,
187
+ protocol: event.protocol,
188
+ setupDuration: toServerDuration(event.setupDuration)
189
+ }
190
+ }
191
+ },
192
+ domainContext: {
193
+ isWebSocket: true,
194
+ webSocket: event.webSocket
195
+ }
196
+ }
197
+ }
198
+
129
199
  function processRequest(request, pageStateHistory, requestResourceEntryMatcher) {
130
200
  var matchingTiming = requestResourceEntryMatcher.matchRequestResourceEntry(request)
131
201
  var startClocks = matchingTiming
@@ -0,0 +1,271 @@
1
+ import {
2
+ clocksNow,
3
+ elapsed,
4
+ initWebSocketObservable,
5
+ LifeCycleEventType,
6
+ PageExitReason,
7
+ UUID
8
+ } from '@cloudcare/browser-core'
9
+
10
+ export function startWebSocketCollection(
11
+ lifeCycle,
12
+ viewContexts,
13
+ webSocketContextObservable
14
+ ) {
15
+ var tracker = trackWebSocket(
16
+ lifeCycle,
17
+ webSocketContextObservable || initWebSocketObservable(),
18
+ viewContexts
19
+ )
20
+ var sessionExpiredSubscription = lifeCycle.subscribe(
21
+ LifeCycleEventType.SESSION_EXPIRED,
22
+ function () {
23
+ tracker.flushOpenConnections('session_end', true)
24
+ }
25
+ )
26
+ var sessionRenewedSubscription = lifeCycle.subscribe(
27
+ LifeCycleEventType.SESSION_RENEWED,
28
+ function () {
29
+ tracker.resumeOpenConnections()
30
+ }
31
+ )
32
+ var pageExitSubscription = lifeCycle.subscribe(
33
+ LifeCycleEventType.PAGE_EXITED,
34
+ function (event) {
35
+ if (event.reason === PageExitReason.UNLOADING) {
36
+ tracker.flushOpenConnections('page_exit')
37
+ }
38
+ }
39
+ )
40
+
41
+ return {
42
+ stop: function () {
43
+ sessionExpiredSubscription.unsubscribe()
44
+ sessionRenewedSubscription.unsubscribe()
45
+ pageExitSubscription.unsubscribe()
46
+ tracker.flushOpenConnections('session_end')
47
+ tracker.stop()
48
+ }
49
+ }
50
+ }
51
+
52
+ export function trackWebSocket(
53
+ lifeCycle,
54
+ webSocketContextObservable,
55
+ viewContexts
56
+ ) {
57
+ var registry = new Map()
58
+ var isWaitingForSessionRenewal = false
59
+ var subscription = webSocketContextObservable.subscribe(function (context) {
60
+ var connection = registry.get(context.instance)
61
+
62
+ if (context.state === 'connecting') {
63
+ connection = createConnection(
64
+ context.instance,
65
+ context.url,
66
+ context.startClocks,
67
+ viewContexts
68
+ )
69
+ connection.pendingSessionRenewal = isWaitingForSessionRenewal
70
+ registry.set(context.instance, connection)
71
+ return
72
+ }
73
+
74
+ if (!connection) {
75
+ return
76
+ }
77
+
78
+ if (connection.pendingSessionRenewal) {
79
+ if (context.state === 'open') {
80
+ connection.openClocks = context.openClocks
81
+ connection.protocol = context.protocol
82
+ } else if (context.state === 'closed') {
83
+ registry.delete(context.instance)
84
+ }
85
+ return
86
+ }
87
+
88
+ if (context.state === 'open') {
89
+ connection.openClocks = context.openClocks
90
+ connection.protocol = context.protocol
91
+ connection.setupDuration = elapsed(
92
+ connection.startClocks.timeStamp,
93
+ context.openClocks.timeStamp
94
+ )
95
+ return
96
+ }
97
+
98
+ if (context.state === 'message-in') {
99
+ connection.messagesIn.count += 1
100
+ connection.messagesIn.size += context.size
101
+ recordMessageTiming(connection, context.at, 'in')
102
+ return
103
+ }
104
+
105
+ if (context.state === 'message-out') {
106
+ connection.messagesOut.count += 1
107
+ connection.messagesOut.size += context.size
108
+ connection.bufferedAmountMax = Math.max(
109
+ connection.bufferedAmountMax,
110
+ context.bufferedAmountPreSend
111
+ )
112
+ recordMessageTiming(connection, context.at, 'out')
113
+ return
114
+ }
115
+
116
+ if (context.state === 'closed') {
117
+ registry.delete(context.instance)
118
+ lifeCycle.notify(
119
+ LifeCycleEventType.WEBSOCKET_COMPLETED,
120
+ buildCompletedEvent(
121
+ connection,
122
+ context,
123
+ 'close_event',
124
+ viewContexts
125
+ )
126
+ )
127
+ }
128
+ })
129
+
130
+ return {
131
+ flushOpenConnections: function (reason, preserveConnections) {
132
+ var at = clocksNow()
133
+ isWaitingForSessionRenewal = !!preserveConnections
134
+ registry.forEach(function (connection) {
135
+ if (connection.pendingSessionRenewal) {
136
+ return
137
+ }
138
+ lifeCycle.notify(
139
+ LifeCycleEventType.WEBSOCKET_COMPLETED,
140
+ buildCompletedEvent(connection, { at: at }, reason, viewContexts)
141
+ )
142
+ if (preserveConnections) {
143
+ connection.pendingSessionRenewal = true
144
+ }
145
+ })
146
+ if (!preserveConnections) {
147
+ registry.clear()
148
+ }
149
+ },
150
+ resumeOpenConnections: function () {
151
+ var at = clocksNow()
152
+ isWaitingForSessionRenewal = false
153
+ registry.forEach(function (connection) {
154
+ if (connection.pendingSessionRenewal) {
155
+ resetConnectionSegment(connection, at, viewContexts)
156
+ }
157
+ })
158
+ },
159
+ stop: function () {
160
+ subscription.unsubscribe()
161
+ registry.clear()
162
+ }
163
+ }
164
+ }
165
+
166
+ function createConnection(webSocket, url, startClocks, viewContexts) {
167
+ var connection = {
168
+ webSocket: webSocket,
169
+ connectionId: UUID(),
170
+ url: url
171
+ }
172
+ resetConnectionSegment(connection, startClocks, viewContexts)
173
+ return connection
174
+ }
175
+
176
+ function resetConnectionSegment(connection, startClocks, viewContexts) {
177
+ var wasOpen = connection.openClocks !== undefined
178
+ var startView = viewContexts.findView(startClocks.relative)
179
+
180
+ connection.startClocks = startClocks
181
+ connection.startViewId = startView && startView.id
182
+ connection.messagesIn = { count: 0, size: 0 }
183
+ connection.messagesOut = { count: 0, size: 0 }
184
+ connection.longestInboundSilence = 0
185
+ connection.bufferedAmountMax = 0
186
+ connection.pendingSessionRenewal = false
187
+
188
+ delete connection.firstMessageInOffset
189
+ delete connection.firstMessageOutOffset
190
+ delete connection.lastMessageInAt
191
+
192
+ if (wasOpen) {
193
+ connection.openClocks = startClocks
194
+ connection.setupDuration = 0
195
+ } else {
196
+ delete connection.openClocks
197
+ delete connection.setupDuration
198
+ }
199
+ }
200
+
201
+ function recordMessageTiming(connection, at, direction) {
202
+ if (!connection.openClocks) {
203
+ return
204
+ }
205
+
206
+ var offset = elapsed(connection.openClocks.timeStamp, at.timeStamp)
207
+ if (direction === 'in' && connection.firstMessageInOffset === undefined) {
208
+ connection.firstMessageInOffset = offset
209
+ } else if (
210
+ direction === 'out' &&
211
+ connection.firstMessageOutOffset === undefined
212
+ ) {
213
+ connection.firstMessageOutOffset = offset
214
+ }
215
+
216
+ if (direction === 'in') {
217
+ if (connection.lastMessageInAt !== undefined) {
218
+ connection.longestInboundSilence = Math.max(
219
+ connection.longestInboundSilence,
220
+ elapsed(connection.lastMessageInAt, at.timeStamp)
221
+ )
222
+ }
223
+ connection.lastMessageInAt = at.timeStamp
224
+ }
225
+ }
226
+
227
+ function buildCompletedEvent(
228
+ connection,
229
+ endInfo,
230
+ trackingEndReason,
231
+ viewContexts
232
+ ) {
233
+ var endClocks = endInfo.at
234
+ var endView = viewContexts.findView(
235
+ endClocks.relative,
236
+ trackingEndReason === 'session_end' || trackingEndReason === 'page_exit'
237
+ ? { returnInactive: true }
238
+ : undefined
239
+ )
240
+
241
+ return {
242
+ webSocket: connection.webSocket,
243
+ connectionId: connection.connectionId,
244
+ url: connection.url,
245
+ protocol: connection.protocol,
246
+ startClocks: connection.startClocks,
247
+ endClocks: endClocks,
248
+ startViewId: connection.startViewId,
249
+ endViewId: endView && endView.id,
250
+ messagesIn: connection.messagesIn,
251
+ messagesOut: connection.messagesOut,
252
+ firstMessageInOffset: connection.firstMessageInOffset,
253
+ firstMessageOutOffset: connection.firstMessageOutOffset,
254
+ lastMessageInAt: connection.lastMessageInAt,
255
+ longestInboundSilence: connection.longestInboundSilence,
256
+ bufferedAmountMax: connection.bufferedAmountMax,
257
+ inboundIdleDurationBeforeClose:
258
+ connection.lastMessageInAt === undefined
259
+ ? undefined
260
+ : elapsed(connection.lastMessageInAt, endClocks.timeStamp),
261
+ closeCode: endInfo.code,
262
+ closeReason: endInfo.reason,
263
+ wasClean: endInfo.wasClean,
264
+ handshakeSucceeded: connection.openClocks !== undefined,
265
+ trackingEndReason: trackingEndReason,
266
+ setupDuration:
267
+ connection.setupDuration === undefined
268
+ ? elapsed(connection.startClocks.timeStamp, endClocks.timeStamp)
269
+ : connection.setupDuration
270
+ }
271
+ }
@@ -19,10 +19,19 @@ export function trackLoadingTime(
19
19
  !isWaitingForLoadEvent &&
20
20
  loadingTimeCandidates.length > 0
21
21
  ) {
22
- var loadingTime = Math.max.apply(Math, loadingTimeCandidates)
23
- if (loadingTime < firstHidden.getTimeStamp() - viewStart.relative) {
24
- callback(loadingTime)
22
+ var firstHiddenTime = firstHidden.getTimeStamp() - viewStart.relative
23
+ var visibleLoadingTimeCandidates = loadingTimeCandidates.filter(function (
24
+ loadingTime
25
+ ) {
26
+ return loadingTime < firstHiddenTime
27
+ })
28
+
29
+ if (visibleLoadingTimeCandidates.length === 0) {
30
+ return
25
31
  }
32
+
33
+ var loadingTime = Math.max.apply(Math, visibleLoadingTimeCandidates)
34
+ callback(loadingTime)
26
35
  }
27
36
  }
28
37