@cloudcare/browser-rum 3.0.17 → 3.0.18

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.
@@ -7,10 +7,13 @@ import {
7
7
  TraceType,
8
8
  isNullUndefinedDefaultValue,
9
9
  DefaultPrivacyLevel,
10
- objectHasValue
10
+ objectHasValue,
11
+ isMatchOption,
12
+ getOrigin,
13
+ each
11
14
  } from '@cloudcare/browser-core'
12
15
  import { buildEnv } from '../boot/buildEnv'
13
-
16
+ import { isTracingOption } from '../domain/tracing/tracer'
14
17
  export function validateAndBuildRumConfiguration(initConfiguration) {
15
18
  if (!initConfiguration.applicationId) {
16
19
  display.error(
@@ -32,19 +35,22 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
32
35
  display.error('Premium Sample Rate should be a number between 0 and 100')
33
36
  return
34
37
  }
35
-
36
- if (initConfiguration.allowedTracingOrigins !== undefined) {
37
- if (!isArray(initConfiguration.allowedTracingOrigins)) {
38
- display.error('Allowed Tracing Origins should be an array')
39
- return
40
- }
41
- }
42
- if (initConfiguration.allowedDDTracingOrigins !== undefined) {
43
- if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
44
- display.error('Allowed Tracing Origins should be an array')
45
- return
46
- }
38
+ var allowedTracingUrls = validateAndBuildTracingOptions(initConfiguration)
39
+ if (!allowedTracingUrls) {
40
+ return
47
41
  }
42
+ // if (initConfiguration.allowedTracingOrigins !== undefined) {
43
+ // if (!isArray(initConfiguration.allowedTracingOrigins)) {
44
+ // display.error('Allowed Tracing Origins should be an array')
45
+ // return
46
+ // }
47
+ // }
48
+ // if (initConfiguration.allowedDDTracingOrigins !== undefined) {
49
+ // if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
50
+ // display.error('Allowed Tracing Origins should be an array')
51
+ // return
52
+ // }
53
+ // }
48
54
  if (
49
55
  initConfiguration.tracingSampleRate !== undefined &&
50
56
  !isPercentage(initConfiguration.tracingSampleRate)
@@ -79,11 +85,7 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
79
85
  100
80
86
  ),
81
87
  tracingSampleRate: initConfiguration.tracingSampleRate,
82
- allowedTracingOrigins: isNullUndefinedDefaultValue(
83
- initConfiguration.allowedTracingOrigins ||
84
- initConfiguration.allowedDDTracingOrigins,
85
- []
86
- ),
88
+ allowedTracingUrls: allowedTracingUrls,
87
89
  excludedActivityUrls: isNullUndefinedDefaultValue(
88
90
  initConfiguration.excludedActivityUrls,
89
91
  []
@@ -108,3 +110,130 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
108
110
  buildEnv
109
111
  )
110
112
  }
113
+ /**
114
+ * Handles allowedTracingUrls and processes legacy allowedTracingOrigins
115
+ */
116
+ function validateAndBuildTracingOptions(initConfiguration) {
117
+ // Advise about parameters precedence.
118
+ if (
119
+ initConfiguration.allowedTracingUrls !== undefined &&
120
+ initConfiguration.allowedTracingOrigins !== undefined
121
+ ) {
122
+ display.warn(
123
+ 'Both allowedTracingUrls and allowedTracingOrigins (deprecated) have been defined. The parameter allowedTracingUrls will override allowedTracingOrigins.'
124
+ )
125
+ }
126
+ // Handle allowedTracingUrls first
127
+ if (initConfiguration.allowedTracingUrls !== undefined) {
128
+ if (!isArray(initConfiguration.allowedTracingUrls)) {
129
+ display.error('Allowed Tracing URLs should be an array')
130
+ return
131
+ }
132
+ // if (initConfiguration.allowedTracingUrls.length !== 0 && initConfiguration.service === undefined) {
133
+ // display.error('Service needs to be configured when tracing is enabled')
134
+ // return
135
+ // }
136
+ // Convert from (MatchOption | TracingOption) to TracingOption, remove unknown properties
137
+ var tracingOptions = []
138
+ each(initConfiguration.allowedTracingUrls, function (option) {
139
+ if (isMatchOption(option)) {
140
+ tracingOptions.push({
141
+ match: option,
142
+ traceType: isNullUndefinedDefaultValue(
143
+ initConfiguration.traceType,
144
+ TraceType.DDTRACE
145
+ )
146
+ })
147
+ } else if (isTracingOption(option)) {
148
+ tracingOptions.push(option)
149
+ } else {
150
+ display.warn(
151
+ 'Allowed Tracing Urls parameters should be a string, RegExp, function, or an object. Ignoring parameter',
152
+ option
153
+ )
154
+ }
155
+ })
156
+ return tracingOptions
157
+ }
158
+
159
+ // Handle conversion of allowedTracingOrigins to allowedTracingUrls
160
+ if (initConfiguration.allowedTracingOrigins !== undefined) {
161
+ if (!isArray(initConfiguration.allowedTracingOrigins)) {
162
+ display.error('Allowed Tracing Origins should be an array')
163
+ return
164
+ }
165
+
166
+ var tracingOptions = []
167
+ each(initConfiguration.allowedTracingOrigins, function (legacyMatchOption) {
168
+ var tracingOption = convertLegacyMatchOptionToTracingOption(
169
+ legacyMatchOption,
170
+ isNullUndefinedDefaultValue(
171
+ initConfiguration.traceType,
172
+ TraceType.DDTRACE
173
+ )
174
+ )
175
+ if (tracingOption) {
176
+ tracingOptions.push(tracingOption)
177
+ }
178
+ })
179
+
180
+ return tracingOptions
181
+ }
182
+ // Handle conversion of allowedDDTracingOrigins to allowedTracingUrls
183
+ if (initConfiguration.allowedDDTracingOrigins !== undefined) {
184
+ if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
185
+ display.error('Allowed Tracing Origins should be an array')
186
+ return
187
+ }
188
+
189
+ var tracingOptions = []
190
+ each(
191
+ initConfiguration.allowedDDTracingOrigins,
192
+ function (legacyMatchOption) {
193
+ var tracingOption = convertLegacyMatchOptionToTracingOption(
194
+ legacyMatchOption,
195
+ isNullUndefinedDefaultValue(
196
+ initConfiguration.traceType,
197
+ TraceType.DDTRACE
198
+ )
199
+ )
200
+ if (tracingOption) {
201
+ tracingOptions.push(tracingOption)
202
+ }
203
+ }
204
+ )
205
+
206
+ return tracingOptions
207
+ }
208
+
209
+ return []
210
+ }
211
+
212
+ /**
213
+ * Converts parameters from the deprecated allowedTracingOrigins
214
+ * to allowedTracingUrls. Handles the change from origin to full URLs.
215
+ */
216
+ function convertLegacyMatchOptionToTracingOption(item, traceType) {
217
+ var match
218
+ if (typeof item === 'string') {
219
+ match = item
220
+ } else if (item instanceof RegExp) {
221
+ match = function (url) {
222
+ return item.test(getOrigin(url))
223
+ }
224
+ } else if (typeof item === 'function') {
225
+ match = function (url) {
226
+ return item(getOrigin(url))
227
+ }
228
+ }
229
+
230
+ if (match === undefined) {
231
+ display.warn(
232
+ 'Allowed Tracing Origins parameters should be a string, RegExp or function. Ignoring parameter',
233
+ item
234
+ )
235
+ return undefined
236
+ }
237
+
238
+ return { match: match, traceType: traceType }
239
+ }
@@ -1,52 +1,11 @@
1
- function TraceIdentifier() {
2
- this.buffer = new Uint8Array(8)
3
- getCrypto().getRandomValues(this.buffer)
4
- this.buffer[0] = this.buffer[0] & 0x7f
5
- }
6
-
7
- TraceIdentifier.prototype = {
8
- // buffer: new Uint8Array(8),
9
- toString: function (radix) {
10
- var high = this.readInt32(0)
11
- var low = this.readInt32(4)
12
- var str = ''
13
-
14
- while (1) {
15
- var mod = (high % radix) * 4294967296 + low
16
-
17
- high = Math.floor(high / radix)
18
- low = Math.floor(mod / radix)
19
- str = (mod % radix).toString(radix) + str
20
-
21
- if (!high && !low) {
22
- break
23
- }
24
- }
25
- return str
26
- },
27
- toDecimalString: function () {
28
- return this.toString(10)
29
- },
30
-
31
- readInt32: function (offset) {
32
- return (
33
- this.buffer[offset] * 16777216 +
34
- (this.buffer[offset + 1] << 16) +
35
- (this.buffer[offset + 2] << 8) +
36
- this.buffer[offset + 3]
37
- )
38
- }
39
- }
40
- function getCrypto() {
41
- return window.crypto || window.msCrypto
42
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier'
43
2
  /**
44
3
  *
45
4
  * @param {*} configuration 配置信息
46
5
  */
47
6
  export function DDtraceTracer(traceSampled) {
48
- this._spanId = new TraceIdentifier().toDecimalString()
49
- this._traceId = new TraceIdentifier().toDecimalString()
7
+ this._spanId = new TraceIdentifier()
8
+ this._traceId = new TraceIdentifier()
50
9
  this._traceSampled = traceSampled
51
10
  }
52
11
  DDtraceTracer.prototype = {
@@ -54,10 +13,10 @@ DDtraceTracer.prototype = {
54
13
  return getCrypto() !== undefined
55
14
  },
56
15
  getSpanId: function () {
57
- return this._spanId
16
+ return this._spanId.toDecimalString()
58
17
  },
59
18
  getTraceId: function () {
60
- return this._traceId
19
+ return this._traceId.toDecimalString()
61
20
  },
62
21
  makeTracingHeaders: function () {
63
22
  return {
@@ -1,46 +1,33 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
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
- n += digits[rand]
8
- }
9
- return n
10
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier'
11
2
 
12
3
  /**
13
4
  *
14
5
  * @param {*} configuration 配置信息
15
6
  */
16
7
  export function JaegerTracer(configuration, traceSampled) {
17
- var rootSpanId = randomTraceId()
18
- // this._traceId = randomTraceId() + rootSpanId // 默认用128bit,兼容其他配置
19
- if (configuration.traceId128Bit) {
20
- // 128bit生成traceid
21
- this._traceId = randomTraceId() + rootSpanId
22
- } else {
23
- this._traceId = rootSpanId
24
- }
25
- this._spanId = rootSpanId
8
+ this._traceId = new TraceIdentifier()
9
+ this._spanId = new TraceIdentifier()
26
10
  this._traceSampled = traceSampled
11
+ this.is128Bit = configuration.traceId128Bit
27
12
  }
28
13
  JaegerTracer.prototype = {
29
14
  isTracingSupported: function () {
30
- return true
15
+ return getCrypto() !== undefined
31
16
  },
32
17
  getSpanId: function () {
33
- return this._spanId
18
+ return this._spanId.toPaddedHexadecimalString()
34
19
  },
35
20
  getTraceId: function () {
36
- return this._traceId
21
+ return this.is128Bit
22
+ ? '0000000000000000' + this._traceId.toPaddedHexadecimalString()
23
+ : this._traceId.toPaddedHexadecimalString()
37
24
  },
38
25
  getUberTraceId: function () {
39
26
  //{trace-id}:{span-id}:{parent-span-id}:{flags}
40
27
  return (
41
- this._traceId +
28
+ this.getTraceId() +
42
29
  ':' +
43
- this._spanId +
30
+ this.getSpanId() +
44
31
  ':' +
45
32
  '0' +
46
33
  ':' +
@@ -0,0 +1,43 @@
1
+ export function TraceIdentifier() {
2
+ this.buffer = new Uint8Array(8)
3
+ getCrypto().getRandomValues(this.buffer)
4
+ this.buffer[0] = this.buffer[0] & 0x7f
5
+ }
6
+
7
+ TraceIdentifier.prototype = {
8
+ // buffer: new Uint8Array(8),
9
+ toString: function (radix) {
10
+ var high = this.readInt32(0)
11
+ var low = this.readInt32(4)
12
+ var str = ''
13
+
14
+ do {
15
+ var mod = (high % radix) * 4294967296 + low
16
+ high = Math.floor(high / radix)
17
+ low = Math.floor(mod / radix)
18
+ str = (mod % radix).toString(radix) + str
19
+ } while (high || low)
20
+ return str
21
+ },
22
+ toDecimalString: function () {
23
+ return this.toString(10)
24
+ },
25
+ /**
26
+ * Format used by OTel headers
27
+ */
28
+ toPaddedHexadecimalString() {
29
+ var traceId = this.toString(16)
30
+ return Array(17 - traceId.length).join('0') + traceId
31
+ },
32
+ readInt32: function (offset) {
33
+ return (
34
+ this.buffer[offset] * 16777216 +
35
+ (this.buffer[offset + 1] << 16) +
36
+ (this.buffer[offset + 2] << 8) +
37
+ this.buffer[offset + 3]
38
+ )
39
+ }
40
+ }
41
+ export function getCrypto() {
42
+ return window.crypto || window.msCrypto
43
+ }
@@ -4,9 +4,13 @@ import {
4
4
  shallowClone,
5
5
  isArray,
6
6
  TraceType,
7
- getOrigin,
8
7
  performDraw,
9
- isNumber
8
+ isNumber,
9
+ getType,
10
+ find,
11
+ isMatchOption,
12
+ matchList,
13
+ isString
10
14
  } from '@cloudcare/browser-core'
11
15
  import { DDtraceTracer } from './ddtraceTracer'
12
16
  import { SkyWalkingTracer } from './skywalkingTracer'
@@ -15,11 +19,19 @@ import { ZipkinSingleTracer } from './zipkinSingleTracer'
15
19
  import { ZipkinMultiTracer } from './zipkinMultiTracer'
16
20
  import { W3cTraceParentTracer } from './w3cTraceParentTracer'
17
21
 
22
+ export function isTracingOption(item) {
23
+ var expectedItem = item
24
+ return (
25
+ getType(expectedItem) === 'object' &&
26
+ isMatchOption(expectedItem.match) &&
27
+ isString(expectedItem.traceType)
28
+ )
29
+ }
18
30
  export function clearTracingIfNeeded(context) {
19
31
  if (context.status === 0 && !context.isAborted) {
20
- context.traceId = undefined
21
- context.spanId = undefined
22
- context.traceSampled = undefined
32
+ // context.traceId = undefined
33
+ // context.spanId = undefined
34
+ // context.traceSampled = undefined
23
35
  }
24
36
  }
25
37
 
@@ -84,20 +96,6 @@ export function startTracer(configuration, sessionManager) {
84
96
  }
85
97
  }
86
98
  }
87
- function isAllowedUrl(configuration, requestUrl) {
88
- var requestOrigin = getOrigin(requestUrl)
89
- var flag = false
90
- each(configuration.allowedTracingOrigins, function (allowedOrigin) {
91
- if (
92
- requestOrigin === allowedOrigin ||
93
- (allowedOrigin instanceof RegExp && allowedOrigin.test(requestOrigin))
94
- ) {
95
- flag = true
96
- return false
97
- }
98
- })
99
- return flag
100
- }
101
99
 
102
100
  export function injectHeadersIfTracingAllowed(
103
101
  configuration,
@@ -105,18 +103,24 @@ export function injectHeadersIfTracingAllowed(
105
103
  sessionManager,
106
104
  inject
107
105
  ) {
108
- if (
109
- !isAllowedUrl(configuration, context.url) ||
110
- !configuration.traceType ||
111
- !sessionManager.findTrackedSession()
112
- ) {
106
+ if (!sessionManager.findTrackedSession()) {
107
+ return
108
+ }
109
+ var tracingOption = find(
110
+ configuration.allowedTracingUrls,
111
+ function (tracingOption) {
112
+ return matchList([tracingOption.match], context.url, true)
113
+ }
114
+ )
115
+ if (!tracingOption) {
113
116
  return
114
117
  }
115
118
  var traceSampled =
116
119
  !isNumber(configuration.tracingSampleRate) ||
117
120
  performDraw(configuration.tracingSampleRate)
118
- var tracer
119
- switch (configuration.traceType) {
121
+ var tracer,
122
+ traceType = tracingOption.traceType
123
+ switch (traceType) {
120
124
  case TraceType.DDTRACE:
121
125
  tracer = new DDtraceTracer(traceSampled)
122
126
  break
@@ -130,7 +134,10 @@ export function injectHeadersIfTracingAllowed(
130
134
  tracer = new JaegerTracer(configuration, traceSampled)
131
135
  break
132
136
  case TraceType.W3C_TRACEPARENT:
133
- tracer = new W3cTraceParentTracer(configuration, traceSampled)
137
+ tracer = new W3cTraceParentTracer(traceSampled)
138
+ break
139
+ case TraceType.W3C_TRACEPARENT_64:
140
+ tracer = new W3cTraceParentTracer(traceSampled, true)
134
141
  break
135
142
  case TraceType.ZIPKIN_SINGLE_HEADER:
136
143
  tracer = new ZipkinSingleTracer(configuration, traceSampled)
@@ -1,41 +1,37 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
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
- n += digits[rand]
8
- }
9
- return n
10
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier'
11
2
 
12
3
  /**
13
4
  *
14
- * @param {*} configuration 配置信息
5
+ * @param {*} traceSampled
6
+ * @param {*} isHexTraceId 是否需要转换成10进制上报数据
15
7
  */
16
- export function W3cTraceParentTracer(configuration, traceSampled) {
17
- var rootSpanId = randomTraceId()
18
- this._traceId = randomTraceId() + rootSpanId
19
- this._spanId = rootSpanId
8
+ export function W3cTraceParentTracer(traceSampled, isHexTraceId) {
9
+ this._traceId = new TraceIdentifier()
10
+ this._spanId = new TraceIdentifier()
20
11
  this._traceSampled = traceSampled
12
+ this.isHexTraceId = isHexTraceId
21
13
  }
22
14
  W3cTraceParentTracer.prototype = {
23
15
  isTracingSupported: function () {
24
- return true
16
+ return getCrypto() !== undefined
25
17
  },
26
18
  getSpanId: function () {
27
- return this._spanId
19
+ return this.isHexTraceId
20
+ ? this._spanId.toDecimalString()
21
+ : this._spanId.toPaddedHexadecimalString()
28
22
  },
29
23
  getTraceId: function () {
30
- return this._traceId
24
+ return this.isHexTraceId
25
+ ? this._traceId.toDecimalString()
26
+ : '0000000000000000' + this._traceId.toPaddedHexadecimalString()
31
27
  },
32
28
  getTraceParent: function () {
33
29
  // '{version}-{traceId}-{spanId}-{sampleDecision}'
34
30
  return (
35
- '00-' +
36
- this._traceId +
31
+ '00-0000000000000000' +
32
+ this._traceId.toPaddedHexadecimalString() +
37
33
  '-' +
38
- this._spanId +
34
+ this._spanId.toPaddedHexadecimalString() +
39
35
  '-' +
40
36
  (this._traceSampled ? '01' : '00')
41
37
  )
@@ -1,44 +1,32 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
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
- n += digits[rand]
8
- }
9
- return n
10
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier'
11
2
 
12
3
  /**
13
4
  *
14
5
  * @param {*} configuration 配置信息
15
6
  */
16
7
  export function ZipkinMultiTracer(configuration, traceSampled) {
17
- var rootSpanId = randomTraceId()
18
- if (configuration.traceId128Bit) {
19
- // 128bit生成traceid
20
- this._traceId = randomTraceId() + rootSpanId
21
- } else {
22
- this._traceId = rootSpanId
23
- }
24
- this._spanId = rootSpanId
8
+ this._traceId = new TraceIdentifier()
9
+ this._spanId = new TraceIdentifier()
25
10
  this._traceSampled = traceSampled
11
+ this.is128Bit = configuration.traceId128Bit
26
12
  }
27
13
  ZipkinMultiTracer.prototype = {
28
14
  isTracingSupported: function () {
29
- return true
15
+ return getCrypto() !== undefined
30
16
  },
31
17
  getSpanId: function () {
32
- return this._spanId
18
+ return this._spanId.toPaddedHexadecimalString()
33
19
  },
34
20
  getTraceId: function () {
35
- return this._traceId
21
+ return this.is128Bit
22
+ ? '0000000000000000' + this._traceId.toPaddedHexadecimalString()
23
+ : this._traceId.toPaddedHexadecimalString()
36
24
  },
37
25
 
38
26
  makeTracingHeaders: function () {
39
27
  return {
40
- 'X-B3-TraceId': this.getTraceId(),
41
- 'X-B3-SpanId': this.getSpanId(),
28
+ 'X-B3-TraceId': this.getSpanId(),
29
+ 'X-B3-SpanId': this.getTraceId(),
42
30
  // 'X-B3-ParentSpanId': '',
43
31
  'X-B3-Sampled': this._traceSampled ? '1' : '0'
44
32
  // 'X-B3-Flags': '0'
@@ -1,40 +1,30 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
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
- n += digits[rand]
8
- }
9
- return n
10
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier'
11
2
 
12
3
  /**
13
4
  *
14
5
  * @param {*} configuration 配置信息
15
6
  */
16
7
  export function ZipkinSingleTracer(configuration, traceSampled) {
17
- var rootSpanId = randomTraceId()
18
- this._traceId = randomTraceId() + rootSpanId
19
- this._spanId = rootSpanId
8
+ this._traceId = new TraceIdentifier()
9
+ this._spanId = new TraceIdentifier()
20
10
  this._traceSampled = traceSampled
21
11
  }
22
12
  ZipkinSingleTracer.prototype = {
23
13
  isTracingSupported: function () {
24
- return true
14
+ return getCrypto() !== undefined
25
15
  },
26
16
  getSpanId: function () {
27
- return this._spanId
17
+ return this._spanId.toPaddedHexadecimalString()
28
18
  },
29
19
  getTraceId: function () {
30
- return this._traceId
20
+ return this._traceId.toPaddedHexadecimalString()
31
21
  },
32
22
  getB3Str: function () {
33
23
  //{TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
34
24
  return (
35
- this._traceId +
25
+ this.getTraceId() +
36
26
  '-' +
37
- this._spanId +
27
+ this.getSpanId() +
38
28
  '-' +
39
29
  (this._traceSampled ? '1' : '0')
40
30
  )