@cloudcare/browser-rum 2.0.19 → 2.0.22

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 (49) hide show
  1. package/README.md +16 -11
  2. package/bundle/dataflux-rum.js +1 -1
  3. package/cjs/boot/buildEnv.js +1 -1
  4. package/cjs/boot/rumPublicApi.js +18 -31
  5. package/cjs/domain/assembly.js +14 -4
  6. package/cjs/domain/requestCollections.js +0 -1
  7. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +5 -1
  8. package/cjs/domain/rumEventsCollection/error/errorCollection.js +17 -2
  9. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +2 -2
  10. package/cjs/domain/rumSession.js +21 -6
  11. package/cjs/domain/tracing/ddtraceTracer.js +75 -0
  12. package/cjs/domain/tracing/jaegerTracer.js +58 -0
  13. package/cjs/domain/tracing/skywalkingTracer.js +93 -0
  14. package/cjs/domain/tracing/tracer.js +51 -60
  15. package/cjs/domain/tracing/w3cTraceParentTracer.js +51 -0
  16. package/cjs/domain/tracing/zipkinMultiTracer.js +58 -0
  17. package/cjs/domain/tracing/zipkinSingleTracer.js +51 -0
  18. package/esm/boot/buildEnv.js +1 -1
  19. package/esm/boot/rumPublicApi.js +19 -32
  20. package/esm/domain/assembly.js +14 -4
  21. package/esm/domain/requestCollections.js +0 -1
  22. package/esm/domain/rumEventsCollection/actions/actionCollection.js +5 -1
  23. package/esm/domain/rumEventsCollection/error/errorCollection.js +18 -3
  24. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +2 -2
  25. package/esm/domain/rumSession.js +21 -6
  26. package/esm/domain/tracing/ddtraceTracer.js +67 -0
  27. package/esm/domain/tracing/jaegerTracer.js +50 -0
  28. package/esm/domain/tracing/skywalkingTracer.js +84 -0
  29. package/esm/domain/tracing/tracer.js +44 -57
  30. package/esm/domain/tracing/w3cTraceParentTracer.js +43 -0
  31. package/esm/domain/tracing/zipkinMultiTracer.js +50 -0
  32. package/esm/domain/tracing/zipkinSingleTracer.js +43 -0
  33. package/package.json +3 -3
  34. package/src/boot/rumPublicApi.js +22 -34
  35. package/src/domain/.DS_Store +0 -0
  36. package/src/domain/assembly.js +14 -6
  37. package/src/domain/requestCollections.js +0 -1
  38. package/src/domain/rumEventsCollection/actions/actionCollection.js +5 -1
  39. package/src/domain/rumEventsCollection/error/errorCollection.js +23 -4
  40. package/src/domain/rumEventsCollection/resource/matchRequestTiming.js +1 -0
  41. package/src/domain/rumEventsCollection/resource/resourceCollection.js +3 -4
  42. package/src/domain/rumSession.js +24 -7
  43. package/src/domain/tracing/ddtraceTracer.js +70 -0
  44. package/src/domain/tracing/jaegerTracer.js +47 -0
  45. package/src/domain/tracing/skywalkingTracer.js +80 -0
  46. package/src/domain/tracing/tracer.js +40 -68
  47. package/src/domain/tracing/w3cTraceParentTracer.js +40 -0
  48. package/src/domain/tracing/zipkinMultiTracer.js +46 -0
  49. package/src/domain/tracing/zipkinSingleTracer.js +40 -0
