@weave-js/core 0.15.1 → 0.15.2
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.
- package/lib/broker/context.js +0 -10
- package/lib/broker/defaultOptions.js +1 -1
- package/lib/broker/public.d.ts +120 -0
- package/lib/middlewares/tracing/index.js +33 -48
- package/lib/middlewares/tracing/tags.js +113 -6
- package/lib/runtime/initContextFactory.js +7 -3
- package/lib/runtime/initTracing.js +23 -9
- package/lib/tracing/collectors/event.js +3 -1
- package/lib/tracing/span.js +136 -49
- package/lib/transport/adapters/adapters.js +1 -1
- package/lib/transport/adapters/dummy/index.js +1 -1
- package/lib/transport/adapters/tcp/index.js +1 -1
- package/lib/types.js +1 -1
- package/package.json +2 -3
- /package/lib/transport/adapters/{adapteBase.js → adapterBase.js} +0 -0
package/lib/broker/context.js
CHANGED
|
@@ -89,17 +89,7 @@ exports.createContext = (runtime) => {
|
|
|
89
89
|
});
|
|
90
90
|
},
|
|
91
91
|
startSpan (name, options) {
|
|
92
|
-
options = Object.assign({
|
|
93
|
-
id: this.id,
|
|
94
|
-
traceId: this.requestId,
|
|
95
|
-
parentId: this.parentId,
|
|
96
|
-
type: 'action',
|
|
97
|
-
service: this.service,
|
|
98
|
-
sampled: this.tracing
|
|
99
|
-
}, options);
|
|
100
|
-
|
|
101
92
|
let span;
|
|
102
|
-
|
|
103
93
|
if (this.span) {
|
|
104
94
|
span = this.span.startChildSpan(name, options);
|
|
105
95
|
} else {
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export type BulkheadOptions = {
|
|
2
|
+
enabled?: boolean;
|
|
3
|
+
concurrentCalls: number;
|
|
4
|
+
maxQueueSize: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type CacheLockOptions = {
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type CacheOptions = {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
adapter: string | object;
|
|
14
|
+
ttl: number;
|
|
15
|
+
lock?: CacheLockOptions;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type ContextTracking = {
|
|
19
|
+
enabled: boolean;
|
|
20
|
+
shutdownTimeout: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type StrictModeOptions = 'remove' | 'error'
|
|
24
|
+
|
|
25
|
+
export type ValidatorOptions = {
|
|
26
|
+
strict: boolean;
|
|
27
|
+
strictMode: StrictModeOptions;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type BrokerOptions = {
|
|
31
|
+
nodeId?: string;
|
|
32
|
+
namespace?: string;
|
|
33
|
+
bulkhead?: BulkheadOptions;
|
|
34
|
+
cache?: CacheOptions;
|
|
35
|
+
contextTracking?: ContextTracking;
|
|
36
|
+
circuitBreaker?: CircuitBreakerOptions;
|
|
37
|
+
transport?: TransportOptions;
|
|
38
|
+
errorHandler?: Function;
|
|
39
|
+
loadInternalMiddlewares?: boolean;
|
|
40
|
+
metrics?: MetricsOptions;
|
|
41
|
+
middlewares?: Array<Middleware>;
|
|
42
|
+
logger?: LoggerOptions;
|
|
43
|
+
tracing?: TracingOptions;
|
|
44
|
+
registry?: RegistryOptions;
|
|
45
|
+
retryPolicy?: RetryPolicyOptions;
|
|
46
|
+
validatorOptions?: ValidatorOptions;
|
|
47
|
+
validateActionParams?: boolean;
|
|
48
|
+
waitForServiceInterval?: number;
|
|
49
|
+
beforeRegisterMiddlewares?: Function;
|
|
50
|
+
uuidFactory?: Function;
|
|
51
|
+
started?: Function;
|
|
52
|
+
stopped?: Function;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type CircuitBreakerOptions = {
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
halfOpenTimeout: number;
|
|
58
|
+
maxFailures: number;
|
|
59
|
+
windowTime: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type RetryPolicyOptions = {
|
|
63
|
+
enabled: boolean;
|
|
64
|
+
delay: number;
|
|
65
|
+
retries: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type TransportOptions = {
|
|
69
|
+
adapter?: string | object;
|
|
70
|
+
maxQueueSize: number;
|
|
71
|
+
heartbeatInterval: number;
|
|
72
|
+
heartbeatTimeout: number;
|
|
73
|
+
localNodeUpdateInterval: number;
|
|
74
|
+
offlineNodeCheckInterval: number;
|
|
75
|
+
maxOfflineTime: number;
|
|
76
|
+
maxChunkSize: number;
|
|
77
|
+
streams: TransportStreamOptions;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type TransportStreamOptions = {
|
|
81
|
+
handleBackpressure: boolean;
|
|
82
|
+
}
|
|
83
|
+
export type Context = {
|
|
84
|
+
id?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type Broker = {
|
|
88
|
+
nodeId: string;
|
|
89
|
+
namespace?: string;
|
|
90
|
+
runtime: Runtime;
|
|
91
|
+
bus: EventEmitter;
|
|
92
|
+
version: string;
|
|
93
|
+
options: BrokerOptions;
|
|
94
|
+
metrics?: MetricRegistry;
|
|
95
|
+
validator: Validator;
|
|
96
|
+
start(): Promise<any>;
|
|
97
|
+
stop(): Promise<any>;
|
|
98
|
+
createService(schema: ServiceSchema): Service;
|
|
99
|
+
loadService(path: string): void;
|
|
100
|
+
loadServices(folder: string, fileMask?: string): void;
|
|
101
|
+
contextFactory: ContextFactory;
|
|
102
|
+
log: Logger;
|
|
103
|
+
createLogger(name: string, params?: any): Logger;
|
|
104
|
+
cache?: Cache;
|
|
105
|
+
getUUID(): string;
|
|
106
|
+
registry: Registry;
|
|
107
|
+
tracer?: Tracer;
|
|
108
|
+
transport?: Transport;
|
|
109
|
+
getNextActionEndpoint(actionName: string): Endpoint | Error;
|
|
110
|
+
call(actionName: string, params?: any, opts?: CallOptions): Promise<any>;
|
|
111
|
+
multiCall(calls: Array<CallAction>): Promise<Array<any>>;
|
|
112
|
+
emit(eventName: string, payload?: any, opts?: EmitOptions): Promise<any>;
|
|
113
|
+
broadcast(eventName: string, payload?: any, opts?: BroadcastOptions): Promise<any>;
|
|
114
|
+
broadcastLocal(eventName: string, payload?: any, opts?: BroadcastOptions): Promise<any>;
|
|
115
|
+
waitForServices(serviceNames: Array<string> | string): Promise<any>;
|
|
116
|
+
ping(nodeID: string, timeout?: number): Promise<PingResult>;
|
|
117
|
+
handleError(err: Error): void;
|
|
118
|
+
fatalError(err: Error): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
@@ -4,56 +4,42 @@
|
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const {
|
|
8
|
-
|
|
7
|
+
const { buildActionTags, buildEventTags, addResponseTags } = require('./tags');
|
|
8
|
+
|
|
9
|
+
function getSpanName (context, actionTracingOptions) {
|
|
10
|
+
let spanName = `action "${context.action.name}"`;
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
if (actionTracingOptions.spanName) {
|
|
14
|
+
switch (typeof actionTracingOptions.spanName) {
|
|
15
|
+
case 'string':
|
|
16
|
+
spanName = actionTracingOptions.spanName;
|
|
17
|
+
break;
|
|
18
|
+
case 'function':
|
|
19
|
+
spanName = actionTracingOptions.spanName.call(context.service, context);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
context.service.log.warn({
|
|
25
|
+
requestId: context.requestId,
|
|
26
|
+
spanId: context.span.id
|
|
27
|
+
}, `Error while getting span name: ${error.message}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return spanName;
|
|
31
|
+
}
|
|
9
32
|
|
|
10
33
|
const wrapTracingLocalActionMiddleware = function (handler, action) {
|
|
11
34
|
const broker = this;
|
|
12
|
-
const
|
|
35
|
+
const globalTracingOptions = broker.options.tracing || {};
|
|
13
36
|
const actionTracingOptions = action.tracing || {};
|
|
14
37
|
|
|
15
|
-
if (
|
|
16
|
-
return function
|
|
17
|
-
const tags = buildActionTags(context,
|
|
18
|
-
|
|
19
|
-
if (tracingOptions.actions.data) {
|
|
20
|
-
tags.data = context.data !== null && isPlainObject(context.data) ? Object.assign({}, context.data) : context.data;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const globalActionTags = tracingOptions.actions.tags;
|
|
24
|
-
let actionTags;
|
|
25
|
-
// local action tags take precedence
|
|
26
|
-
if (isFunction(actionTracingOptions.tags)) {
|
|
27
|
-
actionTags = actionTracingOptions.tags;
|
|
28
|
-
} else if (!actionTracingOptions.tags && isFunction(globalActionTags)) {
|
|
29
|
-
actionTags = globalActionTags;
|
|
30
|
-
} else {
|
|
31
|
-
// By default all params are captured. This can be overridden globally and locally
|
|
32
|
-
actionTags = { ...{ data: true }, ...globalActionTags, ...actionTracingOptions.tags };
|
|
33
|
-
}
|
|
38
|
+
if (globalTracingOptions.enabled) {
|
|
39
|
+
return function tracingLocalMiddleware (context, serviceInjections) {
|
|
40
|
+
const tags = buildActionTags(context, globalTracingOptions, actionTracingOptions);
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
if (Array.isArray(actionTags.data)) {
|
|
37
|
-
tags.data = actionTags.data.reduce((acc, current) => {
|
|
38
|
-
acc[current] = dotGet(context.data, current);
|
|
39
|
-
return acc;
|
|
40
|
-
}, {});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Span name
|
|
45
|
-
let spanName = `action "${context.action.name}"`;
|
|
46
|
-
|
|
47
|
-
if (actionTracingOptions.spanName) {
|
|
48
|
-
switch (typeof actionTracingOptions.spanName) {
|
|
49
|
-
case 'string':
|
|
50
|
-
spanName = actionTracingOptions.spanName;
|
|
51
|
-
break;
|
|
52
|
-
case 'function':
|
|
53
|
-
spanName = actionTracingOptions.spanName.call(context.service, context);
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
42
|
+
const spanName = getSpanName(context, actionTracingOptions);
|
|
57
43
|
|
|
58
44
|
const span = context.startSpan(spanName, {
|
|
59
45
|
id: context.id,
|
|
@@ -73,9 +59,7 @@ const wrapTracingLocalActionMiddleware = function (handler, action) {
|
|
|
73
59
|
isCachedResult: context.isCachedResult
|
|
74
60
|
};
|
|
75
61
|
|
|
76
|
-
|
|
77
|
-
tags.response = result !== null && isPlainObject(result) ? Object.assign({}, result) : result;
|
|
78
|
-
}
|
|
62
|
+
addResponseTags(context, tags, result, globalTracingOptions.actions, actionTracingOptions);
|
|
79
63
|
|
|
80
64
|
span.addTags(tags);
|
|
81
65
|
context.finishSpan(span);
|
|
@@ -95,10 +79,11 @@ const wrapTracingLocalEventMiddleware = function (handler, event) {
|
|
|
95
79
|
const broker = this;
|
|
96
80
|
const service = event.service;
|
|
97
81
|
const tracingOptions = broker.options.tracing || {};
|
|
82
|
+
const eventTracingOptions = event.tracing || {};
|
|
98
83
|
|
|
99
84
|
if (tracingOptions.enabled) {
|
|
100
85
|
return function metricsLocalMiddleware (context) {
|
|
101
|
-
const tags = buildEventTags(context);
|
|
86
|
+
const tags = buildEventTags(context, tracingOptions, eventTracingOptions);
|
|
102
87
|
|
|
103
88
|
const span = context.startSpan(`event "${context.eventName}"`, {
|
|
104
89
|
id: context.id,
|
|
@@ -3,14 +3,64 @@
|
|
|
3
3
|
* @typedef {import("../../types.js").TracingOptions} TracingOptions
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
const { isFunction, dotGet, isObject } = require('@weave-js/utils');
|
|
7
|
+
|
|
8
|
+
function addPreHandleTagsFromDefinition (context, tags, globalTracingActionOptions, actionTracingOptions) {
|
|
9
|
+
const globalActionTags = globalTracingActionOptions.tags;
|
|
10
|
+
let actionTags;
|
|
11
|
+
if (isFunction(actionTracingOptions.tags)) {
|
|
12
|
+
actionTags = actionTracingOptions.tags;
|
|
13
|
+
} else if (!actionTracingOptions.tags && isFunction(globalActionTags)) {
|
|
14
|
+
actionTags = globalActionTags;
|
|
15
|
+
} else {
|
|
16
|
+
actionTags = { data: globalTracingActionOptions.data, ...globalActionTags, ...actionTracingOptions.tags };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (isObject(actionTags)) {
|
|
20
|
+
if (actionTags.data === true) {
|
|
21
|
+
tags.data = context.data !== null && isObject(context.data) ? Object.assign({}, context.data) : context.data;
|
|
22
|
+
} else if (Array.isArray(actionTags.data)) {
|
|
23
|
+
tags.data = actionTags.data.reduce((acc, current) => {
|
|
24
|
+
try {
|
|
25
|
+
acc[current] = dotGet(context.data, current);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
context.service.log.warn({
|
|
28
|
+
requestId: context.requestId,
|
|
29
|
+
spanId: context.span.id
|
|
30
|
+
}, `Unable to get value for tag "${current}" from data`);
|
|
31
|
+
acc[current] = undefined;
|
|
32
|
+
}
|
|
33
|
+
return acc;
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (actionTags.meta === true) {
|
|
38
|
+
tags.meta = context.meta !== null && isObject(context.meta) ? Object.assign({}, context.meta) : context.meta;
|
|
39
|
+
} else if (Array.isArray(actionTags.meta)) {
|
|
40
|
+
tags.meta = actionTags.meta.reduce((acc, current) => {
|
|
41
|
+
try {
|
|
42
|
+
acc[current] = dotGet(context.meta, current);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
context.service.log.warn({
|
|
45
|
+
requestId: context.requestId,
|
|
46
|
+
spanId: context.span.id
|
|
47
|
+
}, `Unable to get value for tag "${current}" from metadata`);
|
|
48
|
+
acc[current] = undefined;
|
|
49
|
+
}
|
|
50
|
+
return acc;
|
|
51
|
+
}, {});
|
|
52
|
+
}
|
|
53
|
+
} else if (isFunction(actionTags)) {
|
|
54
|
+
tags.data = actionTags.call(context.service, context);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
6
58
|
/**
|
|
7
59
|
* Build span tags object
|
|
8
60
|
* @param {Context} context - Context
|
|
9
|
-
* @param {TracingOptions} brokerTracingOptions - Tracing options
|
|
10
|
-
* @param {TracingOptions} actionTracingOptions - Tracing options
|
|
11
61
|
* @returns {object} Tags
|
|
12
62
|
*/
|
|
13
|
-
module.exports.buildActionTags = (context,
|
|
63
|
+
module.exports.buildActionTags = (context, globalTracingOptions, actionTracingOptions) => {
|
|
14
64
|
const tags = {
|
|
15
65
|
requestLevel: context.level,
|
|
16
66
|
action: context.action ? { name: context.action.name, shortName: context.action.shortName } : null,
|
|
@@ -19,17 +69,34 @@ module.exports.buildActionTags = (context, brokerTracingOptions, actionTracingOp
|
|
|
19
69
|
requestId: context.requestId
|
|
20
70
|
};
|
|
21
71
|
|
|
72
|
+
try {
|
|
73
|
+
addPreHandleTagsFromDefinition(context, tags, globalTracingOptions.actions, actionTracingOptions);
|
|
74
|
+
|
|
75
|
+
let globalTags;
|
|
76
|
+
// local action tags take precedence
|
|
77
|
+
if (isFunction(globalTracingOptions.actions.tags)) {
|
|
78
|
+
globalTags = globalTracingOptions.actions.tags.call(context.service, context);
|
|
79
|
+
} else if (isObject(globalTracingOptions.actions.tags)) {
|
|
80
|
+
globalTags = Object.assign({}, globalTracingOptions.actions.tags);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
Object.assign(tags, globalTags);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
context.service.log.warn({
|
|
86
|
+
requestId: context.requestId,
|
|
87
|
+
spanId: context.span.id
|
|
88
|
+
}, `Error while building action tags: ${error.message}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
22
91
|
return tags;
|
|
23
92
|
};
|
|
24
93
|
|
|
25
94
|
/**
|
|
26
95
|
* Build span tags object
|
|
27
96
|
* @param {Context} context - Context
|
|
28
|
-
* @param {TracingOptions} brokerTracingOptions - Tracing options
|
|
29
|
-
* @param {TracingOptions} actionTracingOptions - Tracing options
|
|
30
97
|
* @returns {object} Tags
|
|
31
98
|
*/
|
|
32
|
-
module.exports.buildEventTags = (context,
|
|
99
|
+
module.exports.buildEventTags = (context, globalTracingOptions, eventTracingOptions) => {
|
|
33
100
|
const tags = {
|
|
34
101
|
requestLevel: context.level,
|
|
35
102
|
event: context.eventName,
|
|
@@ -39,5 +106,45 @@ module.exports.buildEventTags = (context, brokerTracingOptions, actionTracingOpt
|
|
|
39
106
|
requestId: context.requestId
|
|
40
107
|
};
|
|
41
108
|
|
|
109
|
+
try {
|
|
110
|
+
addPreHandleTagsFromDefinition(context, tags, globalTracingOptions.events, eventTracingOptions);
|
|
111
|
+
|
|
112
|
+
let globalTags;
|
|
113
|
+
if (isFunction(globalTracingOptions.events.tags)) {
|
|
114
|
+
globalTags = globalTracingOptions.events.tags.call(context.service, context);
|
|
115
|
+
} else if (isObject(globalTracingOptions.events.tags)) {
|
|
116
|
+
globalTags = Object.assign({}, globalTracingOptions.events.tags);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
Object.assign(tags, globalTags);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
context.service.log.warn({
|
|
122
|
+
requestId: context.requestId,
|
|
123
|
+
spanId: context.span.id
|
|
124
|
+
}, `Error while building event tags: ${error.message}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
42
127
|
return tags;
|
|
43
128
|
};
|
|
129
|
+
|
|
130
|
+
module.exports.addResponseTags = (context, tags, result, globalTracingActionOptions, actionTracingOptions) => {
|
|
131
|
+
const globalActionTags = globalTracingActionOptions.tags;
|
|
132
|
+
const actionTags = { response: globalTracingActionOptions.response, ...globalActionTags, ...actionTracingOptions.tags };
|
|
133
|
+
|
|
134
|
+
if (actionTags.response === true) {
|
|
135
|
+
tags.response = result !== null && isObject(result) ? Object.assign({}, result) : result;
|
|
136
|
+
} else if (Array.isArray(actionTags.response)) {
|
|
137
|
+
tags.response = actionTags.response.reduce((acc, current) => {
|
|
138
|
+
try {
|
|
139
|
+
acc[current] = dotGet(result, current);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
context.service.log.warn({
|
|
142
|
+
requestId: context.requestId,
|
|
143
|
+
spanId: context.span.id
|
|
144
|
+
}, `Unable to get response tag "${current}" from result`);
|
|
145
|
+
acc[current] = undefined;
|
|
146
|
+
}
|
|
147
|
+
return acc;
|
|
148
|
+
}, {});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
@@ -55,10 +55,14 @@ exports.initContextFactory = (runtime) => {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (opts.parentContext != null) {
|
|
58
|
-
context.parentId = opts.parentContext.id;
|
|
59
|
-
context.level = opts.parentContext.level + 1;
|
|
60
58
|
context.tracing = opts.parentContext.tracing;
|
|
61
|
-
context.
|
|
59
|
+
context.level = opts.parentContext.level + 1;
|
|
60
|
+
if (opts.parentContext.span) {
|
|
61
|
+
// context.span = opts.parentContext.span;
|
|
62
|
+
context.parentId = opts.parentContext.span.id;
|
|
63
|
+
} else {
|
|
64
|
+
context.parentId = opts.parentContext.id;
|
|
65
|
+
}
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
if (opts.stream) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { resolveCollector } = require('../tracing/collectors');
|
|
2
|
-
const {
|
|
2
|
+
const { Span } = require('../tracing/span');
|
|
3
3
|
|
|
4
4
|
exports.initTracer = (runtime) => {
|
|
5
5
|
const options = runtime.options.tracing;
|
|
@@ -37,16 +37,30 @@ exports.initTracer = (runtime) => {
|
|
|
37
37
|
invokeCollectorMethod (method, args) {
|
|
38
38
|
collectors.map(collector => collector[method].apply(collector, args));
|
|
39
39
|
},
|
|
40
|
-
startSpan (name,
|
|
40
|
+
startSpan (name, spanOptions) {
|
|
41
41
|
const parentOptions = {};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
parentOptions.
|
|
45
|
-
parentOptions.
|
|
42
|
+
|
|
43
|
+
if (spanOptions.parentSpan) {
|
|
44
|
+
parentOptions.traceId = spanOptions.parentSpan.traceId;
|
|
45
|
+
parentOptions.parentId = spanOptions.parentSpan.id;
|
|
46
|
+
parentOptions.sampled = spanOptions.parentSpan.sampled;
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
|
|
49
|
+
const span = new Span(
|
|
50
|
+
this,
|
|
51
|
+
name,
|
|
52
|
+
Object.assign(
|
|
53
|
+
{
|
|
54
|
+
type: 'custom',
|
|
55
|
+
defaultTags: options.defaultTags
|
|
56
|
+
},
|
|
57
|
+
parentOptions,
|
|
58
|
+
spanOptions,
|
|
59
|
+
{
|
|
60
|
+
parentSpan: undefined
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
);
|
|
50
64
|
|
|
51
65
|
span.start();
|
|
52
66
|
|
package/lib/tracing/span.js
CHANGED
|
@@ -1,69 +1,156 @@
|
|
|
1
1
|
const hrTime = require('./time');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
parentId: options.parentId,
|
|
9
|
-
type: options.type,
|
|
10
|
-
sampled: options.sampled || tracer.shouldSample(),
|
|
11
|
-
service: options.service,
|
|
12
|
-
tags: {}
|
|
3
|
+
function defineReadonlyProperty (instance, propName, value, readOnly = false) {
|
|
4
|
+
Object.defineProperty(instance, propName, {
|
|
5
|
+
value,
|
|
6
|
+
writable: !!readOnly,
|
|
7
|
+
enumerable: false
|
|
13
8
|
});
|
|
9
|
+
}
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
11
|
+
// transform this factory function into a class
|
|
12
|
+
exports.Span = class Span {
|
|
13
|
+
constructor (tracer, name, options) {
|
|
14
|
+
defineReadonlyProperty(this, 'tracer', tracer, true);
|
|
15
|
+
// defineReadonlyProperty(this, 'logger', this.tracer.logger, true);
|
|
16
|
+
defineReadonlyProperty(this, 'options', options || {});
|
|
17
|
+
defineReadonlyProperty(this, 'meta', {});
|
|
18
|
+
|
|
19
|
+
this.name = name;
|
|
20
|
+
this.id = options.id || tracer.runtime.generateUUID();
|
|
21
|
+
this.traceId = options.traceId || this.id;
|
|
22
|
+
this.parentId = options.parentId;
|
|
23
|
+
this.type = options.type || 'custom';
|
|
24
|
+
this.sampled = options.sampled || tracer.shouldSample();
|
|
25
|
+
this.tags = {};
|
|
26
|
+
|
|
27
|
+
if (options.service) {
|
|
28
|
+
this.service = {
|
|
29
|
+
name: options.service.name,
|
|
30
|
+
version: options.service.version,
|
|
31
|
+
fullyQualifiedName: options.service.fullyQualifiedName
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (options.defaultTags) {
|
|
36
|
+
this.addTags(options.defaultTags);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (options.tags) {
|
|
40
|
+
this.addTags(options.tags);
|
|
41
|
+
}
|
|
21
42
|
}
|
|
22
43
|
|
|
23
|
-
|
|
24
|
-
Object.assign(
|
|
25
|
-
return
|
|
26
|
-
}
|
|
44
|
+
addTags (tags) {
|
|
45
|
+
Object.assign(this.tags, tags);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
27
48
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
tracer.invokeCollectorMethod('startedSpan', [
|
|
49
|
+
start (time) {
|
|
50
|
+
this.startTime = time || hrTime();
|
|
51
|
+
if (this.sampled) {
|
|
52
|
+
this.tracer.invokeCollectorMethod('startedSpan', [this]);
|
|
32
53
|
}
|
|
33
|
-
return
|
|
34
|
-
}
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
35
56
|
|
|
36
|
-
|
|
57
|
+
startChildSpan (name, options) {
|
|
37
58
|
const parentOptions = {
|
|
38
|
-
parentId:
|
|
39
|
-
|
|
59
|
+
parentId: this.id,
|
|
60
|
+
traceId: this.traceId,
|
|
61
|
+
sampled: this.sampled,
|
|
62
|
+
service: this.service
|
|
40
63
|
};
|
|
41
|
-
return tracer.startSpan(name, Object.assign(parentOptions, options));
|
|
42
|
-
}
|
|
64
|
+
return this.tracer.startSpan(name, Object.assign(parentOptions, options));
|
|
65
|
+
}
|
|
43
66
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
67
|
+
finish (time) {
|
|
68
|
+
this.finishTime = time || hrTime();
|
|
69
|
+
this.duration = this.finishTime - this.startTime;
|
|
47
70
|
|
|
48
|
-
tracer.log.debug(`Span "${
|
|
71
|
+
this.tracer.log.debug(`Span "${this.id}" finished`);
|
|
49
72
|
|
|
50
|
-
if (
|
|
51
|
-
tracer.invokeCollectorMethod('finishedSpan', [
|
|
73
|
+
if (this.sampled) {
|
|
74
|
+
this.tracer.invokeCollectorMethod('finishedSpan', [this]);
|
|
52
75
|
}
|
|
53
76
|
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
span.isActive = () => span.finishTime !== null;
|
|
58
|
-
|
|
59
|
-
span.setError = (error) => {
|
|
60
|
-
span.error = error;
|
|
61
|
-
return span;
|
|
62
|
-
};
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
63
79
|
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
isActive () {
|
|
81
|
+
return this.finishTime !== null;
|
|
66
82
|
}
|
|
67
83
|
|
|
68
|
-
|
|
84
|
+
setError (error) {
|
|
85
|
+
this.error = error;
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
69
88
|
};
|
|
89
|
+
|
|
90
|
+
// exports.createSpan = (tracer, name, options) => {
|
|
91
|
+
// const span = Object.assign({}, {
|
|
92
|
+
// name,
|
|
93
|
+
// id: options.id || tracer.runtime.generateUUID(),
|
|
94
|
+
// traceId: options.traceId || span.id,
|
|
95
|
+
// parentId: options.parentId,
|
|
96
|
+
// type: options.type || 'custom',
|
|
97
|
+
// sampled: options.sampled || tracer.shouldSample(),
|
|
98
|
+
// service: options.service,
|
|
99
|
+
// tags: {}
|
|
100
|
+
// });
|
|
101
|
+
|
|
102
|
+
// if (options.service) {
|
|
103
|
+
// span.service = {
|
|
104
|
+
// name: options.service.name,
|
|
105
|
+
// version: options.service.version,
|
|
106
|
+
// fullyQualifiedName: options.service.fullyQualifiedName
|
|
107
|
+
// };
|
|
108
|
+
// }
|
|
109
|
+
|
|
110
|
+
// span.addTags = (tags) => {
|
|
111
|
+
// Object.assign(span.tags, tags);
|
|
112
|
+
// return span;
|
|
113
|
+
// };
|
|
114
|
+
|
|
115
|
+
// span.start = (time) => {
|
|
116
|
+
// span.startTime = time || hrTime();
|
|
117
|
+
// if (span.sampled) {
|
|
118
|
+
// tracer.invokeCollectorMethod('startedSpan', [span]);
|
|
119
|
+
// }
|
|
120
|
+
// return span;
|
|
121
|
+
// };
|
|
122
|
+
|
|
123
|
+
// span.startChildSpan = (name, options) => {
|
|
124
|
+
// const parentOptions = {
|
|
125
|
+
// parentId: options.parentId,
|
|
126
|
+
// sampled: options.sampled
|
|
127
|
+
// };
|
|
128
|
+
// return tracer.startSpan(name, Object.assign(parentOptions, options));
|
|
129
|
+
// };
|
|
130
|
+
|
|
131
|
+
// span.finish = (time) => {
|
|
132
|
+
// span.finishTime = time || hrTime();
|
|
133
|
+
// span.duration = span.finishTime - span.startTime;
|
|
134
|
+
|
|
135
|
+
// tracer.log.debug(`Span "${span.id}" finished`);
|
|
136
|
+
|
|
137
|
+
// if (span.sampled) {
|
|
138
|
+
// tracer.invokeCollectorMethod('finishedSpan', [span]);
|
|
139
|
+
// }
|
|
140
|
+
|
|
141
|
+
// return span;
|
|
142
|
+
// };
|
|
143
|
+
|
|
144
|
+
// span.isActive = () => span.finishTime !== null;
|
|
145
|
+
|
|
146
|
+
// span.setError = (error) => {
|
|
147
|
+
// span.error = error;
|
|
148
|
+
// return span;
|
|
149
|
+
// };
|
|
150
|
+
|
|
151
|
+
// if (options.tags) {
|
|
152
|
+
// span.addTags(options.tags);
|
|
153
|
+
// }
|
|
154
|
+
|
|
155
|
+
// return span;
|
|
156
|
+
// };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const TransportBase = require('../
|
|
7
|
+
const TransportBase = require('../adapterBase');
|
|
8
8
|
const EventEmitter = require('eventemitter2').EventEmitter2;
|
|
9
9
|
|
|
10
10
|
// create a global eventbus to pass messages between weave service brokers.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
const { defaultsDeep } = require('@weave-js/utils');
|
|
3
|
-
const TransportBase = require('../
|
|
3
|
+
const TransportBase = require('../adapterBase');
|
|
4
4
|
const Swim = require('./discovery/index');
|
|
5
5
|
const MessageTypes = require('../../messageTypes');
|
|
6
6
|
const TCPReader = require('./tcpReader');
|
package/lib/types.js
CHANGED
|
@@ -167,7 +167,7 @@ const { EventEmitter2: EventEmitter } = require('eventemitter2');
|
|
|
167
167
|
* @property {Boolean} enabled - Enable tracing middleware. (default = false)
|
|
168
168
|
* @property {Number} samplingRate - Rate of traced actions. (default = 1.0)
|
|
169
169
|
* @property {Array<String|Object>} collectors - Array of tracing collectors.
|
|
170
|
-
* @property {
|
|
170
|
+
* @property {Object.<string, string>} defaultTags - Default tags for spans.
|
|
171
171
|
* @property {TracingErrorOptions} [errors] - Settings for tracing errors.
|
|
172
172
|
*/
|
|
173
173
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-js/core",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.2",
|
|
4
4
|
"description": "The core package of weave",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Weave",
|
|
@@ -39,11 +39,10 @@
|
|
|
39
39
|
},
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@weave-js/errors": "^0.9.1",
|
|
43
42
|
"@weave-js/utils": "^0.13.0",
|
|
44
43
|
"@weave-js/validator": "^0.14.0",
|
|
45
44
|
"eventemitter2": "^6.4.9",
|
|
46
|
-
"glob": "^10.
|
|
45
|
+
"glob": "^10.3.10"
|
|
47
46
|
},
|
|
48
47
|
"directories": {
|
|
49
48
|
"lib": "lib",
|
|
File without changes
|