@cloudcare/browser-rum 3.0.16 → 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.
Files changed (40) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/domain/assembly.js +27 -8
  4. package/cjs/domain/configuration.js +130 -14
  5. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +1 -1
  6. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +2 -2
  7. package/cjs/domain/rumEventsCollection/view/viewCollection.js +1 -1
  8. package/cjs/domain/tracing/ddtraceTracer.js +6 -43
  9. package/cjs/domain/tracing/jaegerTracer.js +8 -27
  10. package/cjs/domain/tracing/traceIdentifier.js +49 -0
  11. package/cjs/domain/tracing/tracer.js +25 -19
  12. package/cjs/domain/tracing/w3cTraceParentTracer.js +11 -22
  13. package/cjs/domain/tracing/zipkinMultiTracer.js +9 -28
  14. package/cjs/domain/tracing/zipkinSingleTracer.js +7 -20
  15. package/esm/boot/buildEnv.js +1 -1
  16. package/esm/domain/assembly.js +28 -9
  17. package/esm/domain/configuration.js +126 -14
  18. package/esm/domain/rumEventsCollection/actions/actionCollection.js +1 -1
  19. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +2 -2
  20. package/esm/domain/rumEventsCollection/view/viewCollection.js +1 -1
  21. package/esm/domain/tracing/ddtraceTracer.js +5 -42
  22. package/esm/domain/tracing/jaegerTracer.js +8 -27
  23. package/esm/domain/tracing/traceIdentifier.js +40 -0
  24. package/esm/domain/tracing/tracer.js +23 -20
  25. package/esm/domain/tracing/w3cTraceParentTracer.js +11 -22
  26. package/esm/domain/tracing/zipkinMultiTracer.js +9 -28
  27. package/esm/domain/tracing/zipkinSingleTracer.js +7 -20
  28. package/package.json +3 -3
  29. package/src/domain/assembly.js +47 -23
  30. package/src/domain/configuration.js +148 -19
  31. package/src/domain/rumEventsCollection/actions/actionCollection.js +1 -1
  32. package/src/domain/rumEventsCollection/resource/resourceCollection.js +2 -2
  33. package/src/domain/rumEventsCollection/view/viewCollection.js +1 -1
  34. package/src/domain/tracing/ddtraceTracer.js +5 -46
  35. package/src/domain/tracing/jaegerTracer.js +11 -24
  36. package/src/domain/tracing/traceIdentifier.js +43 -0
  37. package/src/domain/tracing/tracer.js +34 -27
  38. package/src/domain/tracing/w3cTraceParentTracer.js +17 -21
  39. package/src/domain/tracing/zipkinMultiTracer.js +11 -23
  40. package/src/domain/tracing/zipkinSingleTracer.js +8 -18
@@ -1,48 +1,29 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
3
- var digits = '0123456789abcdef';
4
- var n = '';
5
-
6
- for (var i = 0; i < 16; i += 1) {
7
- var rand = Math.floor(Math.random() * 16);
8
- n += digits[rand];
9
- }
10
-
11
- return n;
12
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier';
13
2
  /**
14
3
  *
15
4
  * @param {*} configuration 配置信息
16
5
  */
17
6
 
18
-
19
7
  export function ZipkinMultiTracer(configuration, traceSampled) {
20
- var rootSpanId = randomTraceId();
21
-
22
- if (configuration.traceId128Bit) {
23
- // 128bit生成traceid
24
- this._traceId = randomTraceId() + rootSpanId;
25
- } else {
26
- this._traceId = rootSpanId;
27
- }
28
-
29
- this._spanId = rootSpanId;
8
+ this._traceId = new TraceIdentifier();
9
+ this._spanId = new TraceIdentifier();
30
10
  this._traceSampled = traceSampled;
11
+ this.is128Bit = configuration.traceId128Bit;
31
12
  }
