@cloudcare/browser-rum 3.3.14 → 3.3.15

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 (56) hide show
  1. package/README.md +48 -0
  2. package/README.zh.md +35 -0
  3. package/bundle/dataflux-rum.js +1 -1
  4. package/cjs/boot/buildEnv.js +1 -1
  5. package/cjs/boot/perStartRum.js +3 -0
  6. package/cjs/boot/perStartRum.js.map +1 -1
  7. package/cjs/boot/recorderApi.js +72 -56
  8. package/cjs/boot/recorderApi.js.map +1 -1
  9. package/cjs/boot/rumPublicApi.js +35 -3
  10. package/cjs/boot/rumPublicApi.js.map +1 -1
  11. package/cjs/domain/assembly.js +7 -1
  12. package/cjs/domain/assembly.js.map +1 -1
  13. package/cjs/domain/configuration.js +62 -0
  14. package/cjs/domain/configuration.js.map +1 -1
  15. package/cjs/domain/deflate/deflateWorker.js +1 -1
  16. package/cjs/domain/replay/record/canvasEncodeWorker.js +1 -1
  17. package/cjs/domain/requestCollection.js +3 -1
  18. package/cjs/domain/requestCollection.js.map +1 -1
  19. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
  20. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  21. package/cjs/domain/tracing/tracer.js +5 -1
  22. package/cjs/domain/tracing/tracer.js.map +1 -1
  23. package/cjs/domain/visitorId/index.js +379 -0
  24. package/cjs/domain/visitorId/index.js.map +1 -0
  25. package/esm/boot/buildEnv.js +1 -1
  26. package/esm/boot/perStartRum.js +3 -0
  27. package/esm/boot/perStartRum.js.map +1 -1
  28. package/esm/boot/recorderApi.js +72 -56
  29. package/esm/boot/recorderApi.js.map +1 -1
  30. package/esm/boot/rumPublicApi.js +36 -4
  31. package/esm/boot/rumPublicApi.js.map +1 -1
  32. package/esm/domain/assembly.js +7 -1
  33. package/esm/domain/assembly.js.map +1 -1
  34. package/esm/domain/configuration.js +62 -1
  35. package/esm/domain/configuration.js.map +1 -1
  36. package/esm/domain/deflate/deflateWorker.js +1 -1
  37. package/esm/domain/replay/record/canvasEncodeWorker.js +1 -1
  38. package/esm/domain/requestCollection.js +4 -2
  39. package/esm/domain/requestCollection.js.map +1 -1
  40. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
  41. package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
  42. package/esm/domain/tracing/tracer.js +6 -2
  43. package/esm/domain/tracing/tracer.js.map +1 -1
  44. package/esm/domain/visitorId/index.js +368 -0
  45. package/esm/domain/visitorId/index.js.map +1 -0
  46. package/package.json +4 -4
  47. package/src/boot/perStartRum.js +4 -0
  48. package/src/boot/recorderApi.js +73 -59
  49. package/src/boot/rumPublicApi.js +38 -4
  50. package/src/domain/assembly.js +13 -1
  51. package/src/domain/configuration.js +154 -0
  52. package/src/domain/requestCollection.js +4 -1
  53. package/src/domain/rumEventsCollection/resource/resourceCollection.js +3 -0
  54. package/src/domain/tracing/tracer.js +15 -2
  55. package/src/domain/visitorId/index.js +522 -0
  56. package/types/index.d.ts +2 -0
@@ -19,17 +19,23 @@ import {
19
19
  createCustomerDataTrackerManager,
20
20
  CustomerDataCompressionStatus,
21
21
  displayAlreadyInitializedError,
22
- getType
22
+ getType,
23
+ noop
23
24
  } from '@cloudcare/browser-core'
24
25
  import { buildCommonContext } from '../domain/contexts/commonContext'
25
26
  import { callPluginsMethod } from '../domain/plugins'
26
27
  import { createPreStartStrategy } from './perStartRum'
27
28
  import { createResourceTrackerManager } from '../domain/rumEventsCollection/resource/resourceTrackerManager'
29
+ import {
30
+ startVisitorId,
31
+ VISITOR_ID_CONTEXT_KEY
32
+ } from '../domain/visitorId'
28
33
  var RUM_STORAGE_KEY = 'rum'
29
34
  export function makeRumPublicApi(startRumImpl, recorderApi, options) {
30
35
  if (options === undefined) {
31
36
  options = {}
32
37
  }
38
+ var startVisitorIdImpl = options.startVisitorId || startVisitorId
33
39
  const customerDataTrackerManager = createCustomerDataTrackerManager(
34
40
  CustomerDataCompressionStatus.Unknown
35
41
  )
@@ -49,12 +55,32 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
49
55
  email: { type: 'string' }
50
56
  }
