@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
@@ -5,44 +5,31 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.ZipkinSingleTracer = ZipkinSingleTracer;
7
7
 
8
- // === Generate a random 64-bit number in fixed-length hex format
9
- function randomTraceId() {
10
- var digits = '0123456789abcdef';
11
- var n = '';
8
+ var _traceIdentifier = require("./traceIdentifier");
12
9
 
13
- for (var i = 0; i < 16; i += 1) {
14
- var rand = Math.floor(Math.random() * 16);
15
- n += digits[rand];
16
- }
17
-
18
- return n;
19
- }
20
10
  /**
21
11
  *
22
12
  * @param {*} configuration 配置信息
23
13
  */
24
-
25
-
26
14
  function ZipkinSingleTracer(configuration, traceSampled) {
27
- var rootSpanId = randomTraceId();
28
- this._traceId = randomTraceId() + rootSpanId;
29
- this._spanId = rootSpanId;
15
+ this._traceId = new _traceIdentifier.TraceIdentifier();
16
+ this._spanId = new _traceIdentifier.TraceIdentifier();
30
17
  this._traceSampled = traceSampled;
31
18
  }
32
19
 
33
20
  ZipkinSingleTracer.prototype = {
34
21
  isTracingSupported: function isTracingSupported() {
35
- return true;
22
+ return (0, _traceIdentifier.getCrypto)() !== undefined;
36
23
  },
37
24
  getSpanId: function getSpanId() {
38
- return this._spanId;
25
+ return this._spanId.toPaddedHexadecimalString();
39
26
  },
40
27
  getTraceId: function getTraceId() {
41
- return this._traceId;
28
+ return this._traceId.toPaddedHexadecimalString();
42
29
  },
43
30
  getB3Str: function getB3Str() {
44
31
  //{TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
45
- return this._traceId + '-' + this._spanId + '-' + (this._traceSampled ? '1' : '0');
32
+ return this.getTraceId() + '-' + this.getSpanId() + '-' + (this._traceSampled ? '1' : '0');
46
33
  },
47
34
  makeTracingHeaders: function makeTracingHeaders() {
48
35
  return {
@@ -1,4 +1,4 @@
1
1
  export var buildEnv = {
2
- sdkVersion: '3.0.16',
2
+ sdkVersion: '3.0.18',
3
3
  sdkName: 'df_web_rum_sdk'
4
4
  };
@@ -1,13 +1,32 @@
1
- import { extend2Lev, withSnakeCaseKeys, timeStampNow, isEmptyObject, LifeCycleEventType, RumEventType, deviceInfo, currentDrift, createEventRateLimiter, limitModification, display } from '@cloudcare/browser-core';
1
+ import { extend2Lev, withSnakeCaseKeys, timeStampNow, isEmptyObject, LifeCycleEventType, RumEventType, deviceInfo, currentDrift, createEventRateLimiter, limitModification, display, assign } from '@cloudcare/browser-core';
2
2
  var SessionType = {
3
3
  SYNTHETICS: 'synthetics',
4
4
  USER: 'user'
5
5
  };
6
- var VIEW_EVENTS_MODIFIABLE_FIELD_PATHS = [// Fields with sensitive data
7
- 'view.url', 'view.referrer', 'action.target.name', 'error.message', 'error.stack', 'error.resource.url', 'resource.url'];
8
- var OTHER_EVENTS_MODIFIABLE_FIELD_PATHS = VIEW_EVENTS_MODIFIABLE_FIELD_PATHS.concat([// User-customizable field
9
- 'tags']);
6
+ var VIEW_MODIFIABLE_FIELD_PATHS = {
7
+ 'view.url': 'string',
8
+ 'view.referrer': 'string'
9
+ };
10
+ var USER_CUSTOMIZABLE_FIELD_PATHS = {
11
+ tags: 'object',
12
+ '_gc.trace_id': 'string',
13
+ '_gc.span_id': 'string'
14
+ };
15
+ var modifiableFieldPathsByEvent = {};
10
16
  export function startRumAssembly(configuration, lifeCycle, sessionManager, userSessionManager, viewContexts, urlContexts, actionContexts, buildCommonContext, reportError) {
17
+ modifiableFieldPathsByEvent[RumEventType.VIEW] = VIEW_MODIFIABLE_FIELD_PATHS;
18
+ modifiableFieldPathsByEvent[RumEventType.ERROR] = assign({
19
+ 'error.message': 'string',
20
+ 'error.stack': 'string',
21
+ 'error.resource.url': 'string'
22
+ }, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS);
23
+ modifiableFieldPathsByEvent[RumEventType.RESOURCE] = assign({
24
+ 'resource.url': 'string'
25
+ }, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS);
26
+ modifiableFieldPathsByEvent[RumEventType.ACTION] = assign({
27
+ 'action.target.name': 'string'
28
+ }, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS);
29
+ modifiableFieldPathsByEvent[RumEventType.LONG_TASK] = assign({}, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS);
11
30
  var eventRateLimiters = {};
12
31
  eventRateLimiters[RumEventType.ERROR] = createEventRateLimiter(RumEventType.ERROR, configuration.eventRateLimiterThreshold, reportError);
13
32
  eventRateLimiters[RumEventType.ACTION] = createEventRateLimiter(RumEventType.ACTION, configuration.eventRateLimiterThreshold, reportError);
@@ -25,7 +44,7 @@ export function startRumAssembly(configuration, lifeCycle, sessionManager, userS
25
44
  var actionId = actionContexts.findActionId(startTime);
26
45
  var commonContext = savedCommonContext || buildCommonContext();
27
46
  var rumContext = {
28
- _dd: {
47
+ _gc: {
29
48
  sdkName: configuration.sdkName,
30
49
  sdkVersion: configuration.sdkVersion,
31
50
  drift: currentDrift()
@@ -86,7 +105,7 @@ export function startRumAssembly(configuration, lifeCycle, sessionManager, userS
86
105
  }, commonContext.user);
87
106
  }
88
107
 
89
- if (shouldSend(serverRumEvent, configuration.beforeSend, eventRateLimiters, domainContext)) {
108
+ if (shouldSend(serverRumEvent, configuration.beforeSend, domainContext, eventRateLimiters)) {
90
109
  if (isEmptyObject(serverRumEvent.tags)) {
91
110
  delete serverRumEvent.tags;
92
111
  }
@@ -97,9 +116,9 @@ export function startRumAssembly(configuration, lifeCycle, sessionManager, userS
97
116
  });
98
117
  }
99
118
 
100
- function shouldSend(event, beforeSend, eventRateLimiters, domainContext) {
119
+ function shouldSend(event, beforeSend, domainContext, eventRateLimiters) {
101
120
  if (beforeSend) {
102
- var result = limitModification(event, event.type === RumEventType.VIEW ? VIEW_EVENTS_MODIFIABLE_FIELD_PATHS : OTHER_EVENTS_MODIFIABLE_FIELD_PATHS, function (event) {
121
+ var result = limitModification(event, modifiableFieldPathsByEvent[event.type], function (event) {
103
122
  return beforeSend(event, domainContext);
104
123
  });
105
124
 
@@ -1,5 +1,6 @@
1
- import { assign, display, isPercentage, validateAndBuildConfiguration, isArray, TraceType, isNullUndefinedDefaultValue, DefaultPrivacyLevel, objectHasValue } from '@cloudcare/browser-core';
1
+ import { assign, display, isPercentage, validateAndBuildConfiguration, isArray, TraceType, isNullUndefinedDefaultValue, DefaultPrivacyLevel, objectHasValue, isMatchOption, getOrigin, each } from '@cloudcare/browser-core';
2
2
  import { buildEnv } from '../boot/buildEnv';
3
+ import { isTracingOption } from '../domain/tracing/tracer';
3
4
  export function validateAndBuildRumConfiguration(initConfiguration) {
4
5
  if (!initConfiguration.applicationId) {
5
6
  display.error('Application ID is not configured, no RUM data will be collected.');
@@ -17,19 +18,23 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
17
18
  return;
18
19
  }
19
20
 
20
- if (initConfiguration.allowedTracingOrigins !== undefined) {
21
- if (!isArray(initConfiguration.allowedTracingOrigins)) {
22
- display.error('Allowed Tracing Origins should be an array');
23
- return;
24
- }
25
- }
21
+ var allowedTracingUrls = validateAndBuildTracingOptions(initConfiguration);
22
+
23
+ if (!allowedTracingUrls) {
24
+ return;
25
+ } // if (initConfiguration.allowedTracingOrigins !== undefined) {
26
+ // if (!isArray(initConfiguration.allowedTracingOrigins)) {
27
+ // display.error('Allowed Tracing Origins should be an array')
28
+ // return
29
+ // }
30
+ // }
31
+ // if (initConfiguration.allowedDDTracingOrigins !== undefined) {
32
+ // if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
33
+ // display.error('Allowed Tracing Origins should be an array')
34
+ // return
35
+ // }
36
+ // }
26
37
 
27
- if (initConfiguration.allowedDDTracingOrigins !== undefined) {
28
- if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
29
- display.error('Allowed Tracing Origins should be an array');
30
- return;
31
- }
32
- }
33
38
 
34
39
  if (initConfiguration.tracingSampleRate !== undefined && !isPercentage(initConfiguration.tracingSampleRate)) {
35
40
  display.error('Tracing Sample Rate should be a number between 0 and 100');
@@ -54,7 +59,7 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
54
59
  actionNameAttribute: initConfiguration.actionNameAttribute,
55
60
  sessionReplaySampleRate: isNullUndefinedDefaultValue(initConfiguration.sessionReplaySampleRate, 100),
56
61
  tracingSampleRate: initConfiguration.tracingSampleRate,
57
- allowedTracingOrigins: isNullUndefinedDefaultValue(initConfiguration.allowedTracingOrigins || initConfiguration.allowedDDTracingOrigins, []),
62
+ allowedTracingUrls: allowedTracingUrls,
58
63
  excludedActivityUrls: isNullUndefinedDefaultValue(initConfiguration.excludedActivityUrls, []),
59
64
  trackUserInteractions: trackUserInteractions || trackFrustrations,
60
65
  trackFrustrations: trackFrustrations,
@@ -65,4 +70,111 @@ export function validateAndBuildRumConfiguration(initConfiguration) {
65
70
  // 是否需要对webview 发送数据,需要装我们对应ios sdk
66
71
  defaultPrivacyLevel: objectHasValue(DefaultPrivacyLevel, initConfiguration.defaultPrivacyLevel) ? initConfiguration.defaultPrivacyLevel : DefaultPrivacyLevel.MASK_USER_INPUT
67
72
  }, baseConfiguration, buildEnv);
73
+ }
74
+ /**
75
+ * Handles allowedTracingUrls and processes legacy allowedTracingOrigins
76
+ */
77
+
78
+ function validateAndBuildTracingOptions(initConfiguration) {
79
+ // Advise about parameters precedence.
80
+ if (initConfiguration.allowedTracingUrls !== undefined && initConfiguration.allowedTracingOrigins !== undefined) {
81
+ display.warn('Both allowedTracingUrls and allowedTracingOrigins (deprecated) have been defined. The parameter allowedTracingUrls will override allowedTracingOrigins.');
82
+ } // Handle allowedTracingUrls first
83
+
84
+
85
+ if (initConfiguration.allowedTracingUrls !== undefined) {
86
+ if (!isArray(initConfiguration.allowedTracingUrls)) {
87
+ display.error('Allowed Tracing URLs should be an array');
88
+ return;
89
+ } // if (initConfiguration.allowedTracingUrls.length !== 0 && initConfiguration.service === undefined) {
90
+ // display.error('Service needs to be configured when tracing is enabled')
91
+ // return
92
+ // }
93
+ // Convert from (MatchOption | TracingOption) to TracingOption, remove unknown properties
94
+
95
+
96
+ var tracingOptions = [];
97
+ each(initConfiguration.allowedTracingUrls, function (option) {
98
+ if (isMatchOption(option)) {
99
+ tracingOptions.push({
100
+ match: option,
101
+ traceType: isNullUndefinedDefaultValue(initConfiguration.traceType, TraceType.DDTRACE)
102
+ });
103
+ } else if (isTracingOption(option)) {
104
+ tracingOptions.push(option);
105
+ } else {
106
+ display.warn('Allowed Tracing Urls parameters should be a string, RegExp, function, or an object. Ignoring parameter', option);
107
+ }
108
+ });
109
+ return tracingOptions;
110
+ } // Handle conversion of allowedTracingOrigins to allowedTracingUrls
111
+
112
+
113
+ if (initConfiguration.allowedTracingOrigins !== undefined) {
114
+ if (!isArray(initConfiguration.allowedTracingOrigins)) {
115
+ display.error('Allowed Tracing Origins should be an array');
116
+ return;
117
+ }
118
+
119
+ var tracingOptions = [];
120
+ each(initConfiguration.allowedTracingOrigins, function (legacyMatchOption) {
121
+ var tracingOption = convertLegacyMatchOptionToTracingOption(legacyMatchOption, isNullUndefinedDefaultValue(initConfiguration.traceType, TraceType.DDTRACE));
122
+
123
+ if (tracingOption) {
124
+ tracingOptions.push(tracingOption);
125
+ }
126
+ });
127
+ return tracingOptions;
128
+ } // Handle conversion of allowedDDTracingOrigins to allowedTracingUrls
129
+
130
+
131
+ if (initConfiguration.allowedDDTracingOrigins !== undefined) {
132
+ if (!isArray(initConfiguration.allowedDDTracingOrigins)) {
133
+ display.error('Allowed Tracing Origins should be an array');
134
+ return;
135
+ }
136
+
137
+ var tracingOptions = [];
138
+ each(initConfiguration.allowedDDTracingOrigins, function (legacyMatchOption) {
139
+ var tracingOption = convertLegacyMatchOptionToTracingOption(legacyMatchOption, isNullUndefinedDefaultValue(initConfiguration.traceType, TraceType.DDTRACE));
140
+
141
+ if (tracingOption) {
142
+ tracingOptions.push(tracingOption);
143
+ }
144
+ });
145
+ return tracingOptions;
146
+ }
147
+
148
+ return [];
149
+ }
150
+ /**
151
+ * Converts parameters from the deprecated allowedTracingOrigins
152
+ * to allowedTracingUrls. Handles the change from origin to full URLs.
153
+ */
154
+
155
+
156
+ function convertLegacyMatchOptionToTracingOption(item, traceType) {
157
+ var match;
158
+
159
+ if (typeof item === 'string') {
160
+ match = item;
161
+ } else if (item instanceof RegExp) {
162
+ match = function match(url) {
163
+ return item.test(getOrigin(url));
164
+ };
165
+ } else if (typeof item === 'function') {
166
+ match = function match(url) {
167
+ return item(getOrigin(url));
168
+ };
169
+ }
170
+
171
+ if (match === undefined) {
172
+ display.warn('Allowed Tracing Origins parameters should be a string, RegExp or function. Ignoring parameter', item);
173
+ return undefined;
174
+ }
175
+
176
+ return {
177
+ match: match,
178
+ traceType: traceType
179
+ };
68
180
  }
@@ -40,7 +40,7 @@ function processAction(action, foregroundContexts) {
40
40
  count: action.counts.resourceCount
41
41
  }
42
42
  },
43
- _dd: {
43
+ _gc: {
44
44
  action: {
45
45
  target: action.target,
46
46
  position: action.position
@@ -119,7 +119,7 @@ function computeRequestTracingInfo(request) {
119
119
  }
120
120
 
121
121
  return {
122
- _dd: {
122
+ _gc: {
123
123
  spanId: request.spanId,
124
124
  traceId: request.traceId
125
125
  },
@@ -131,7 +131,7 @@ function computeRequestTracingInfo(request) {
131
131
 
132
132
  function computeEntryTracingInfo(entry) {
133
133
  return entry.traceId ? {
134
- _dd: {
134
+ _gc: {
135
135
  traceId: entry.traceId
136
136
  }
137
137
  } : undefined;
@@ -59,7 +59,7 @@ function computePerformanceViewDetails(entry) {
59
59
  function processViewUpdate(view, foregroundContexts, recorderApi) {
60
60
  var replayStats = recorderApi.getReplayStats(view.id);
61
61
  var viewEvent = {
62
- _dd: {
62
+ _gc: {
63
63
  document_version: view.documentVersion,
64
64
  replay_stats: replayStats
65
65
  },
@@ -1,49 +1,12 @@
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 toString(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
- high = Math.floor(high / radix);
17
- low = Math.floor(mod / radix);
18
- str = (mod % radix).toString(radix) + str;
19
-
20
- if (!high && !low) {
21
- break;
22
- }
23
- }
24
-
25
- return str;
26
- },
27
- toDecimalString: function toDecimalString() {
28
- return this.toString(10);
29
- },
30
- readInt32: function readInt32(offset) {
31
- return this.buffer[offset] * 16777216 + (this.buffer[offset + 1] << 16) + (this.buffer[offset + 2] << 8) + this.buffer[offset + 3];
32
- }
33
- };
34
-
35
- function getCrypto() {
36
- return window.crypto || window.msCrypto;
37
- }
1
+ import { TraceIdentifier, getCrypto } from './traceIdentifier';
38
2
  /**
39
3
  *
40
4
  * @param {*} configuration 配置信息
41
5
  */
42
6
 
43
-
44
7
  export function DDtraceTracer(traceSampled) {
45
- this._spanId = new TraceIdentifier().toDecimalString();
46
- this._traceId = new TraceIdentifier().toDecimalString();
8
+ this._spanId = new TraceIdentifier();
9
+ this._traceId = new TraceIdentifier();
47
10
  this._traceSampled = traceSampled;
48
11
  }
49
12
  DDtraceTracer.prototype = {
@@ -51,10 +14,10 @@ DDtraceTracer.prototype = {
51
14
  return getCrypto() !== undefined;
52
15
  },
53
16
  getSpanId: function getSpanId() {
54
- return this._spanId;
17
+ return this._spanId.toDecimalString();
55
18
  },
56
19
  getTraceId: function getTraceId() {
57
- return this._traceId;
20
+ return this._traceId.toDecimalString();
58
21
  },
59
22
  makeTracingHeaders: function makeTracingHeaders() {
60
23
  return {
@@ -1,47 +1,28 @@
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 JaegerTracer(configuration, traceSampled) {
20
- var rootSpanId = randomTraceId(); // this._traceId = randomTraceId() + rootSpanId // 默认用128bit,兼容其他配置
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
  JaegerTracer.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
  getUberTraceId: function getUberTraceId() {
43
24
  //{trace-id}:{span-id}:{parent-span-id}:{flags}
44
- return this._traceId + ':' + this._spanId + ':' + '0' + ':' + (this._traceSampled ? '1' : '0');
25
+ return this.getTraceId() + ':' + this.getSpanId() + ':' + '0' + ':' + (this._traceSampled ? '1' : '0');
45
26
  },
46
27
  makeTracingHeaders: function makeTracingHeaders() {
47
28
  return {
@@ -0,0 +1,40 @@
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
+ TraceIdentifier.prototype = {
7
+ // buffer: new Uint8Array(8),
8
+ toString: function toString(radix) {
9
+ var high = this.readInt32(0);
10
+ var low = this.readInt32(4);
11
+ var str = '';
12
+
13
+ do {
14
+ var mod = high % radix * 4294967296 + low;
15
+ high = Math.floor(high / radix);
16
+ low = Math.floor(mod / radix);
17
+ str = (mod % radix).toString(radix) + str;
18
+ } while (high || low);
19
+
20
+ return str;
21
+ },
22
+ toDecimalString: function toDecimalString() {
23
+ return this.toString(10);
24
+ },
25
+
26
+ /**
27
+ * Format used by OTel headers
28
+ */
29
+ toPaddedHexadecimalString() {
30
+ var traceId = this.toString(16);
31
+ return Array(17 - traceId.length).join('0') + traceId;
32
+ },
33
+
34
+ readInt32: function readInt32(offset) {
35
+ return this.buffer[offset] * 16777216 + (this.buffer[offset + 1] << 16) + (this.buffer[offset + 2] << 8) + this.buffer[offset + 3];
36
+ }
37
+ };
38
+ export function getCrypto() {
39
+ return window.crypto || window.msCrypto;
40
+ }
@@ -1,15 +1,18 @@
1
- import { objectEntries, each, shallowClone, isArray, TraceType, getOrigin, performDraw, isNumber } from '@cloudcare/browser-core';
1
+ import { objectEntries, each, shallowClone, isArray, TraceType, performDraw, isNumber, getType, find, isMatchOption, matchList, isString } from '@cloudcare/browser-core';
2
2
  import { DDtraceTracer } from './ddtraceTracer';
3
3
  import { SkyWalkingTracer } from './skywalkingTracer';
4
4
  import { JaegerTracer } from './jaegerTracer';
5
5
  import { ZipkinSingleTracer } from './zipkinSingleTracer';
6
6
  import { ZipkinMultiTracer } from './zipkinMultiTracer';
7
7
  import { W3cTraceParentTracer } from './w3cTraceParentTracer';
8
+ export function isTracingOption(item) {
9
+ var expectedItem = item;
10
+ return getType(expectedItem) === 'object' && isMatchOption(expectedItem.match) && isString(expectedItem.traceType);
11
+ }
8
12
  export function clearTracingIfNeeded(context) {
9
- if (context.status === 0 && !context.isAborted) {
10
- context.traceId = undefined;
11
- context.spanId = undefined;
12
- context.traceSampled = undefined;
13
+ if (context.status === 0 && !context.isAborted) {// context.traceId = undefined
14
+ // context.spanId = undefined
15
+ // context.traceSampled = undefined
13
16
  }
14
17
  }
15
18
  export function startTracer(configuration, sessionManager) {
@@ -59,28 +62,24 @@ export function startTracer(configuration, sessionManager) {
59
62
  }
60
63
  };
61
64
  }
65
+ export function injectHeadersIfTracingAllowed(configuration, context, sessionManager, inject) {
66
+ if (!sessionManager.findTrackedSession()) {
67
+ return;
68
+ }
62
69
 
63
- function isAllowedUrl(configuration, requestUrl) {
64
- var requestOrigin = getOrigin(requestUrl);
65
- var flag = false;
66
- each(configuration.allowedTracingOrigins, function (allowedOrigin) {
67
- if (requestOrigin === allowedOrigin || allowedOrigin instanceof RegExp && allowedOrigin.test(requestOrigin)) {
68
- flag = true;
69
- return false;
70
- }
70
+ var tracingOption = find(configuration.allowedTracingUrls, function (tracingOption) {
71
+ return matchList([tracingOption.match], context.url, true);
71
72
  });
72
- return flag;
73
- }
74
73
 
75
- export function injectHeadersIfTracingAllowed(configuration, context, sessionManager, inject) {
76
- if (!isAllowedUrl(configuration, context.url) || !configuration.traceType || !sessionManager.findTrackedSession()) {
74
+ if (!tracingOption) {
77
75
  return;
78
76
  }
79
77
 
80
78
  var traceSampled = !isNumber(configuration.tracingSampleRate) || performDraw(configuration.tracingSampleRate);
81
- var tracer;
79
+ var tracer,
80
+ traceType = tracingOption.traceType;
82
81
 
83
- switch (configuration.traceType) {
82
+ switch (traceType) {
84
83
  case TraceType.DDTRACE:
85
84
  tracer = new DDtraceTracer(traceSampled);
86
85
  break;
@@ -98,7 +97,11 @@ export function injectHeadersIfTracingAllowed(configuration, context, sessionMan
98
97
  break;
99
98
 
100
99
  case TraceType.W3C_TRACEPARENT:
101
- tracer = new W3cTraceParentTracer(configuration, traceSampled);
100
+ tracer = new W3cTraceParentTracer(traceSampled);
101
+ break;
102
+
103
+ case TraceType.W3C_TRACEPARENT_64:
104
+ tracer = new W3cTraceParentTracer(traceSampled, true);
102
105
  break;
103
106
 
104
107
  case TraceType.ZIPKIN_SINGLE_HEADER:
@@ -1,40 +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
- * @param {*} configuration 配置信息
4
+ * @param {*} traceSampled
5
+ * @param {*} isHexTraceId 是否需要转换成10进制上报数据
16
6
  */
17
7
 
18
-
19
- export function W3cTraceParentTracer(configuration, traceSampled) {
20
- var rootSpanId = randomTraceId();
21
- this._traceId = randomTraceId() + rootSpanId;
22
- this._spanId = rootSpanId;
8
+ export function W3cTraceParentTracer(traceSampled, isHexTraceId) {
9
+ this._traceId = new TraceIdentifier();
10
+ this._spanId = new TraceIdentifier();
23
11
  this._traceSampled = traceSampled;
12
+ this.isHexTraceId = isHexTraceId;
24
13
  }
25
14
  W3cTraceParentTracer.prototype = {
26
15
  isTracingSupported: function isTracingSupported() {
27
- return true;
16
+ return getCrypto() !== undefined;
28
17
  },
29
18
  getSpanId: function getSpanId() {
30
- return this._spanId;
19
+ return this.isHexTraceId ? this._spanId.toDecimalString() : this._spanId.toPaddedHexadecimalString();
31
20
  },
32
21
  getTraceId: function getTraceId() {
33
- return this._traceId;
22
+ return this.isHexTraceId ? this._traceId.toDecimalString() : '0000000000000000' + this._traceId.toPaddedHexadecimalString();
34
23
  },
35
24
  getTraceParent: function getTraceParent() {
36
25
  // '{version}-{traceId}-{spanId}-{sampleDecision}'
37
- return '00-' + this._traceId + '-' + this._spanId + '-' + (this._traceSampled ? '01' : '00');
26
+ return '00-0000000000000000' + this._traceId.toPaddedHexadecimalString() + '-' + this._spanId.toPaddedHexadecimalString() + '-' + (this._traceSampled ? '01' : '00');
38
27
  },
39
28
  makeTracingHeaders: function makeTracingHeaders() {
40
29
  return {