@weave-js/core 0.10.0 → 0.12.0
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/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +58 -46
- package/lib/broker/{default-options.js → defaultOptions.js} +31 -13
- package/lib/broker/index.js +273 -251
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/adapters/base.js +119 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +10 -0
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +34 -11
- package/lib/constants.js +11 -8
- package/lib/{error-handler.js → errorHandler.js} +8 -8
- package/lib/errors.js +46 -39
- package/lib/helper/{define-action.js → defineAction.js} +5 -3
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +4 -2
- package/lib/helper/{define-service.js → defineService.js} +6 -3
- package/lib/index.js +25 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -14
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +28 -25
- package/lib/logger/levels.js +23 -22
- package/lib/logger/tools.js +17 -13
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +89 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +82 -34
- package/lib/middlewares/bulkhead/index.js +34 -33
- package/lib/middlewares/cache/index.js +106 -0
- package/lib/middlewares/circuit-breaker/index.js +85 -60
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +3 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +18 -18
- package/lib/middlewares/tracing/index.js +50 -37
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +32 -32
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +8 -8
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +39 -35
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +64 -65
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +65 -69
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +45 -45
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +61 -55
- package/lib/registry/eventEndpoint.js +26 -0
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +147 -163
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/{service-item.js → serviceItem.js} +21 -21
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +21 -21
- package/lib/runtime/initCache.js +19 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +22 -22
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +54 -53
- package/lib/runtime/{init-logger.js → initLogger.js} +11 -11
- package/lib/runtime/{init-metrics.js → initMetrics.js} +55 -54
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +21 -21
- package/lib/runtime/{init-registry.js → initRegistry.js} +6 -6
- package/lib/runtime/initServiceManager.js +145 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +25 -24
- package/lib/runtime/{init-transport.js → initTransport.js} +5 -5
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +4 -4
- package/lib/runtime/{init-validator.js → initValidator.js} +5 -5
- package/lib/tracing/collectors/base.js +29 -37
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +16 -16
- package/lib/tracing/span.js +34 -32
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/adapteBase.js +107 -0
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +19 -19
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +90 -76
- package/lib/transport/adapters/tcp/index.js +180 -175
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -38
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +58 -58
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/createTransport.js +377 -334
- package/lib/transport/errorPayloadFactory.js +15 -0
- package/lib/transport/messageHandlers.js +517 -0
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +95 -71
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +47 -45
- package/lib/utils/{restore-error.js → restoreError.js} +4 -4
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1334
- package/lib/build-runtime.js +0 -70
- package/lib/cache/base.js +0 -142
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -121
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/registry/event-endpoint.js +0 -26
- package/lib/runtime/init-cache.js +0 -10
- package/lib/runtime/init-service-manager.js +0 -128
- package/lib/tracing/collectors/console.js +0 -34
- package/lib/tracing/index.js +0 -3
- package/lib/tracing/tracer.js +0 -64
- package/lib/transport/adapters/adapter-base.js +0 -92
- package/lib/transport/message-handlers.js +0 -448
- package/lib/transport/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
const { Constants } = require('../../metrics')
|
|
1
|
+
const { Constants } = require('../../metrics');
|
|
2
2
|
|
|
3
3
|
module.exports.getMiddlewareWrapper = (runtime) => function (type, action, handler) {
|
|
4
|
-
const serviceName = action.service ? action.service.fullyQualifiedName : null
|
|
5
|
-
const actionName = action.name
|
|
4
|
+
const serviceName = action.service ? action.service.fullyQualifiedName : null;
|
|
5
|
+
const actionName = action.name;
|
|
6
6
|
|
|
7
7
|
return function metricMiddleware (context, serviceInjections) {
|
|
8
|
-
const callerNodeId = context.callerNodeId
|
|
8
|
+
const callerNodeId = context.callerNodeId;
|
|
9
9
|
|
|
10
|
-
runtime.metrics.increment(Constants.REQUESTS_TOTAL, { type, serviceName, actionName, callerNodeId })
|
|
11
|
-
runtime.metrics.increment(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
12
|
-
const requestEnd = runtime.metrics.timer(Constants.REQUESTS_TIME, { type, serviceName, actionName, callerNodeId })
|
|
10
|
+
runtime.metrics.increment(Constants.REQUESTS_TOTAL, { type, serviceName, actionName, callerNodeId });
|
|
11
|
+
runtime.metrics.increment(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
12
|
+
const requestEnd = runtime.metrics.timer(Constants.REQUESTS_TIME, { type, serviceName, actionName, callerNodeId });
|
|
13
13
|
|
|
14
14
|
return handler(context, serviceInjections)
|
|
15
15
|
.then(result => {
|
|
16
|
-
requestEnd()
|
|
17
|
-
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
18
|
-
return result
|
|
16
|
+
requestEnd();
|
|
17
|
+
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
18
|
+
return result;
|
|
19
19
|
})
|
|
20
20
|
.catch(error => {
|
|
21
|
-
requestEnd()
|
|
22
|
-
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId })
|
|
23
|
-
runtime.metrics.increment(Constants.REQUESTS_ERRORS_TOTAL)
|
|
24
|
-
runtime.handleError(error)
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
}
|
|
21
|
+
requestEnd();
|
|
22
|
+
runtime.metrics.decrement(Constants.REQUESTS_IN_FLIGHT, { type, serviceName, actionName, callerNodeId });
|
|
23
|
+
runtime.metrics.increment(Constants.REQUESTS_ERRORS_TOTAL);
|
|
24
|
+
runtime.handleError(error);
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
|
-
const { Constants } = require('../../metrics')
|
|
7
|
-
const { getMiddlewareWrapper } = require('./getMiddlewareWrapper')
|
|
6
|
+
const { Constants } = require('../../metrics');
|
|
7
|
+
const { getMiddlewareWrapper } = require('./getMiddlewareWrapper');
|
|
8
8
|
|
|
9
9
|
module.exports = (runtime) => {
|
|
10
|
-
const wrapMetricMiddleware = getMiddlewareWrapper(runtime)
|
|
10
|
+
const wrapMetricMiddleware = getMiddlewareWrapper(runtime);
|
|
11
11
|
|
|
12
12
|
return {
|
|
13
13
|
created () {
|
|
14
14
|
// Request metrics
|
|
15
|
-
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_TOTAL, description: 'Number of total requests.' })
|
|
16
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_IN_FLIGHT, description: 'Number of running requests.' })
|
|
17
|
-
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_ERRORS_TOTAL, description: 'Number of failed requests.' })
|
|
18
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_TIME, description: 'Request times in milliseconds' })
|
|
15
|
+
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_TOTAL, description: 'Number of total requests.' });
|
|
16
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_IN_FLIGHT, description: 'Number of running requests.' });
|
|
17
|
+
runtime.metrics.register({ type: 'counter', name: Constants.REQUESTS_ERRORS_TOTAL, description: 'Number of failed requests.' });
|
|
18
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.REQUESTS_TIME, description: 'Request times in milliseconds' });
|
|
19
19
|
|
|
20
20
|
// Event metrics
|
|
21
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_EMITS, description: 'Number of total emitted events.' })
|
|
22
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS, description: 'Number of total broadcasted events.' })
|
|
23
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS_LOCAL, description: 'Number of total local broadcasted events.' })
|
|
24
|
-
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_RECEIVED, description: 'Number of total received events.' })
|
|
21
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_EMITS, description: 'Number of total emitted events.' });
|
|
22
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS, description: 'Number of total broadcasted events.' });
|
|
23
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_BROADCASTS_LOCAL, description: 'Number of total local broadcasted events.' });
|
|
24
|
+
runtime.metrics.register({ type: 'counter', name: Constants.EVENT_TOTAL_RECEIVED, description: 'Number of total received events.' });
|
|
25
25
|
|
|
26
26
|
// Transport metrics
|
|
27
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' })
|
|
28
|
-
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_SENT, description: 'Number of in flight streams.' })
|
|
29
|
-
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_RECEIVED, description: 'Number of in flight streams.' })
|
|
30
|
-
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' })
|
|
27
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' });
|
|
28
|
+
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_SENT, description: 'Number of in flight streams.' });
|
|
29
|
+
runtime.metrics.register({ type: 'counter', name: Constants.TRANSPORTER_PACKETS_RECEIVED, description: 'Number of in flight streams.' });
|
|
30
|
+
runtime.metrics.register({ type: 'gauge', name: Constants.TRANSPORT_IN_FLIGHT_STREAMS, description: 'Number of in flight streams.' });
|
|
31
31
|
},
|
|
32
32
|
localAction (next, action) {
|
|
33
|
-
return wrapMetricMiddleware('local', action, next)
|
|
33
|
+
return wrapMetricMiddleware('local', action, next);
|
|
34
34
|
},
|
|
35
35
|
remoteAction (next, action) {
|
|
36
|
-
return wrapMetricMiddleware
|
|
36
|
+
return wrapMetricMiddleware('remote', action, next);
|
|
37
37
|
},
|
|
38
38
|
emit (next) {
|
|
39
39
|
return (event, payload) => {
|
|
40
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_EMITS)
|
|
41
|
-
return next(event, payload)
|
|
42
|
-
}
|
|
40
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_EMITS);
|
|
41
|
+
return next(event, payload);
|
|
42
|
+
};
|
|
43
43
|
},
|
|
44
44
|
broadcast (next) {
|
|
45
45
|
return (event, payload) => {
|
|
46
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS)
|
|
47
|
-
return next(event, payload)
|
|
48
|
-
}
|
|
46
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS);
|
|
47
|
+
return next(event, payload);
|
|
48
|
+
};
|
|
49
49
|
},
|
|
50
50
|
broadcastLocal (next) {
|
|
51
51
|
return (event, payload) => {
|
|
52
|
-
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS_LOCAL)
|
|
53
|
-
return next(event, payload)
|
|
54
|
-
}
|
|
52
|
+
runtime.metrics.increment(Constants.EVENT_TOTAL_BROADCASTS_LOCAL);
|
|
53
|
+
return next(event, payload);
|
|
54
|
+
};
|
|
55
55
|
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
|
|
2
2
|
/*
|
|
3
|
-
* Author: Kevin Ries (kevin@
|
|
3
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
4
4
|
* -----
|
|
5
5
|
* Copyright 2021 Fachwerk
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const { delay } = require('@weave-js/utils')
|
|
8
|
+
const { delay } = require('@weave-js/utils');
|
|
9
9
|
|
|
10
10
|
const wrapRetryMiddleware = function (handler, action) {
|
|
11
|
-
const self = this
|
|
12
|
-
const options = Object.assign({}, self.options.retryPolicy, action.retryPolicy || {})
|
|
11
|
+
const self = this;
|
|
12
|
+
const options = Object.assign({}, self.options.retryPolicy, action.retryPolicy || {});
|
|
13
13
|
|
|
14
14
|
// middleware is enabled
|
|
15
15
|
if (options.enabled) {
|
|
@@ -17,28 +17,28 @@ const wrapRetryMiddleware = function (handler, action) {
|
|
|
17
17
|
return function retryMiddleware (context, serviceInjections) {
|
|
18
18
|
// if the context has no repeat count, set it to zero.
|
|
19
19
|
if (context.retryCount === undefined) {
|
|
20
|
-
context.retryCount = 0
|
|
20
|
+
context.retryCount = 0;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const attempts = typeof context.options.retries === 'number' ? context.options.retries : options.retries
|
|
23
|
+
const attempts = typeof context.options.retries === 'number' ? context.options.retries : options.retries;
|
|
24
24
|
|
|
25
25
|
return handler(context, serviceInjections)
|
|
26
26
|
.catch(error => {
|
|
27
27
|
if (context.retryCount++ < attempts && error.retryable === true) {
|
|
28
|
-
self.log.warn(`Retry to recall action '${context.action.name}' after ${options.delay}.`)
|
|
28
|
+
self.log.warn(`Retry to recall action '${context.action.name}' after ${options.delay}.`);
|
|
29
29
|
return delay(options.delay)
|
|
30
|
-
.then(() => self.call(context.action.name, context.data, { context }))
|
|
30
|
+
.then(() => self.call(context.action.name, context.data, { context }));
|
|
31
31
|
}
|
|
32
|
-
return Promise.reject(error)
|
|
33
|
-
})
|
|
34
|
-
}
|
|
32
|
+
return Promise.reject(error);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
35
|
}
|
|
36
|
-
return handler
|
|
37
|
-
}
|
|
36
|
+
return handler;
|
|
37
|
+
};
|
|
38
38
|
|
|
39
39
|
module.exports = () => {
|
|
40
40
|
return {
|
|
41
41
|
localAction: wrapRetryMiddleware,
|
|
42
42
|
remoteAction: wrapRetryMiddleware
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { promiseTimeout } = require('@weave-js/utils')
|
|
8
|
-
const { WeaveRequestTimeoutError } = require('../../errors')
|
|
7
|
+
const { promiseTimeout } = require('@weave-js/utils');
|
|
8
|
+
const { WeaveRequestTimeoutError } = require('../../errors');
|
|
9
9
|
|
|
10
|
-
const wrapTimeoutMiddleware = function (handler
|
|
11
|
-
const self = this
|
|
12
|
-
const registryOptions = self.options.registry || {}
|
|
10
|
+
const wrapTimeoutMiddleware = function (handler) {
|
|
11
|
+
const self = this;
|
|
12
|
+
const registryOptions = self.options.registry || {};
|
|
13
13
|
|
|
14
14
|
return function timeoutMiddleware (context, serviceInjections) {
|
|
15
15
|
if (typeof context.options.timeout === 'undefined' || registryOptions.requestTimeout) {
|
|
16
|
-
context.options.timeout = registryOptions.requestTimeout || 0
|
|
16
|
+
context.options.timeout = registryOptions.requestTimeout || 0;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
if (context.options.timeout > 0 && !context.startHighResolutionTime) {
|
|
20
|
-
context.startHighResolutionTime = process.hrtime()
|
|
20
|
+
context.startHighResolutionTime = process.hrtime();
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
let promise = handler(context, serviceInjections)
|
|
23
|
+
let promise = handler(context, serviceInjections);
|
|
24
24
|
|
|
25
25
|
if (context.options.timeout > 0) {
|
|
26
|
-
promise = promiseTimeout(context.options.timeout, promise, new WeaveRequestTimeoutError(context.action.name, context.nodeId))
|
|
26
|
+
promise = promiseTimeout(context.options.timeout, promise, new WeaveRequestTimeoutError(context.action.name, context.nodeId, context.options.timeout))
|
|
27
27
|
.catch(error => {
|
|
28
28
|
if (error instanceof WeaveRequestTimeoutError) {
|
|
29
|
-
self.log.warn(`Request '${context.action.name}' timed out.`)
|
|
29
|
+
self.log.warn(`Request '${context.action.name}' timed out.`);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
return Promise.reject(error)
|
|
33
|
-
})
|
|
32
|
+
return Promise.reject(error);
|
|
33
|
+
});
|
|
34
34
|
}
|
|
35
|
-
return promise
|
|
36
|
-
}
|
|
37
|
-
}
|
|
35
|
+
return promise;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
38
|
|
|
39
39
|
module.exports = () => {
|
|
40
40
|
return {
|
|
41
41
|
localAction: wrapTimeoutMiddleware,
|
|
42
42
|
remoteAction: wrapTimeoutMiddleware
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { isPlainObject } = require('@weave-js/utils');
|
|
8
|
+
const { buildActionTags, buildEventTags } = require('./tags');
|
|
8
9
|
|
|
9
10
|
const wrapTracingLocalActionMiddleware = function (handler) {
|
|
10
|
-
const broker = this
|
|
11
|
-
const
|
|
11
|
+
const broker = this;
|
|
12
|
+
const tracingOptions = broker.options.tracing || {};
|
|
12
13
|
|
|
13
|
-
if (
|
|
14
|
+
if (tracingOptions.enabled) {
|
|
14
15
|
return function metricsLocalMiddleware (context, serviceInjections) {
|
|
15
|
-
const tags =
|
|
16
|
+
const tags = buildActionTags(context, tracingOptions);
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
if (tracingOptions) {
|
|
19
|
+
tags.data = context.data !== null && isPlainObject(context.data) ? Object.assign({}, context.data) : context.data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const spanName = `action "${context.action.name}"`;
|
|
18
23
|
|
|
19
24
|
const span = context.startSpan(spanName, {
|
|
20
25
|
id: context.id,
|
|
@@ -24,36 +29,45 @@ const wrapTracingLocalActionMiddleware = function (handler) {
|
|
|
24
29
|
service: context.service,
|
|
25
30
|
tags,
|
|
26
31
|
sampled: context.tracing
|
|
27
|
-
})
|
|
32
|
+
});
|
|
28
33
|
|
|
29
|
-
context.span = span
|
|
34
|
+
context.span = span;
|
|
30
35
|
|
|
31
36
|
return handler(context, serviceInjections)
|
|
32
37
|
.then(result => {
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
const tags = {
|
|
39
|
+
isCachedResult: context.isCachedResult
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
if (tracingOptions) {
|
|
43
|
+
tags.response = result !== null && isPlainObject(result) ? Object.assign({}, result) : result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
span.addTags(tags);
|
|
47
|
+
span.finish();
|
|
48
|
+
return result;
|
|
35
49
|
})
|
|
36
50
|
.catch(error => {
|
|
37
51
|
span
|
|
38
52
|
.setError(error)
|
|
39
|
-
.finish()
|
|
40
|
-
return Promise.reject(error)
|
|
41
|
-
})
|
|
42
|
-
}
|
|
53
|
+
.finish();
|
|
54
|
+
return Promise.reject(error);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
43
57
|
}
|
|
44
|
-
return handler
|
|
45
|
-
}
|
|
58
|
+
return handler;
|
|
59
|
+
};
|
|
46
60
|
|
|
47
61
|
const wrapTracingLocalEventMiddleware = function (handler, event) {
|
|
48
|
-
const broker = this
|
|
49
|
-
const service = event.service
|
|
50
|
-
const
|
|
62
|
+
const broker = this;
|
|
63
|
+
const service = event.service;
|
|
64
|
+
const tracingOptions = broker.options.tracing || {};
|
|
51
65
|
|
|
52
|
-
if (
|
|
66
|
+
if (tracingOptions.enabled) {
|
|
53
67
|
return function metricsLocalMiddleware (context) {
|
|
54
|
-
const tags =
|
|
68
|
+
const tags = buildEventTags(context);
|
|
55
69
|
|
|
56
|
-
const span = context.startSpan(`event
|
|
70
|
+
const span = context.startSpan(`event "${context.eventName}"`, {
|
|
57
71
|
id: context.id,
|
|
58
72
|
traceId: context.requestId,
|
|
59
73
|
parentId: context.parentId,
|
|
@@ -61,30 +75,29 @@ const wrapTracingLocalEventMiddleware = function (handler, event) {
|
|
|
61
75
|
service,
|
|
62
76
|
tags,
|
|
63
77
|
sampled: context.tracing
|
|
64
|
-
})
|
|
78
|
+
});
|
|
65
79
|
|
|
66
|
-
context.span = span
|
|
80
|
+
context.span = span;
|
|
67
81
|
|
|
68
82
|
return handler(context)
|
|
69
83
|
.then(result => {
|
|
70
|
-
span.
|
|
71
|
-
|
|
72
|
-
return result
|
|
84
|
+
span.finish();
|
|
85
|
+
return result;
|
|
73
86
|
})
|
|
74
87
|
.catch(error => {
|
|
75
88
|
span
|
|
76
89
|
.setError(error)
|
|
77
|
-
.finish()
|
|
78
|
-
return Promise.reject(error)
|
|
79
|
-
})
|
|
80
|
-
}
|
|
90
|
+
.finish();
|
|
91
|
+
return Promise.reject(error);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
81
94
|
}
|
|
82
|
-
return handler
|
|
83
|
-
}
|
|
95
|
+
return handler;
|
|
96
|
+
};
|
|
84
97
|
|
|
85
|
-
module.exports = (
|
|
98
|
+
module.exports = () => {
|
|
86
99
|
return {
|
|
87
100
|
localAction: wrapTracingLocalActionMiddleware,
|
|
88
101
|
localEvent: wrapTracingLocalEventMiddleware
|
|
89
|
-
}
|
|
90
|
-
}
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import("../../types.js").Context} Context
|
|
3
|
+
* @typedef {import("../../types.js").TracingOptions} TracingOptions
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Build span tags object
|
|
8
|
+
* @param {Context} context - Context
|
|
9
|
+
* @param {TracingOptions} brokerTracingOptions - Tracing options
|
|
10
|
+
* @param {TracingOptions} actionTracingOptions - Tracing options
|
|
11
|
+
* @returns {object} Tags
|
|
12
|
+
*/
|
|
13
|
+
module.exports.buildActionTags = (context, brokerTracingOptions, actionTracingOptions) => {
|
|
14
|
+
const tags = {
|
|
15
|
+
requestLevel: context.level,
|
|
16
|
+
action: context.action ? { name: context.action.name, shortName: context.action.shortName } : null,
|
|
17
|
+
isRemoteCall: !!context.callerNodeId,
|
|
18
|
+
nodeId: context.nodeId,
|
|
19
|
+
requestId: context.requestId
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
return tags;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build span tags object
|
|
27
|
+
* @param {Context} context - Context
|
|
28
|
+
* @param {TracingOptions} brokerTracingOptions - Tracing options
|
|
29
|
+
* @param {TracingOptions} actionTracingOptions - Tracing options
|
|
30
|
+
* @returns {object} Tags
|
|
31
|
+
*/
|
|
32
|
+
module.exports.buildEventTags = (context, brokerTracingOptions, actionTracingOptions) => {
|
|
33
|
+
const tags = {
|
|
34
|
+
requestLevel: context.level,
|
|
35
|
+
event: context.eventName,
|
|
36
|
+
eventType: context.eventType,
|
|
37
|
+
isRemoteCall: !!context.callerNodeId,
|
|
38
|
+
nodeId: context.nodeId,
|
|
39
|
+
requestId: context.requestId
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return tags;
|
|
43
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { WeaveParameterValidationError } = require('../../errors')
|
|
8
|
-
const { capitalize } = require('@weave-js/utils')
|
|
7
|
+
const { WeaveParameterValidationError } = require('../../errors');
|
|
8
|
+
const { capitalize } = require('@weave-js/utils');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {import('../../types').Runtime} Runtime
|
|
@@ -16,79 +16,79 @@ const { capitalize } = require('@weave-js/utils')
|
|
|
16
16
|
* Create validator middleware
|
|
17
17
|
* @param {Runtime} runtime - Runtime reference
|
|
18
18
|
* @returns {Middleware} - Validator middleware
|
|
19
|
-
|
|
19
|
+
*/
|
|
20
20
|
module.exports = (runtime) => {
|
|
21
|
-
const validator = runtime.validator
|
|
21
|
+
const validator = runtime.validator;
|
|
22
22
|
|
|
23
23
|
const processErrors = (context, type, results) => {
|
|
24
|
-
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }))
|
|
25
|
-
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors))
|
|
26
|
-
}
|
|
24
|
+
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }));
|
|
25
|
+
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors));
|
|
26
|
+
};
|
|
27
27
|
|
|
28
28
|
return {
|
|
29
29
|
localAction (handler, action) {
|
|
30
30
|
const parameterOptions = Object.assign(
|
|
31
31
|
runtime.options.validatorOptions,
|
|
32
32
|
action.validatorOptions
|
|
33
|
-
)
|
|
33
|
+
);
|
|
34
34
|
|
|
35
35
|
// validate request schema
|
|
36
|
-
let validateRequestSchema
|
|
37
|
-
let validateResponseSchema
|
|
36
|
+
let validateRequestSchema;
|
|
37
|
+
let validateResponseSchema;
|
|
38
38
|
if (action.params && typeof action.params === 'object') {
|
|
39
|
-
validateRequestSchema = validator.compile(action.params, parameterOptions)
|
|
39
|
+
validateRequestSchema = validator.compile(action.params, parameterOptions);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
if (action.responseSchema && typeof action.responseSchema === 'object') {
|
|
43
|
-
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions)
|
|
43
|
+
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
if (!validateRequestSchema && !validateRequestSchema) {
|
|
47
|
-
return handler
|
|
47
|
+
return handler;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
return (context, serviceInjections) => {
|
|
51
|
-
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true
|
|
51
|
+
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true;
|
|
52
52
|
|
|
53
53
|
if (requestSchemaResult === true) {
|
|
54
54
|
return handler(context, serviceInjections)
|
|
55
55
|
.then((result) => {
|
|
56
56
|
if (validateResponseSchema) {
|
|
57
|
-
const responseSchemaResult = validateResponseSchema(result)
|
|
57
|
+
const responseSchemaResult = validateResponseSchema(result);
|
|
58
58
|
if (responseSchemaResult === true) {
|
|
59
|
-
return result
|
|
59
|
+
return result;
|
|
60
60
|
}
|
|
61
|
-
return processErrors(context, 'response', responseSchemaResult)
|
|
61
|
+
return processErrors(context, 'response', responseSchemaResult);
|
|
62
62
|
}
|
|
63
|
-
return result
|
|
64
|
-
})
|
|
63
|
+
return result;
|
|
64
|
+
});
|
|
65
65
|
} else {
|
|
66
66
|
// Enriching the validator errors with some useful information
|
|
67
|
-
return processErrors(context, 'request', requestSchemaResult)
|
|
67
|
+
return processErrors(context, 'request', requestSchemaResult);
|
|
68
68
|
}
|
|
69
|
-
}
|
|
69
|
+
};
|
|
70
70
|
},
|
|
71
71
|
localEvent (handler, event) {
|
|
72
72
|
if (event.params && typeof event.params === 'object') {
|
|
73
73
|
const parameterOptions = Object.assign(
|
|
74
74
|
runtime.options.validatorOptions,
|
|
75
75
|
event.validatorOptions
|
|
76
|
-
)
|
|
76
|
+
);
|
|
77
77
|
|
|
78
|
-
const validate = validator.compile(event.params, parameterOptions)
|
|
78
|
+
const validate = validator.compile(event.params, parameterOptions);
|
|
79
79
|
|
|
80
80
|
return (context, serviceInjections) => {
|
|
81
|
-
let result = validate(context.data)
|
|
81
|
+
let result = validate(context.data);
|
|
82
82
|
|
|
83
83
|
if (result === true) {
|
|
84
|
-
return handler(context, serviceInjections)
|
|
84
|
+
return handler(context, serviceInjections);
|
|
85
85
|
} else {
|
|
86
|
-
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }))
|
|
87
|
-
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result))
|
|
86
|
+
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }));
|
|
87
|
+
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result));
|
|
88
88
|
}
|
|
89
|
-
}
|
|
89
|
+
};
|
|
90
90
|
}
|
|
91
|
-
return handler
|
|
91
|
+
return handler;
|
|
92
92
|
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/*
|
|
10
|
-
* Author: Kevin Ries (kevin@
|
|
10
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
11
11
|
* -----
|
|
12
12
|
* Copyright 2021 Fachwerk
|
|
13
13
|
*/
|
|
@@ -31,15 +31,15 @@ exports.createActionEndpoint = (runtime, node, service, action) => {
|
|
|
31
31
|
isLocal: node.id === runtime.nodeId,
|
|
32
32
|
state: true,
|
|
33
33
|
name: `${node.id}:${action.name}`
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
endpoint.updateAction = (newAction) => {
|
|
37
|
-
endpoint.action = newAction
|
|
38
|
-
}
|
|
37
|
+
endpoint.action = newAction;
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
endpoint.isAvailable = () => {
|
|
41
|
-
return endpoint.state === true
|
|
42
|
-
}
|
|
41
|
+
return endpoint.state === true;
|
|
42
|
+
};
|
|
43
43
|
|
|
44
|
-
return endpoint
|
|
45
|
-
}
|
|
44
|
+
return endpoint;
|
|
45
|
+
};
|