51
57
  })
58
+ var visitorId = {
59
+ getId: noop,
60
+ reset: noop,
61
+ stop: noop
62
+ }
63
+ function getGlobalContext() {
64
+ var context = globalContextManager.getContext()
65
+ var currentVisitorId = visitorId.getId()
66
+ if (currentVisitorId) {
67
+ context[VISITOR_ID_CONTEXT_KEY] = currentVisitorId
68
+ }
69
+ return context
70
+ }
52
71
  function getCommonContext() {
53
- return buildCommonContext(
72
+ var commonContext = buildCommonContext(
54
73
  globalContextManager,
55
74
  userContextManager,
56
75
  recorderApi
57
76
  )
77
+ var currentVisitorId = visitorId.getId()
78
+ if (currentVisitorId) {
79
+ commonContext.context[VISITOR_ID_CONTEXT_KEY] = currentVisitorId
80
+ // Keep the SDK value separate from customer fields in buffered snapshots.
81
+ commonContext.visitorId = currentVisitorId
82
+ }
83
+ return commonContext
58
84
  }
59
85
  let strategy
60
86
  function addPluginError(providedError) {
@@ -79,6 +105,10 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
79
105
  rum: publicApi,
80
106
  initConfiguration: initConfiguration
81
107
  })
108
+ },
109
+ onConfigurationReady: function (configuration) {
110
+ visitorId.stop()
111
+ visitorId = startVisitorIdImpl(configuration)
82
112
  }
83
113
  }),
84
114
  getCommonContext,
@@ -197,10 +227,10 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
197
227
 
198
228
  /** @deprecated: use getGlobalContext instead */
199
229
  getRumGlobalContext: monitor(function () {
200
- return globalContextManager.getContext()
230
+ return getGlobalContext()
201
231
  }),
202
232
  getGlobalContext: monitor(function () {
203
- return globalContextManager.getContext()
233
+ return getGlobalContext()
204
234
  }),
205
235
 
206
236
  /** @deprecated: use setGlobalContext instead */