@@ -1,4 +1,4 @@
1
- import { makePublicApi, checkCookiesAuthorized, checkIsNotLocalFile, BoundedBuffer, buildCookieOptions, createContextManager, isPercentage, clocksNow, extend2Lev, timeStampNow, ActionType, ErrorSource, isEmptyObject, formatDate } from '@cloudcare/browser-core';
1
+ import { makePublicApi, checkCookiesAuthorized, checkIsNotLocalFile, BoundedBuffer, buildCookieOptions, createContextManager, isPercentage, clocksNow, extend2Lev, timeStampNow, ActionType, ErrorSource, isEmptyObject, deepClone, formatDate, createHandlingStack } from '@cloudcare/browser-core';
2
2
  export function makeRumPublicApi(startRumImpl) {
3
3
  var isAlreadyInitialized = false;
4
4
  var globalContextManager = createContextManager();
@@ -39,7 +39,7 @@ export function makeRumPublicApi(startRumImpl) {
39
39
  };
40
40
 
41
41
  function clonedCommonContext() {
42
- return extend2Lev({}, {
42
+ return deepClone({
43
43
  context: globalContextManager.get(),
44
44
  user: user
45
45
  });
@@ -123,14 +123,14 @@ export function makeRumPublicApi(startRumImpl) {
123
123
  });
124
124
  }
125
125
  },
126
- // addAction: function (name, context) {
127
- // addActionStrategy({
128
- // name: name,
129
- // context: extend2Lev({}, context),
130
- // startClocks: clocksNow(),
131
- // type: ActionType.CUSTOM
132
- // })
133
- // },
126
+ addAction: function addAction(name, context) {
127
+ addActionStrategy({
128
+ name: name,
129
+ context: deepClone(context),
130
+ startClocks: clocksNow(),
131
+ type: ActionType.CUSTOM
132
+ });
133
+ },
134
134
 
135
135
  /**
136
136
  * @deprecated use addAction instead
@@ -138,28 +138,15 @@ export function makeRumPublicApi(startRumImpl) {
138
138
  // addUserAction: function (name, context) {
139
139
  // rumPublicApi.addAction(name, context)
140
140
  // },
141
- // addError: function (error, context, source) {
142
- // if (typeof source === 'undefined') {
143
- // source = ErrorSource.CUSTOM
144
- // }
145
- // var checkedSource
146
- // if (
147
- // source === ErrorSource.CUSTOM ||
148
- // source === ErrorSource.NETWORK ||
149
- // source === ErrorSource.SOURCE
150
- // ) {
151
- // checkedSource = source
152
- // } else {
153
- // console.error(`DD_RUM.addError: Invalid source '${source}'`)
154
- // checkedSource = ErrorSource.CUSTOM
155
- // }
156
- // addErrorStrategy({
157
- // error: error,
158
- // context: extend2Lev({}, context),
159
- // source: checkedSource,
160
- // startClocks: clocksNow()
161
- // })
162
- // },
141
+ addError: function addError(error, context) {
142
+ var handlingStack = createHandlingStack();
143
+ addErrorStrategy({
144
+ error: error,
145
+ handlingStack: handlingStack,
146
+ context: deepClone(context),
147
+ startClocks: clocksNow()
148
+ });
149
+ },
163
150
  // addTiming: function (name) {
164
151
  // addTimingStrategy(name)
165
152
  // },
@@ -13,7 +13,7 @@ export function startRumAssembly(applicationId, configuration, lifeCycle, sessio
13
13
  lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, function (data) {
14
14
  var startTime = data.startTime;
15
15
  var rawRumEvent = data.rawRumEvent;
16
- var savedCommonContext = data.savedGlobalContext;
16
+ var savedCommonContext = data.savedCommonContext;
17
17
  var customerContext = data.customerContext;
18
18
  var viewContext = parentContexts.findView(startTime);
19
19
  var deviceContext = {
@@ -48,9 +48,20 @@ export function startRumAssembly(applicationId, configuration, lifeCycle, sessio
48
48
  type: getSessionType()
49
49
  }
50
50
  };
51
- var rumEvent = needToAssembleWithAction(rawRumEvent) ? extend2Lev(rumContext, deviceContext, viewContext, actionContext, rawRumEvent) : extend2Lev(rumContext, deviceContext, viewContext, rawRumEvent);
51
+ var sessionAliasTag = {};
52
+
53
+ if (session.isTrackedWidthService()) {
54
+ // 如果是触发了采样的数据,则加上额外的tag
55
+ sessionAliasTag = {
56
+ session: {
57
+ is_sampling: '1'
58
+ }
59
+ };
60
+ }
61
+
62
+ var rumEvent = needToAssembleWithAction(rawRumEvent) ? extend2Lev(rumContext, deviceContext, sessionAliasTag, viewContext, actionContext, rawRumEvent) : extend2Lev(rumContext, deviceContext, sessionAliasTag, viewContext, rawRumEvent);
52
63
  var serverRumEvent = withSnakeCaseKeys(rumEvent);
53
- var context = extend2Lev(commonContext.context, customerContext);
64
+ var context = extend2Lev({}, commonContext.context, customerContext);
54
65
 
55
66
  if (!isEmptyObject(context)) {
56
67
  serverRumEvent.tags = context;
@@ -75,7 +86,6 @@ export function startRumAssembly(applicationId, configuration, lifeCycle, sessio
75
86
  // ) {
76
87
  // console.log(serverRumEvent, '======serverRumEvent-====')
77
88
  // }
78
- console.log(serverRumEvent, '======serverRumEvent-====');
79
89
  lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, serverRumEvent);
80
90
  }
81
91
  }
@@ -92,7 +92,6 @@ export function trackFetch(lifeCycle, configuration, tracer) {
92
92
  fetchProxy.onRequestComplete(function (context) {
93
93
  if (isAllowedRequestUrl(configuration, context.url)) {
94
94
  tracer.clearTracingIfCancelled(context);
95
- console.log(context.duration, 'duration=====');
96
95
  lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, {
97
96
  duration: context.duration,
98
97
  method: context.method,
@@ -33,7 +33,11 @@ function processAction(action) {
33
33
  count: action.counts.resourceCount
34
34
  }
35
35
  }
36
- } : undefined;
36
+ } : {
37
+ action: {
38
+ loadingTime: 0
39
+ }
40
+ };
37
41
  var customerContext = !isAutoAction(action) ? action.context : undefined;
38
42
  var actionEvent = extend2Lev({
39
43
  action: {
@@ -1,4 +1,4 @@
1
- import { startAutomaticErrorCollection, getTimestamp, getStatusGroup, urlParse, replaceNumberCharByPath, RumEventType, LifeCycleEventType } from '@cloudcare/browser-core';
1
+ import { startAutomaticErrorCollection, getTimestamp, getStatusGroup, urlParse, replaceNumberCharByPath, RumEventType, LifeCycleEventType, computeStackTrace, formatUnknownError, ErrorHandling, extend, ErrorSource } from '@cloudcare/browser-core';
2
2
  export function startErrorCollection(lifeCycle, configuration) {
3
3
  startAutomaticErrorCollection(configuration).subscribe(function (error) {
4
4
  lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, {
@@ -9,11 +9,14 @@ export function startErrorCollection(lifeCycle, configuration) {
9
9
  }
10
10
  export function doStartErrorCollection(lifeCycle) {
11
11
  lifeCycle.subscribe(LifeCycleEventType.RAW_ERROR_COLLECTED, function (error) {
12
- lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processError(error.error));
12
+ lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, extend({
13
+ customerContext: error.customerContext,
14
+ savedCommonContext: error.savedCommonContext
15
+ }, processError(error.error)));
13
16
  });
14
17
  return {
15
18
  addError: function addError(customError, savedCommonContext) {
16
- var rawError = computeRawError(customError.error, customError.startClocks, customError.source);
19
+ var rawError = computeRawError(customError.error, customError.handlingStack, customError.startClocks);
17
20
  lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, {
18
21
  customerContext: customError.context,
19
22
  savedCommonContext: savedCommonContext,
@@ -23,6 +26,16 @@ export function doStartErrorCollection(lifeCycle) {
23
26
  };
24
27
  }
25
28
 
29
+ function computeRawError(error, handlingStack, startClocks) {
30
+ var stackTrace = error instanceof Error ? computeStackTrace(error) : undefined;
31
+ return extend({
32
+ startClocks: startClocks,
33
+ source: ErrorSource.CUSTOM,
34
+ originalError: error,
35
+ handling: ErrorHandling.HANDLED
36
+ }, formatUnknownError(stackTrace, error, 'Provided', handlingStack));
37
+ }
38
+
26
39
  function processError(error) {
27
40
  var resource = error.resource;
28
41
 
@@ -47,6 +60,8 @@ function processError(error) {
47
60
  source: error.source,
48
61
  stack: error.stack,
49
62
  type: error.type,
63
+ handling_stack: error.handlingStack,
64
+ handling: error.handling,
50
65
  starttime: getTimestamp(error.startClocks.relative)
51
66
  },
52
67
  type: RumEventType.ERROR
@@ -96,8 +96,8 @@ function computeRequestTracingInfo(request) {
96
96
 
97
97
  return {
98
98
  _dd: {
99
- spanId: request.spanId.toDecimalString(),
100
- traceId: request.traceId.toDecimalString()
99
+ spanId: request.spanId,
100
+ traceId: request.traceId
101
101
  },
102
102
  resource: {
103
103
  id: UUID()
@@ -3,7 +3,9 @@ export var RUM_SESSION_KEY = 'rum';
3
3
  export var RumTrackingType = {
4
4
  NOT_TRACKED: '0',
5
5
  TRACKED_WITH_RESOURCES: '1',
6
- TRACKED_WITHOUT_RESOURCES: '2'
6
+ TRACKED_WITHOUT_RESOURCES: '2',
7
+ TRACKED_WITH_SERVICE: '3' // 采样数据添加额外的tag,不舍弃,直接上报到服务端
8
+
7
9
  };
8
10
  export function startRumSession(configuration, lifeCycle) {
9
11
  var session = startSessionManagement(configuration.cookieOptions, RUM_SESSION_KEY, function (rawTrackingType) {
@@ -21,8 +23,12 @@ export function startRumSession(configuration, lifeCycle) {
21
23
  isTracked: function isTracked() {
22
24
  return session.getId() !== undefined && _isTracked(session.getTrackingType());
23
25
  },
26
+ isTrackedWidthService: function isTrackedWidthService() {
27
+ return session.getId() !== undefined && session.getTrackingType() === RumTrackingType.TRACKED_WITH_SERVICE;
28
+ },
24
29
  isTrackedWithResource: function isTrackedWithResource() {
25
- return session.getId() !== undefined && session.getTrackingType() === RumTrackingType.TRACKED_WITH_RESOURCES;
30
+ var trackingType = session.getTrackingType();
31
+ return session.getId() !== undefined && (trackingType === RumTrackingType.TRACKED_WITH_RESOURCES || trackingType === RumTrackingType.TRACKED_WITH_SERVICE);
26
32
  }
27
33
  };
28
34
  }
@@ -33,9 +39,18 @@ function computeSessionState(configuration, rawTrackingType) {
33
39
  if (hasValidRumSession(rawTrackingType)) {
34
40
  trackingType = rawTrackingType;
35
41
  } else if (!performDraw(configuration.sampleRate)) {
36
- trackingType = RumTrackingType.NOT_TRACKED;
42
+ if (configuration.isServiceSampling) {
43
+ trackingType = RumTrackingType.TRACKED_WITH_SERVICE;
44
+ } else {
45
+ trackingType = RumTrackingType.NOT_TRACKED;
46
+ }
37
47
  } else if (!performDraw(configuration.resourceSampleRate)) {
38
- trackingType = RumTrackingType.TRACKED_WITHOUT_RESOURCES;
48
+ // trackingType = RumTrackingType.TRACKED_WITHOUT_RESOURCES
49
+ if (configuration.isServiceSampling) {
50
+ trackingType = RumTrackingType.TRACKED_WITH_SERVICE;
51
+ } else {
52
+ trackingType = RumTrackingType.TRACKED_WITHOUT_RESOURCES;
53
+ }
39
54
  } else {
40
55
  trackingType = RumTrackingType.TRACKED_WITH_RESOURCES;
41
56
  }
@@ -47,9 +62,9 @@ function computeSessionState(configuration, rawTrackingType) {
47
62
  }
48
63
 
49
64
  function hasValidRumSession(trackingType) {
50
- return trackingType === RumTrackingType.NOT_TRACKED || trackingType === RumTrackingType.TRACKED_WITH_RESOURCES || trackingType === RumTrackingType.TRACKED_WITHOUT_RESOURCES;
65
+ return trackingType === RumTrackingType.NOT_TRACKED || trackingType === RumTrackingType.TRACKED_WITH_RESOURCES || trackingType === RumTrackingType.TRACKED_WITHOUT_RESOURCES || trackingType === RumTrackingType.TRACKED_WITH_SERVICE;
51
66
  }
52
67
 
53
68
  function _isTracked(rumSessionType) {
54
- return rumSessionType === RumTrackingType.TRACKED_WITH_RESOURCES || rumSessionType === RumTrackingType.TRACKED_WITHOUT_RESOURCES;
69
+ return rumSessionType === RumTrackingType.TRACKED_WITH_RESOURCES || rumSessionType === RumTrackingType.TRACKED_WITHOUT_RESOURCES || rumSessionType === RumTrackingType.TRACKED_WITH_SERVICE;
55
70
  }
@@ -0,0 +1,67 @@
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
+ }
38
+ /**
39
+ *
40
+ * @param {*} configuration 配置信息
41
+ */
42
+
43
+
44
+ export function DDtraceTracer(configuration) {
45
+ this._spanId = new TraceIdentifier().toDecimalString();
46
+ this._traceId = new TraceIdentifier().toDecimalString();
47
+ }
48
+ DDtraceTracer.prototype = {
49
+ isTracingSupported: function isTracingSupported() {
50
+ return getCrypto() !== undefined;
51
+ },
52
+ getSpanId: function getSpanId() {
53
+ return this._spanId;
54
+ },
55
+ getTraceId: function getTraceId() {
56
+ return this._traceId;
57
+ },
58
+ makeTracingHeaders: function makeTracingHeaders() {
59
+ return {
60
+ 'x-datadog-origin': 'rum',
61
+ // 'x-datadog-parent-id': spanId.toDecimalString(),
62
+ 'x-datadog-sampled': '1',
63
+ 'x-datadog-sampling-priority': '1',
64
+ 'x-datadog-trace-id': this.getTraceId()
65
+ };
66
+ }
67
+ };
@@ -0,0 +1,50 @@
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
+ }
13
+ /**
14
+ *
15
+ * @param {*} configuration 配置信息
16
+ */
17
+
18
+
19
+ export function JaegerTracer(configuration) {
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;
30
+ }
31
+ JaegerTracer.prototype = {
32
+ isTracingSupported: function isTracingSupported() {
33
+ return true;
34
+ },
35
+ getSpanId: function getSpanId() {
36
+ return this._spanId;
37
+ },
38
+ getTraceId: function getTraceId() {
39
+ return this._traceId;
40
+ },
41
+ getUberTraceId: function getUberTraceId() {
42
+ //{trace-id}:{span-id}:{parent-span-id}:{flags}
43
+ return this._traceId + ':' + this._spanId + ':' + '0' + ':' + '1';
44
+ },
45
+ makeTracingHeaders: function makeTracingHeaders() {
46
+ return {
47
+ 'uber-trace-id': this.getUberTraceId()
48
+ };
49
+ }
50
+ };
@@ -0,0 +1,84 @@
1
+ import { base64Encode, urlParse } from '@cloudcare/browser-core'; // start SkyWalking
2
+
3
+ function uuid() {
4
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
5
+ /* tslint:disable */
6
+ var r = Math.random() * 16 | 0;
7
+ /* tslint:disable */
8
+
9
+ var v = c === 'x' ? r : r & 0x3 | 0x8;
10
+ return v.toString(16);
11
+ });
12
+ }
13
+ /**
14
+ *
15
+ * @param {*} traceId
16
+ * @param {*} traceSegmentId
17
+ * @param {*} applicationId
18
+ * @param {*} version
19
+ * @param {*} requestUrlHost
20
+ */
21
+
22
+
23
+ function getSkyWalkingSw8(traceId, traceSegmentId, applicationId, env, version, requestUrlHost) {
24
+ try {
25
+ var traceIdStr = String(base64Encode(traceId));
26
+ var segmentId = String(base64Encode(traceSegmentId));
27
+ var service = String(base64Encode(applicationId + '_rum_' + env));
28
+ var instance = String(base64Encode(version));
29
+ var endpoint = String(base64Encode(window.location.href));
30
+ var peer = String(base64Encode(requestUrlHost));
31
+ var index = '0'; // var values = `${1}-${traceIdStr}-${segmentId}-${index}-${service}-${instance}-${endpoint}-${peer}`;
32
+
33
+ return '1-' + traceIdStr + '-' + segmentId + '-' + index + '-' + service + '-' + instance + '-' + endpoint + '-' + peer;
34
+ } catch (err) {
35
+ return '';
36
+ }
37
+ }
38
+ /**
39
+ *
40
+ * @param {*} configuration 配置信息
41
+ * @param {*} requestUrl 请求的url
42
+ */
43
+
44
+
45
+ export function SkyWalkingTracer(configuration, requestUrl) {
46
+ this._spanId = uuid();
47
+ this._traceId = uuid();
48
+ this._applicationId = configuration.applicationId;
49
+ this._env = configuration.env;
50
+ this._version = configuration.version;
51
+ this._urlParse = urlParse(requestUrl).getParse();
52
+ }
53
+ SkyWalkingTracer.prototype = {
54
+ isTracingSupported: function isTracingSupported() {
55
+ if (this._env && this._version && this._urlParse) return true;
56
+ return false;
57
+ },
58
+ getSpanId: function getSpanId() {
59
+ return this._spanId;
60
+ },
61
+ getTraceId: function getTraceId() {
62
+ return this._traceId;
63
+ },
64
+ getSkyWalkingSw8: function getSkyWalkingSw8() {
65
+ try {
66
+ var traceIdStr = String(base64Encode(this._traceId));
67
+ var segmentId = String(base64Encode(this._spanId));
68
+ var service = String(base64Encode(this._applicationId + '_rum_' + this.env));
69
+ var instance = String(base64Encode(this._version));
70
+ var endpoint = String(base64Encode(window.location.href));
71
+ var peer = String(base64Encode(this._urlParse.Host));
72
+ var index = '0'; // var values = `${1}-${traceIdStr}-${segmentId}-${index}-${service}-${instance}-${endpoint}-${peer}`;
73
+
74
+ return '1-' + traceIdStr + '-' + segmentId + '-' + index + '-' + service + '-' + instance + '-' + endpoint + '-' + peer;
75
+ } catch (err) {
76
+ return '';
77
+ }
78
+ },
79
+ makeTracingHeaders: function makeTracingHeaders() {
80
+ return {
81
+ 'sw8': this.getSkyWalkingSw8()
82
+ };
83
+ }
84
+ };
@@ -1,4 +1,10 @@
1
- import { objectEntries, each, extend, isArray, getOrigin } from '@cloudcare/browser-core';
1
+ import { objectEntries, each, extend, isArray, TraceType, getOrigin } from '@cloudcare/browser-core';
2
+ import { DDtraceTracer } from './ddtraceTracer';
3
+ import { SkyWalkingTracer } from './skywalkingTracer';
4
+ import { JaegerTracer } from './jaegerTracer';
5
+ import { ZipkinSingleTracer } from './zipkinSingleTracer';
6
+ import { ZipkinMultiTracer } from './zipkinMultiTracer';
7
+ import { W3cTraceParentTracer } from './w3cTraceParentTracer';
2
8
  export function clearTracingIfCancelled(context) {
3
9
  if (context.status === 0) {
4
10
  context.traceId = undefined;
@@ -40,20 +46,10 @@ export function startTracer(configuration) {
40
46
  };
41
47
  }
42
48
 
43
- function injectHeadersIfTracingAllowed(configuration, context, inject) {
44
- if (!isTracingSupported() || !isAllowedUrl(configuration, context.url)) {
45
- return;
46
- }
47
-
48
- context.traceId = new TraceIdentifier();
49
- context.spanId = new TraceIdentifier();
50
- inject(makeTracingHeaders(context.traceId, context.spanId));
51
- }
52
-
53
49
  function isAllowedUrl(configuration, requestUrl) {
54
50
  var requestOrigin = getOrigin(requestUrl);
55
51
  var flag = false;
56
- each(configuration.allowedDDTracingOrigins, function (allowedOrigin) {
52
+ each(configuration.allowedTracingOrigins, function (allowedOrigin) {
57
53
  if (requestOrigin === allowedOrigin || allowedOrigin instanceof RegExp && allowedOrigin.test(requestOrigin)) {
58
54
  flag = true;
59
55
  return false;
@@ -62,56 +58,47 @@ function isAllowedUrl(configuration, requestUrl) {
62
58
  return flag;
63
59
  }
64
60
 
65
- export function isTracingSupported() {
66
- return getCrypto() !== undefined;
67
- }
61
+ export function injectHeadersIfTracingAllowed(configuration, context, inject) {
62
+ if (!isAllowedUrl(configuration, context.url) || !configuration.traceType) {
63
+ return;
64
+ }
68
65
 
69
- function getCrypto() {
70
- return window.crypto || window.msCrypto;
71
- }
66
+ var tracer;
72
67
 
73
- function makeTracingHeaders(traceId, spanId) {
74
- return {
75
- 'x-datadog-origin': 'rum',
76
- // 'x-datadog-parent-id': spanId.toDecimalString(),
77
- 'x-datadog-sampled': '1',
78
- 'x-datadog-sampling-priority': '1',
79
- 'x-datadog-trace-id': traceId.toDecimalString()
80
- };
81
- }
82
- /* tslint:disable:no-bitwise */
68
+ switch (configuration.traceType) {
69
+ case TraceType.DDTRACE:
70
+ tracer = new DDtraceTracer();
71
+ break;
83
72
 
73
+ case TraceType.SKYWALKING_V3:
74
+ tracer = new SkyWalkingTracer(configuration, context.url);
75
+ break;
84
76
 
85
- export function TraceIdentifier() {
86
- this.buffer = new Uint8Array(8);
87
- getCrypto().getRandomValues(this.buffer);
88
- this.buffer[0] = this.buffer[0] & 0x7f;
89
- }
90
- TraceIdentifier.prototype = {
91
- // buffer: new Uint8Array(8),
92
- toString: function toString(radix) {
93
- var high = this.readInt32(0);
94
- var low = this.readInt32(4);
95
- var str = '';
77
+ case TraceType.ZIPKIN_MULTI_HEADER:
78
+ tracer = new ZipkinMultiTracer(configuration);
79
+ break;
96
80
 
97
- while (1) {
98
- var mod = high % radix * 4294967296 + low;
99
- high = Math.floor(high / radix);
100
- low = Math.floor(mod / radix);
101
- str = (mod % radix).toString(radix) + str;
81
+ case TraceType.JAEGER:
82
+ tracer = new JaegerTracer(configuration);
83
+ break;
102
84
 
103
- if (!high && !low) {
104
- break;
105
- }
106
- }
85
+ case TraceType.W3C_TRACEPARENT:
86
+ tracer = new W3cTraceParentTracer(configuration);
87
+ break;
88
+
89
+ case TraceType.ZIPKIN_SINGLE_HEADER:
90
+ tracer = new ZipkinSingleTracer(configuration);
91
+ break;
107
92
 
108
- return str;
109
- },
110
- toDecimalString: function toDecimalString() {
111
- return this.toString(10);
112
- },
113
- readInt32: function readInt32(offset) {
114
- return this.buffer[offset] * 16777216 + (this.buffer[offset + 1] << 16) + (this.buffer[offset + 2] << 8) + this.buffer[offset + 3];
93
+ default:
94
+ break;
115
95
  }
116
- };
117
- /* tslint:enable:no-bitwise */
96
+
97
+ if (!tracer || !tracer.isTracingSupported()) {
98
+ return;
99
+ }
100
+
101
+ context.traceId = tracer.getTraceId();
102
+ context.spanId = tracer.getSpanId();
103
+ inject(tracer.makeTracingHeaders());
104
+ }
@@ -0,0 +1,43 @@
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
+ }
13
+ /**
14
+ *
15
+ * @param {*} configuration 配置信息
16
+ */
17
+
18
+
19
+ export function W3cTraceParentTracer(configuration) {
20
+ var rootSpanId = randomTraceId();
21
+ this._traceId = randomTraceId() + rootSpanId;
22
+ this._spanId = rootSpanId;
23
+ }
24
+ W3cTraceParentTracer.prototype = {
25
+ isTracingSupported: function isTracingSupported() {
26
+ return true;
27
+ },
28
+ getSpanId: function getSpanId() {
29
+ return this._spanId;
30
+ },
31
+ getTraceId: function getTraceId() {
32
+ return this._traceId;
33
+ },
34
+ getTraceParent: function getTraceParent() {
35
+ // '{version}-{traceId}-{spanId}-{sampleDecision}'
36
+ return '00-' + this._traceId + '-' + this._spanId + '-01';
37
+ },
38
+ makeTracingHeaders: function makeTracingHeaders() {
39
+ return {
40
+ 'traceparent': this.getTraceParent()
41
+ };
42
+ }
43
+ };
@@ -0,0 +1,50 @@
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
+ }
13
+ /**
14
+ *
15
+ * @param {*} configuration 配置信息
16
+ */
17
+
18
+
19
+ export function ZipkinMultiTracer(configuration) {
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;
30
+ }
31
+ ZipkinMultiTracer.prototype = {
32
+ isTracingSupported: function isTracingSupported() {
33
+ return true;
34
+ },
35
+ getSpanId: function getSpanId() {
36
+ return this._spanId;
37
+ },
38
+ getTraceId: function getTraceId() {
39
+ return this._traceId;
40
+ },
41
+ makeTracingHeaders: function makeTracingHeaders() {
42
+ return {
43
+ 'X-B3-TraceId': this.getTraceId(),
44
+ 'X-B3-SpanId': this.getSpanId(),
45
+ // 'X-B3-ParentSpanId': '',
46
+ 'X-B3-Sampled': '1' // 'X-B3-Flags': '0'
47
+
48
+ };
49
+ }
50
+ };