32
13
  ZipkinMultiTracer.prototype = {
33
14
  isTracingSupported: function isTracingSupported() {
34
- return true;
15
+ return getCrypto() !== undefined;
35
16
  },
36
17
  getSpanId: function getSpanId() {
37
- return this._spanId;
18
+ return this._spanId.toPaddedHexadecimalString();
38
19
  },
39
20
  getTraceId: function getTraceId() {
40
- return this._traceId;
21
+ return this.is128Bit ? '0000000000000000' + this._traceId.toPaddedHexadecimalString() : this._traceId.toPaddedHexadecimalString();
41
22
  },
42
23
  makeTracingHeaders: function makeTracingHeaders() {
43
24
  return {
44
- 'X-B3-TraceId': this.getTraceId(),
45
- 'X-B3-SpanId': this.getSpanId(),
25
+ 'X-B3-TraceId': this.getSpanId(),
26
+ 'X-B3-SpanId': this.getTraceId(),
46
27
  // 'X-B3-ParentSpanId': '',
47
28
  'X-B3-Sampled': this._traceSampled ? '1' : '0' // 'X-B3-Flags': '0'
48
29
 
@@ -1,40 +1,27 @@
1
- // === Generate a random 64-bit number in fixed-length hex format
2
- function randomTraceId() {
3
- var digits = '0123456789abcdef';
4
- var n = '';
5
-
6
- for (var i = 0; i < 16; i += 1) {
7
- var rand = Math.floor(Math.random() * 16);
8
- n += digits[rand];
9
- }
10
-
11
- return n;
12
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier';
13
2
  /**
14
3
  *
15
4
  * @param {*} configuration 配置信息
16
5
  */
17
6
 
18
-
19
7
  export function ZipkinSingleTracer(configuration, traceSampled) {
20
- var rootSpanId = randomTraceId();
21
- this._traceId = randomTraceId() + rootSpanId;
22
- this._spanId = rootSpanId;
8
+ this._traceId = new TraceIdentifier();
9
+ this._spanId = new TraceIdentifier();
23
10
  this._traceSampled = traceSampled;
24
11
  }
25
12
  ZipkinSingleTracer.prototype = {
26
13
  isTracingSupported: function isTracingSupported() {
27
- return true;
14
+ return getCrypto() !== undefined;
28
15
  },
29
16
  getSpanId: function getSpanId() {
30
- return this._spanId;
17
+ return this._spanId.toPaddedHexadecimalString();
31
18
  },
32
19
  getTraceId: function getTraceId() {
33
- return this._traceId;
20
+ return this._traceId.toPaddedHexadecimalString();
34
21
  },
35
22
  getB3Str: function getB3Str() {
36
23
  //{TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
37
- return this._traceId + '-' + this._spanId + '-' + (this._traceSampled ? '1' : '0');
24
+ return this.getTraceId() + '-' + this.getSpanId() + '-' + (this._traceSampled ? '1' : '0');
38
25
  },
39
26
  makeTracingHeaders: function makeTracingHeaders() {
40
27
  return {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@cloudcare/browser-rum",
3
- "version": "3.0.16",
3
+ "version": "3.0.18",
4
4
  "main": "cjs/index.js",
5
5
  "module": "esm/index.js",
6
6
  "dependencies": {
7
- "@cloudcare/browser-core": "2.0.14"
7
+ "@cloudcare/browser-core": "2.0.16"
8
8
  },
9
9
  "scripts": {
10
10
  "dev": "webpack serve --open --mode=development",
@@ -29,5 +29,5 @@
29
29
  "license": "MIT",
30
30
  "homepage": "https://github.com/CloudCare/dataflux-rum-sdk-javascript/blob/master/packages/rum/README.md",
31
31
  "description": "DataFlux RUM Web 端数据指标监控",
32
- "gitHead": "1aee6e645bb9cfe3f31fe88008e83c74087ce41c"
32
+ "gitHead": "d68cc2e25637dbe6a09461ca4186b882f878c169"
33
33
  }
@@ -9,29 +9,26 @@ import {
9
9
  currentDrift,
10
10
  createEventRateLimiter,
11
11
  limitModification,
12
- display
12
+ display,
13
+ assign
13
14
  } from '@cloudcare/browser-core'
14
15
  var SessionType = {
15
16
  SYNTHETICS: 'synthetics',
16
17
  USER: 'user'
17
18
  }
18
19
 
19
- var VIEW_EVENTS_MODIFIABLE_FIELD_PATHS = [
20
- // Fields with sensitive data
21
- 'view.url',
22
- 'view.referrer',
23
- 'action.target.name',
24
- 'error.message',
25
- 'error.stack',
26
- 'error.resource.url',
27
- 'resource.url'
28
- ]
20
+ var VIEW_MODIFIABLE_FIELD_PATHS = {
21
+ 'view.url': 'string',
22
+ 'view.referrer': 'string'
23
+ }
24
+
25
+ var USER_CUSTOMIZABLE_FIELD_PATHS = {
26
+ tags: 'object',
27
+ '_gc.trace_id': 'string',
28
+ '_gc.span_id': 'string'
29
+ }
29
30
 
30
- var OTHER_EVENTS_MODIFIABLE_FIELD_PATHS =
31
- VIEW_EVENTS_MODIFIABLE_FIELD_PATHS.concat([
32
- // User-customizable field
33
- 'tags'
34
- ])
31
+ var modifiableFieldPathsByEvent = {}
35
32
  export function startRumAssembly(
36
33
  configuration,
37
34
  lifeCycle,
@@ -43,6 +40,35 @@ export function startRumAssembly(
43
40
  buildCommonContext,
44
41
  reportError
45
42
  ) {
43
+ modifiableFieldPathsByEvent[RumEventType.VIEW] = VIEW_MODIFIABLE_FIELD_PATHS
44
+ modifiableFieldPathsByEvent[RumEventType.ERROR] = assign(
45
+ {
46
+ 'error.message': 'string',
47
+ 'error.stack': 'string',
48
+ 'error.resource.url': 'string'
49
+ },
50
+ USER_CUSTOMIZABLE_FIELD_PATHS,
51
+ VIEW_MODIFIABLE_FIELD_PATHS
52
+ )
53
+ modifiableFieldPathsByEvent[RumEventType.RESOURCE] = assign(
54
+ {
55
+ 'resource.url': 'string'
56
+ },
57
+ USER_CUSTOMIZABLE_FIELD_PATHS,
58
+ VIEW_MODIFIABLE_FIELD_PATHS
59
+ )
60
+ modifiableFieldPathsByEvent[RumEventType.ACTION] = assign(
61
+ {
62
+ 'action.target.name': 'string'
63
+ },
64
+ USER_CUSTOMIZABLE_FIELD_PATHS,
65
+ VIEW_MODIFIABLE_FIELD_PATHS
66
+ )
67
+ modifiableFieldPathsByEvent[RumEventType.LONG_TASK] = assign(
68
+ {},
69
+ USER_CUSTOMIZABLE_FIELD_PATHS,
70
+ VIEW_MODIFIABLE_FIELD_PATHS
71
+ )
46
72
  var eventRateLimiters = {}
47
73
  eventRateLimiters[RumEventType.ERROR] = createEventRateLimiter(
48
74
  RumEventType.ERROR,
@@ -72,7 +98,7 @@ export function startRumAssembly(
72
98
  var actionId = actionContexts.findActionId(startTime)
73
99
  var commonContext = savedCommonContext || buildCommonContext()
74
100
  var rumContext = {
75
- _dd: {
101
+ _gc: {
76
102
  sdkName: configuration.sdkName,
77
103
  sdkVersion: configuration.sdkVersion,
78
104
  drift: currentDrift()
@@ -139,8 +165,8 @@ export function startRumAssembly(
139
165
  shouldSend(
140
166
  serverRumEvent,
141
167
  configuration.beforeSend,
142
- eventRateLimiters,
143
- domainContext
168
+ domainContext,
169
+ eventRateLimiters
144
170
  )
145
171
  ) {
146
172
  if (isEmptyObject(serverRumEvent.tags)) {
@@ -156,13 +182,11 @@ export function startRumAssembly(
156
182
  )
157
183
  }
158
184
 
159
- function shouldSend(event, beforeSend, eventRateLimiters, domainContext) {
185
+ function shouldSend(event, beforeSend, domainContext, eventRateLimiters) {
160
186
  if (beforeSend) {
161
187
  var result = limitModification(
162
188
  event,
163
- event.type === RumEventType.VIEW
164
- ? VIEW_EVENTS_MODIFIABLE_FIELD_PATHS
165
- : OTHER_EVENTS_MODIFIABLE_FIELD_PATHS,
189
+ modifiableFieldPathsByEvent[event.type],
166
190
  function (event) {
167
191
  return beforeSend(event, domainContext)
168
192
  }
@@ -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
+ }
@@ -66,7 +66,7 @@ function processAction(action, foregroundContexts) {
66
66
  count: action.counts.resourceCount
67
67
  }
68
68
  },
69
- _dd: {
69
+ _gc: {
70
70
  action: {
71
71
  target: action.target,
72
72
  position: action.position
@@ -166,7 +166,7 @@ function computeRequestTracingInfo(request) {
166
166
  return undefined
167
167
  }
168
168
  return {
169
- _dd: {
169
+ _gc: {
170
170
  spanId: request.spanId,
171
171
  traceId: request.traceId
172
172
  },
@@ -175,5 +175,5 @@ function computeRequestTracingInfo(request) {
175
175
  }
176
176
 
177
177
  function computeEntryTracingInfo(entry) {
178
- return entry.traceId ? { _dd: { traceId: entry.traceId } } : undefined
178
+ return entry.traceId ? { _gc: { traceId: entry.traceId } } : undefined
179
179
  }
@@ -78,7 +78,7 @@ function computePerformanceViewDetails(entry) {
78
78
  function processViewUpdate(view, foregroundContexts, recorderApi) {
79
79
  var replayStats = recorderApi.getReplayStats(view.id)
80
80
  var viewEvent = {
81
- _dd: {
81
+ _gc: {
82
82
  document_version: view.documentVersion,
83
83
  replay_stats: replayStats
84
84
  },
@@ -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
+ }