@@ -216,6 +246,10 @@ export function makeRumPublicApi(startRumImpl, recorderApi, options) {
216
246
  clearGlobalContext: monitor(function () {
217
247
  return globalContextManager.clearContext()
218
248
  }),
249
+ resetVisitorId: monitor(function () {
250
+ visitorId.reset()
251
+ addTelemetryUsage({ feature: 'reset-visitor-id' })
252
+ }),
219
253
 
220
254
  getInitConfiguration: monitor(function () {
221
255
  return deepClone(strategy.initConfiguration)
@@ -188,8 +188,20 @@ export function startRumAssembly(
188
188
  {},
189
189
  commonContext.context,
190
190
  viewContext.context,
191
- customerContext
191
+ customerContext,
192
+ rawRumEvent.context
192
193
  )
194
+ if (configuration.visitorId && configuration.visitorId.enabled) {
195
+ var visitorId =
196
+ (rawRumEvent.type === RumEventType.RESOURCE &&
197
+ rawRumEvent.context &&
198
+ rawRumEvent.context.visitor_id) ||
199
+ commonContext.visitorId ||
200
+ (savedCommonContext && getCommonContext().visitorId)
201
+ if (visitorId) {
202
+ context.visitor_id = visitorId
203
+ }
204
+ }
193
205
  if (!isEmptyObject(context)) {
194
206
  serverRumEvent.context = context
195
207
  }
@@ -14,11 +14,14 @@ import {
14
14
  getOrigin,
15
15
  each,
16
16
  isNumber,
17
+ isString,
18
+ getType,
17
19
  ExperimentalFeature
18
20
  } from '@cloudcare/browser-core'
19
21
  import { buildEnv } from '../boot/buildEnv'
20
22
  import { isTracingOption } from '../domain/tracing/tracer'
21
23
  import { validatePlugins } from './plugins'
24
+ import { DEFAULT_VISITOR_ID_TTL_HOURS } from './visitorId'
22
25
 
23
26
  var REPLAY_CANVAS_QUALITY_PRESETS = {
24
27
  low: {
@@ -61,6 +64,65 @@ var POSITIVE_REPLAY_CANVAS_OPTIONS = [
61
64
  ['replayCanvasAutoMaxPerRun', 'Auto Max Per Run']
62
65
  ]
63
66
 
67
+ var HTTP_HEADER_NAME_PATTERN = /^[!#$%&'*+\-.^_`|~0-9a-z]+$/
68
+ var FORBIDDEN_VISITOR_ID_HEADERS = [
69
+ 'b3',
70
+ 'traceparent',
71
+ 'tracestate',
72
+ 'uber-trace-id',
73
+ 'sw8',
74
+ 'x-gc-trace-id',
75
+ 'x-gc-span-id',
76
+ 'x-rum-resource-id'
77
+ ]
78
+ var FORBIDDEN_VISITOR_ID_HEADER_FRAGMENTS = [
79
+ 'authorization',
80
+ 'authentication',
81
+ 'api-key',
82
+ 'api_key',
83
+ 'apikey',
84
+ 'auth-token',
85
+ 'auth_token',
86
+ 'access-token',
87
+ 'access_token',
88
+ 'session-token',
89
+ 'session_token',
90
+ 'refresh-token',
91
+ 'refresh_token',
92
+ 'id-token',
93
+ 'id_token',
94
+ 'security-token',
95
+ 'security_token',
96
+ 'csrf',
97
+ 'xsrf',
98
+ 'jwt',
99
+ 'signature'
100
+ ]
101
+ var FORBIDDEN_REQUEST_HEADERS = [
102
+ 'accept-charset',
103
+ 'accept-encoding',
104
+ 'access-control-request-headers',
105
+ 'access-control-request-method',
106
+ 'authorization',
107
+ 'connection',
108
+ 'content-length',
109
+ 'cookie',
110
+ 'cookie2',
111
+ 'date',
112
+ 'dnt',
113
+ 'expect',
114
+ 'host',
115
+ 'keep-alive',
116
+ 'origin',
117
+ 'permissions-policy',
118
+ 'referer',
119
+ 'te',
120
+ 'trailer',
121
+ 'transfer-encoding',
122
+ 'upgrade',
123
+ 'via'
124
+ ]
125
+
64
126
  function getReplayCanvasQualityPreset(value) {
65
127
  return typeof value === 'string'
66
128
  ? REPLAY_CANVAS_QUALITY_PRESETS[value]
@@ -108,6 +170,12 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
108
170
  if (!allowedTracingUrls) {
109
171
  return
110
172
  }
173
+ var visitorId = validateAndBuildVisitorIdConfiguration(
174
+ initConfiguration.visitorId
175
+ )
176
+ if (!visitorId) {
177
+ return
178
+ }
111
179
  // if (initConfiguration.allowedTracingOrigins !== undefined) {
112
180
  // if (!isArray(initConfiguration.allowedTracingOrigins)) {
113
181
  // display.error('Allowed Tracing Origins should be an array')
@@ -253,6 +321,7 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
253
321
  allowTraceHeaderWithoutSession:
254
322
  !!initConfiguration.allowTraceHeaderWithoutSession,
255
323
  allowedTracingUrls: allowedTracingUrls,
324
+ visitorId: visitorId,
256
325
  injectTraceHeader:
257
326
  initConfiguration.injectTraceHeader &&
258
327
  catchUserErrors(
@@ -364,6 +433,91 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
364
433
  )
365
434
  }
366
435
 
436
+ export function validateAndBuildVisitorIdConfiguration(visitorId) {
437
+ if (visitorId === undefined) {
438
+ return { enabled: false }
439
+ }
440
+ if (getType(visitorId) !== 'object') {
441
+ display.error('Visitor ID configuration should be an object')
442
+ return
443
+ }
444
+ if (
445
+ visitorId.enabled !== undefined &&
446
+ typeof visitorId.enabled !== 'boolean'
447
+ ) {
448
+ display.error('Visitor ID enabled should be a boolean')
449
+ return
450
+ }
451
+ if (visitorId.enabled !== true) {
452
+ return { enabled: false }
453
+ }
454
+ if (!isValidVisitorIdHeader(visitorId.header)) {
455
+ display.error(
456
+ 'Visitor ID header should be a lowercase, writable HTTP header name and must not overlap authentication, CSRF, signature, tracing, or SDK resource tracking headers'
457
+ )
458
+ return
459
+ }
460
+ if (
461
+ !isArray(visitorId.match) ||
462
+ visitorId.match.length === 0 ||
463
+ visitorId.match.some(function (matchOption) {
464
+ return (
465
+ !isMatchOption(matchOption) ||
466
+ (isString(matchOption) && matchOption.length === 0)
467
+ )
468
+ })
469
+ ) {
470
+ display.error(
471
+ 'Visitor ID match should be a non-empty array of strings, RegExp values, or functions'
472
+ )
473
+ return
474
+ }
475
+ if (
476
+ visitorId.ttlHours !== undefined &&
477
+ (!isNumber(visitorId.ttlHours) ||
478
+ !isFinite(visitorId.ttlHours) ||
479
+ visitorId.ttlHours <= 0)
480
+ ) {
481
+ display.error('Visitor ID TTL should be a positive number of hours')
482
+ return
483
+ }
484
+
485
+ return {
486
+ enabled: true,
487
+ header: visitorId.header,
488
+ match: visitorId.match.slice(),
489
+ ttlHours: isNullUndefinedDefaultValue(
490
+ visitorId.ttlHours,
491
+ DEFAULT_VISITOR_ID_TTL_HOURS
492
+ )
493
+ }
494
+ }
495
+
496
+ function isValidVisitorIdHeader(header) {
497
+ if (
498
+ !isString(header) ||
499
+ header !== header.toLowerCase() ||
500
+ !HTTP_HEADER_NAME_PATTERN.test(header)
501
+ ) {
502
+ return false
503
+ }
504
+ if (
505
+ header.indexOf('proxy-') === 0 ||
506
+ header.indexOf('sec-') === 0 ||
507
+ header.indexOf('x-datadog-') === 0 ||
508
+ header.indexOf('x-b3-') === 0 ||
509
+ FORBIDDEN_VISITOR_ID_HEADER_FRAGMENTS.some(function (fragment) {
510
+ return header.indexOf(fragment) !== -1
511
+ })
512
+ ) {
513
+ return false
514
+ }
515
+ return (
516
+ FORBIDDEN_REQUEST_HEADERS.indexOf(header) === -1 &&
517
+ FORBIDDEN_VISITOR_ID_HEADERS.indexOf(header) === -1
518
+ )
519
+ }
520
+
367
521
  /**
368
522
  * Handles allowedTracingUrls and processes legacy allowedTracingOrigins
369
523
  */
@@ -2,6 +2,7 @@ import {
2
2
  RequestType,
3
3
  initFetchObservable,
4
4
  initXhrObservable,
5
+ isRequest,
5
6
  LifeCycleEventType,
6
7
  tryToClone,
7
8
  readBytesFromStream,
@@ -51,6 +52,7 @@ export function trackXhr(lifeCycle, configuration, tracer) {
51
52
  status: context.status,
52
53
  traceId: context.traceId,
53
54
  traceSampled: context.traceSampled,
55
+ visitorId: context.visitorId,
54
56
  type: RequestType.XHR,
55
57
  url: context.url,
56
58
  xhr: context.xhr,
@@ -99,9 +101,10 @@ export function trackFetch(lifeCycle, configuration, tracer) {
99
101
  status: context.status,
100
102
  traceId: context.traceId,
101
103
  traceSampled: context.traceSampled,
104
+ visitorId: context.visitorId,
102
105
  requestHeaderContexts:
103
106
  context.init?.headers ||
104
- (context.input instanceof Request
107
+ (isRequest(context.input)
105
108
  ? context.input.headers
106
109
  : undefined),
107
110
  type: RequestType.FETCH,
@@ -243,6 +243,9 @@ function processRequest(request, pageStateHistory, requestResourceEntryMatcher)
243
243
  tracingInfo,
244
244
  correspondingTimingOverrides
245
245
  )
246
+ if (request.visitorId) {
247
+ resourceEvent.context = { visitor_id: request.visitorId }
248
+ }
246
249
  return {
247
250
  startTime: startClocks.relative,
248
251
  rawRumEvent: resourceEvent,
@@ -10,7 +10,9 @@ import {
10
10
  find,
11
11
  isMatchOption,
12
12
  matchList,
13
- isString
13
+ isString,
14
+ isRequest,
15
+ display
14
16
  } from '@cloudcare/browser-core'
15
17
  import { DDtraceTracer } from './ddtraceTracer'
16
18
  import { SkyWalkingTracer } from './skywalkingTracer'
@@ -45,7 +47,7 @@ export function startTracer(configuration, sessionManager) {
45
47
  sessionManager,
46
48
  function (tracingHeaders) {
47
49
  if (
48
- context.input instanceof Request &&
50
+ isRequest(context.input) &&
49
51
  (!context.init || !context.init.headers)
50
52
  ) {
51
53
  context.input = new Request(context.input)
@@ -160,6 +162,17 @@ export function injectHeadersIfTracingAllowed(
160
162
  if (getType(result) === 'object') {
161
163
  each(result, function (value, key) {
162
164
  if (getType(value) === 'string') {
165
+ if (
166
+ context.visitorId &&
167
+ configuration.visitorId &&
168
+ configuration.visitorId.enabled &&
169
+ key.toLowerCase() === configuration.visitorId.header
170
+ ) {
171
+ display.warn(
172
+ 'injectTraceHeader cannot overwrite the SDK-managed Visitor ID header.'
173
+ )
174
+ return
175
+ }
163
176
  headers[key] = value
164
177
  }
165
178
  })