@weave-js/core 0.11.0 → 0.12.1
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 +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +9 -9
- package/lib/broker/index.js +161 -165
- package/lib/buildRuntime.js +34 -34
- 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 +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -22
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -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 +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +63 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +47 -15
- package/lib/middlewares/index.js +2 -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 +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- 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 +145 -148
- 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/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +14 -5
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +85 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- 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 +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +49 -25
- package/lib/utils/index.js +2 -2
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -147
- package/lib/cache/inMemory.js +0 -140
- package/lib/cache/index.js +0 -48
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
exports.initEventbus = (runtime) => {
|
|
2
|
-
const { options: brokerOptions, bus, registry, contextFactory } = runtime
|
|
2
|
+
const { options: brokerOptions, bus, registry, contextFactory } = runtime;
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Emit a event on all services (grouped and load balanced).
|
|
@@ -10,60 +10,60 @@ exports.initEventbus = (runtime) => {
|
|
|
10
10
|
*/
|
|
11
11
|
const emit = (eventName, payload, options) => {
|
|
12
12
|
if (Array.isArray(options)) {
|
|
13
|
-
options = { groups: options }
|
|
13
|
+
options = { groups: options };
|
|
14
14
|
} else if (options == null) {
|
|
15
|
-
options = {}
|
|
15
|
+
options = {};
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
// Emit local events
|
|
19
19
|
if (/^\$/.test(eventName)) {
|
|
20
|
-
bus.emit(eventName, payload)
|
|
20
|
+
bus.emit(eventName, payload);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
// todo: create an event context object
|
|
24
|
-
const context = contextFactory.create(null, payload, options)
|
|
24
|
+
const context = contextFactory.create(null, payload, options);
|
|
25
25
|
|
|
26
|
-
context.eventType = 'emit'
|
|
27
|
-
context.eventName = eventName
|
|
28
|
-
context.eventGroups = options.groups
|
|
26
|
+
context.eventType = 'emit';
|
|
27
|
+
context.eventName = eventName;
|
|
28
|
+
context.eventGroups = options.groups;
|
|
29
29
|
|
|
30
|
-
const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups)
|
|
31
|
-
const groupedEndpoints = {}
|
|
32
|
-
const promises = []
|
|
30
|
+
const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups);
|
|
31
|
+
const groupedEndpoints = {};
|
|
32
|
+
const promises = [];
|
|
33
33
|
|
|
34
34
|
endpoints.map(([endpoint, groupName]) => {
|
|
35
35
|
if (endpoint) {
|
|
36
36
|
if (endpoint.node.id === brokerOptions.nodeId) {
|
|
37
37
|
// Local event. Call handler
|
|
38
|
-
context.setEndpoint(endpoint)
|
|
39
|
-
promises.push(endpoint.action.handler(context))
|
|
38
|
+
context.setEndpoint(endpoint);
|
|
39
|
+
promises.push(endpoint.action.handler(context));
|
|
40
40
|
} else {
|
|
41
|
-
const e = groupedEndpoints[endpoint.node.id]
|
|
41
|
+
const e = groupedEndpoints[endpoint.node.id];
|
|
42
42
|
if (e) {
|
|
43
|
-
e.groups.push(groupName)
|
|
43
|
+
e.groups.push(groupName);
|
|
44
44
|
} else {
|
|
45
45
|
groupedEndpoints[endpoint.node.id] = {
|
|
46
46
|
endpoint,
|
|
47
47
|
groups: [groupName]
|
|
48
|
-
}
|
|
48
|
+
};
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
})
|
|
52
|
+
});
|
|
53
53
|
|
|
54
54
|
// Send remote events
|
|
55
55
|
if (runtime.transport) {
|
|
56
56
|
Object.values(groupedEndpoints)
|
|
57
57
|
.forEach(groupedEndpoint => {
|
|
58
|
-
const newContext = context.copy()
|
|
59
|
-
newContext.setEndpoint(groupedEndpoint.endpoint)
|
|
60
|
-
newContext.eventGroups = groupedEndpoint.groups
|
|
61
|
-
promises.push(runtime.transport.sendEvent(newContext))
|
|
62
|
-
})
|
|
58
|
+
const newContext = context.copy();
|
|
59
|
+
newContext.setEndpoint(groupedEndpoint.endpoint);
|
|
60
|
+
newContext.eventGroups = groupedEndpoint.groups;
|
|
61
|
+
promises.push(runtime.transport.sendEvent(newContext));
|
|
62
|
+
});
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
return Promise.all(promises)
|
|
66
|
-
}
|
|
65
|
+
return Promise.all(promises);
|
|
66
|
+
};
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Send a broadcasted event to all local services.
|
|
@@ -75,22 +75,22 @@ exports.initEventbus = (runtime) => {
|
|
|
75
75
|
const broadcastLocal = (eventName, payload, options) => {
|
|
76
76
|
// If the given group is no array - wrap it.
|
|
77
77
|
if (Array.isArray(options)) {
|
|
78
|
-
options = { groups: options }
|
|
78
|
+
options = { groups: options };
|
|
79
79
|
} else if (options == null) {
|
|
80
|
-
options = {}
|
|
80
|
+
options = {};
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
const context = contextFactory.create(null, payload, options)
|
|
84
|
-
context.eventType = 'broadcastLocal'
|
|
85
|
-
context.eventName = eventName
|
|
83
|
+
const context = contextFactory.create(null, payload, options);
|
|
84
|
+
context.eventType = 'broadcastLocal';
|
|
85
|
+
context.eventName = eventName;
|
|
86
86
|
|
|
87
87
|
// Emit the event on the internal event bus
|
|
88
88
|
if (/^\$/.test(eventName)) {
|
|
89
|
-
bus.emit(eventName, payload)
|
|
89
|
+
bus.emit(eventName, payload);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
return registry.eventCollection.emitLocal(context)
|
|
93
|
-
}
|
|
92
|
+
return registry.eventCollection.emitLocal(context);
|
|
93
|
+
};
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
96
|
* Send a broadcasted event to all services.
|
|
@@ -101,38 +101,38 @@ exports.initEventbus = (runtime) => {
|
|
|
101
101
|
*/
|
|
102
102
|
const broadcast = (eventName, payload, options) => {
|
|
103
103
|
if (Array.isArray(options)) {
|
|
104
|
-
options = { groups: options }
|
|
104
|
+
options = { groups: options };
|
|
105
105
|
} else if (options == null) {
|
|
106
|
-
options = {}
|
|
106
|
+
options = {};
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
const promises = []
|
|
109
|
+
const promises = [];
|
|
110
110
|
|
|
111
111
|
if (runtime.transport) {
|
|
112
112
|
// create context
|
|
113
113
|
// todo: create an event context object
|
|
114
|
-
const context = contextFactory.create(null, payload, options)
|
|
115
|
-
context.eventType = 'broadcast'
|
|
116
|
-
context.eventName = eventName
|
|
117
|
-
context.eventGroups = options.groups
|
|
114
|
+
const context = contextFactory.create(null, payload, options);
|
|
115
|
+
context.eventType = 'broadcast';
|
|
116
|
+
context.eventName = eventName;
|
|
117
|
+
context.eventGroups = options.groups;
|
|
118
118
|
|
|
119
119
|
// Avoid to broadcast internal events.
|
|
120
120
|
if (!/^\$/.test(eventName)) {
|
|
121
|
-
const endpoints = registry.eventCollection.getAllEndpointsUniqueNodes(eventName, options.groups)
|
|
121
|
+
const endpoints = registry.eventCollection.getAllEndpointsUniqueNodes(eventName, options.groups);
|
|
122
122
|
|
|
123
123
|
endpoints.map(endpoint => {
|
|
124
124
|
if (endpoint.node.id !== brokerOptions.nodeId) {
|
|
125
|
-
const newContext = context.copy()
|
|
126
|
-
newContext.setEndpoint(endpoint)
|
|
127
|
-
promises.push(runtime.transport.sendEvent(newContext))
|
|
125
|
+
const newContext = context.copy();
|
|
126
|
+
newContext.setEndpoint(endpoint);
|
|
127
|
+
promises.push(runtime.transport.sendEvent(newContext));
|
|
128
128
|
}
|
|
129
|
-
})
|
|
129
|
+
});
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
promises.push(broadcastLocal(eventName, payload, options))
|
|
134
|
-
return Promise.all(promises)
|
|
135
|
-
}
|
|
133
|
+
promises.push(broadcastLocal(eventName, payload, options));
|
|
134
|
+
return Promise.all(promises);
|
|
135
|
+
};
|
|
136
136
|
|
|
137
137
|
Object.defineProperty(runtime, 'eventBus', {
|
|
138
138
|
value: {
|
|
@@ -140,5 +140,5 @@ exports.initEventbus = (runtime) => {
|
|
|
140
140
|
broadcast,
|
|
141
141
|
broadcastLocal
|
|
142
142
|
}
|
|
143
|
-
})
|
|
144
|
-
}
|
|
143
|
+
});
|
|
144
|
+
};
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* @typedef {import('../types.js').Transport} Transport
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { defaultsDeep } = require('@weave-js/utils')
|
|
8
|
-
const { createLogger: createDefaultLogger } = require('../logger/index')
|
|
7
|
+
const { defaultsDeep } = require('@weave-js/utils');
|
|
8
|
+
const { createLogger: createDefaultLogger } = require('../logger/index');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Init logger
|
|
@@ -18,11 +18,11 @@ exports.initLogger = (runtime) => {
|
|
|
18
18
|
nodeId: runtime.options.nodeId,
|
|
19
19
|
moduleName,
|
|
20
20
|
...additional
|
|
21
|
-
}
|
|
21
|
+
};
|
|
22
22
|
|
|
23
23
|
// custom logger generator function.
|
|
24
24
|
if (typeof runtime.options.logger === 'function') {
|
|
25
|
-
return runtime.options.logger(bindings, runtime.options.logger.level)
|
|
25
|
+
return runtime.options.logger(bindings, runtime.options.logger.level);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// merge log options
|
|
@@ -30,18 +30,18 @@ exports.initLogger = (runtime) => {
|
|
|
30
30
|
base: {
|
|
31
31
|
...bindings
|
|
32
32
|
}
|
|
33
|
-
}, runtime.options.logger)
|
|
33
|
+
}, runtime.options.logger);
|
|
34
34
|
|
|
35
|
-
return createDefaultLogger(loggerOptions)
|
|
36
|
-
}
|
|
35
|
+
return createDefaultLogger(loggerOptions);
|
|
36
|
+
};
|
|
37
37
|
|
|
38
|
-
const createLogger = (moduleName, service) => loggerFactory(runtime, moduleName, service)
|
|
38
|
+
const createLogger = (moduleName, service) => loggerFactory(runtime, moduleName, service);
|
|
39
39
|
|
|
40
40
|
// create weave default logger
|
|
41
|
-
const log = createLogger('WEAVE')
|
|
41
|
+
const log = createLogger('WEAVE');
|
|
42
42
|
|
|
43
43
|
Object.assign(runtime, {
|
|
44
44
|
createLogger,
|
|
45
45
|
log
|
|
46
|
-
})
|
|
47
|
-
}
|
|
46
|
+
});
|
|
47
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const { isPlainObject, isFunction } = require('@weave-js/utils')
|
|
2
|
-
const { WeaveError } = require('../errors')
|
|
3
|
-
const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common')
|
|
4
|
-
const MetricTypes = require('../metrics/types')
|
|
1
|
+
const { isPlainObject, isFunction } = require('@weave-js/utils');
|
|
2
|
+
const { WeaveError } = require('../errors');
|
|
3
|
+
const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common');
|
|
4
|
+
const MetricTypes = require('../metrics/types');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @typedef {import('../types.js').Runtime} Runtime
|
|
@@ -13,15 +13,15 @@ const MetricTypes = require('../metrics/types')
|
|
|
13
13
|
* @returns {void}
|
|
14
14
|
*/
|
|
15
15
|
exports.initMetrics = (runtime) => {
|
|
16
|
-
const metricOptions = runtime.options.metrics
|
|
16
|
+
const metricOptions = runtime.options.metrics;
|
|
17
17
|
|
|
18
18
|
if (metricOptions.enabled) {
|
|
19
|
-
const storage = new Map()
|
|
19
|
+
const storage = new Map();
|
|
20
20
|
|
|
21
|
-
const log = runtime.createLogger('METRICS')
|
|
21
|
+
const log = runtime.createLogger('METRICS');
|
|
22
22
|
|
|
23
23
|
/** @type {NodeJS.Timeout} */
|
|
24
|
-
let commonUpdateTimer
|
|
24
|
+
let commonUpdateTimer;
|
|
25
25
|
|
|
26
26
|
Object.defineProperty(runtime, 'metrics', {
|
|
27
27
|
value: {
|
|
@@ -33,101 +33,101 @@ exports.initMetrics = (runtime) => {
|
|
|
33
33
|
// Load adapters
|
|
34
34
|
if (metricOptions.adapters) {
|
|
35
35
|
if (!Array.isArray(metricOptions.adapters)) {
|
|
36
|
-
runtime.handleError(new WeaveError('Metic adapter needs to be an Array.'))
|
|
36
|
+
runtime.handleError(new WeaveError('Metic adapter needs to be an Array.'));
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
this.adapters = metricOptions.adapters.map(adapter => {
|
|
40
|
-
adapter.init(this)
|
|
41
|
-
return adapter
|
|
42
|
-
})
|
|
40
|
+
adapter.init(this);
|
|
41
|
+
return adapter;
|
|
42
|
+
});
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
stop () {
|
|
46
46
|
if (commonUpdateTimer) {
|
|
47
|
-
clearInterval(commonUpdateTimer)
|
|
47
|
+
clearInterval(commonUpdateTimer);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
return Promise.all(this.adapters.map(adapter => adapter.stop()))
|
|
50
|
+
return Promise.all(this.adapters.map(adapter => adapter.stop()));
|
|
51
51
|
},
|
|
52
52
|
register (obj) {
|
|
53
53
|
if (!isPlainObject(obj)) {
|
|
54
|
-
runtime.handleError(new WeaveError('Param needs to be an object.'))
|
|
54
|
+
runtime.handleError(new WeaveError('Param needs to be an object.'));
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (!obj.type) {
|
|
58
|
-
runtime.handleError(new WeaveError('Type is missing.'))
|
|
58
|
+
runtime.handleError(new WeaveError('Type is missing.'));
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (!obj.name) {
|
|
62
|
-
runtime.handleError(new WeaveError('Name is missing.'))
|
|
62
|
+
runtime.handleError(new WeaveError('Name is missing.'));
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const createMetricType = MetricTypes.resolve(obj.type)
|
|
65
|
+
const createMetricType = MetricTypes.resolve(obj.type);
|
|
66
66
|
|
|
67
67
|
if (!createMetricType) {
|
|
68
|
-
runtime.handleError(new WeaveError('Unknown metric type.'))
|
|
68
|
+
runtime.handleError(new WeaveError('Unknown metric type.'));
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
const type = createMetricType(this, obj)
|
|
71
|
+
const type = createMetricType(this, obj);
|
|
72
72
|
|
|
73
|
-
this.storage.set(obj.name, type)
|
|
73
|
+
this.storage.set(obj.name, type);
|
|
74
74
|
|
|
75
|
-
return type
|
|
75
|
+
return type;
|
|
76
76
|
},
|
|
77
77
|
increment (name, labels, value = 1, timestamp) {
|
|
78
78
|
if (!metricOptions.enabled) {
|
|
79
|
-
return null
|
|
79
|
+
return null;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
const item = this.storage.get(name)
|
|
82
|
+
const item = this.storage.get(name);
|
|
83
83
|
|
|
84
84
|
if (!item) {
|
|
85
|
-
runtime.handleError(new WeaveError('Item not found.'))
|
|
85
|
+
runtime.handleError(new WeaveError('Item not found.'));
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
item.increment(labels, value, timestamp)
|
|
88
|
+
item.increment(labels, value, timestamp);
|
|
89
89
|
},
|
|
90
90
|
decrement (name, labels, value = 1, timestamp) {
|
|
91
91
|
if (!metricOptions.enabled) {
|
|
92
|
-
return null
|
|
92
|
+
return null;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
const item = this.storage.get(name)
|
|
95
|
+
const item = this.storage.get(name);
|
|
96
96
|
|
|
97
97
|
if (!item) {
|
|
98
|
-
runtime.handleError(new WeaveError('Item not found.'))
|
|
98
|
+
runtime.handleError(new WeaveError('Item not found.'));
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
item.decrement(labels, value, timestamp)
|
|
101
|
+
item.decrement(labels, value, timestamp);
|
|
102
102
|
},
|
|
103
103
|
set (name, value, labels, timestamp) {
|
|
104
104
|
if (!metricOptions.enabled) {
|
|
105
|
-
return null
|
|
105
|
+
return null;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
const item = this.storage.get(name)
|
|
108
|
+
const item = this.storage.get(name);
|
|
109
109
|
|
|
110
110
|
if (!isFunction(item.set)) {
|
|
111
|
-
runtime.handleError(new WeaveError('Invalid metric type'))
|
|
111
|
+
runtime.handleError(new WeaveError('Invalid metric type'));
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
item.set(value, labels, timestamp)
|
|
114
|
+
item.set(value, labels, timestamp);
|
|
115
115
|
},
|
|
116
116
|
timer (name, labels, timestamp) {
|
|
117
|
-
let item
|
|
117
|
+
let item;
|
|
118
118
|
if (name) {
|
|
119
|
-
item = this.storage.get(name)
|
|
119
|
+
item = this.storage.get(name);
|
|
120
120
|
}
|
|
121
|
-
const start = process.hrtime()
|
|
121
|
+
const start = process.hrtime();
|
|
122
122
|
|
|
123
123
|
return () => {
|
|
124
|
-
const delta = process.hrtime(start)
|
|
125
|
-
const duration = (delta[0] + delta[1] / 1e9) * 1000
|
|
124
|
+
const delta = process.hrtime(start);
|
|
125
|
+
const duration = (delta[0] + delta[1] / 1e9) * 1000;
|
|
126
126
|
if (item) {
|
|
127
|
-
item.set(duration, labels, timestamp)
|
|
127
|
+
item.set(duration, labels, timestamp);
|
|
128
128
|
}
|
|
129
|
-
return duration
|
|
130
|
-
}
|
|
129
|
+
return duration;
|
|
130
|
+
};
|
|
131
131
|
// const item = this.storage.get(name)
|
|
132
132
|
// if (item) {
|
|
133
133
|
|
|
@@ -135,30 +135,30 @@ exports.initMetrics = (runtime) => {
|
|
|
135
135
|
// item.observe(labels, value, timestamp)
|
|
136
136
|
},
|
|
137
137
|
getMetric (name) {
|
|
138
|
-
const item = this.storage.get(name)
|
|
138
|
+
const item = this.storage.get(name);
|
|
139
139
|
|
|
140
140
|
if (!item) {
|
|
141
|
-
runtime.handleError(new WeaveError('Item not found.'))
|
|
141
|
+
runtime.handleError(new WeaveError('Item not found.'));
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
return item
|
|
144
|
+
return item;
|
|
145
145
|
},
|
|
146
146
|
list (/* options = {} */) {
|
|
147
|
-
const results = []
|
|
147
|
+
const results = [];
|
|
148
148
|
|
|
149
149
|
this.storage.forEach(metric => {
|
|
150
|
-
results.push(metric.toObject())
|
|
151
|
-
})
|
|
150
|
+
results.push(metric.toObject());
|
|
151
|
+
});
|
|
152
152
|
|
|
153
|
-
return results
|
|
153
|
+
return results;
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
|
-
})
|
|
156
|
+
});
|
|
157
157
|
|
|
158
158
|
if (metricOptions.enabled && metricOptions.collectCommonMetrics) {
|
|
159
|
-
registerCommonMetrics(runtime)
|
|
160
|
-
commonUpdateTimer = setInterval(() => updateCommonMetrics(runtime), metricOptions.collectInterval)
|
|
161
|
-
commonUpdateTimer.unref()
|
|
159
|
+
registerCommonMetrics(runtime);
|
|
160
|
+
commonUpdateTimer = setInterval(() => updateCommonMetrics(runtime), metricOptions.collectInterval);
|
|
161
|
+
commonUpdateTimer.unref();
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
}
|
|
164
|
+
};
|
|
@@ -1,71 +1,71 @@
|
|
|
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
7
|
exports.initMiddlewareHandler = (runtime) => {
|
|
8
|
-
const list = []
|
|
8
|
+
const list = [];
|
|
9
9
|
|
|
10
10
|
Object.defineProperty(runtime, 'middlewareHandler', {
|
|
11
11
|
value: {
|
|
12
12
|
count () {
|
|
13
|
-
return list.length
|
|
13
|
+
return list.length;
|
|
14
14
|
},
|
|
15
15
|
add (middleware) {
|
|
16
16
|
if (!middleware) {
|
|
17
|
-
return
|
|
17
|
+
return;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
if (typeof middleware === 'function') {
|
|
21
|
-
middleware = middleware.call(runtime, runtime)
|
|
21
|
+
middleware = middleware.call(runtime, runtime);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
list.push(middleware)
|
|
24
|
+
list.push(middleware);
|
|
25
25
|
},
|
|
26
26
|
wrapMethod (methodName, handler, bindTo = runtime) {
|
|
27
27
|
if (list.length) {
|
|
28
|
-
const middlewareList = list.filter(middleware => !!middleware[methodName])
|
|
28
|
+
const middlewareList = list.filter(middleware => !!middleware[methodName]);
|
|
29
29
|
if (middlewareList.length) {
|
|
30
|
-
handler = middlewareList.reduce((next, middleware) => middleware[methodName].call(runtime, next), handler.bind(bindTo))
|
|
30
|
+
handler = middlewareList.reduce((next, middleware) => middleware[methodName].call(runtime, next), handler.bind(bindTo));
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
return handler
|
|
33
|
+
return handler;
|
|
34
34
|
},
|
|
35
35
|
wrapHandler (methodName, handler, definition) {
|
|
36
36
|
if (list.length) {
|
|
37
37
|
handler = list.reduce((handler, middleware) => {
|
|
38
38
|
if (typeof middleware[methodName] === 'function') {
|
|
39
|
-
return middleware[methodName].call(runtime, handler, definition)
|
|
39
|
+
return middleware[methodName].call(runtime, handler, definition);
|
|
40
40
|
} else {
|
|
41
|
-
return handler
|
|
41
|
+
return handler;
|
|
42
42
|
}
|
|
43
|
-
}, handler)
|
|
43
|
+
}, handler);
|
|
44
44
|
}
|
|
45
|
-
return handler
|
|
45
|
+
return handler;
|
|
46
46
|
},
|
|
47
47
|
callHandlersAsync (methodName, args, reverse = false) {
|
|
48
|
-
const middlewareList = reverse ? Array.from(list).reverse() : list
|
|
48
|
+
const middlewareList = reverse ? Array.from(list).reverse() : list;
|
|
49
49
|
const momentousHandlers = middlewareList
|
|
50
50
|
.filter(middleware => typeof middleware[methodName] === 'function')
|
|
51
|
-
.map(middleware => middleware[methodName])
|
|
51
|
+
.map(middleware => middleware[methodName]);
|
|
52
52
|
|
|
53
53
|
if (momentousHandlers.length) {
|
|
54
|
-
return momentousHandlers.reduce((p, func) => p.then(() => func.apply(runtime, args)), Promise.resolve())
|
|
54
|
+
return momentousHandlers.reduce((p, func) => p.then(() => func.apply(runtime, args)), Promise.resolve());
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
return Promise.resolve()
|
|
57
|
+
return Promise.resolve();
|
|
58
58
|
},
|
|
59
59
|
callHandlersSync (methodName, args, reverse = false) {
|
|
60
60
|
if (list.length) {
|
|
61
|
-
const middlewareList = reverse ? Array.from(list).reverse() : list
|
|
61
|
+
const middlewareList = reverse ? Array.from(list).reverse() : list;
|
|
62
62
|
|
|
63
63
|
middlewareList
|
|
64
64
|
.filter(middleware => typeof middleware[methodName] === 'function')
|
|
65
65
|
.map(middleware => middleware[methodName])
|
|
66
|
-
.forEach(handler => handler.apply(runtime, args))
|
|
66
|
+
.forEach(handler => handler.apply(runtime, args));
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
})
|
|
71
|
-
}
|
|
70
|
+
});
|
|
71
|
+
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// @ts-check
|
|
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
8
|
* @typedef {import('../types').Runtime} Runtime
|
|
9
9
|
*/
|
|
10
|
-
const { createRegistry } = require('../registry/registry.js')
|
|
10
|
+
const { createRegistry } = require('../registry/registry.js');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Injects registy in runtime
|
|
@@ -15,11 +15,11 @@ const { createRegistry } = require('../registry/registry.js')
|
|
|
15
15
|
* @returns {void}
|
|
16
16
|
*/
|
|
17
17
|
exports.initRegistry = (runtime) => {
|
|
18
|
-
const registry = createRegistry(runtime)
|
|
18
|
+
const registry = createRegistry(runtime);
|
|
19
19
|
|
|
20
|
-
registry.init(runtime)
|
|
20
|
+
registry.init(runtime);
|
|
21
21
|
|
|
22
22
|
Object.defineProperty(runtime, 'registry', {
|
|
23
23
|
value: registry
|
|
24
|
-
})
|
|
25
|
-
}
|
|
24
|
+
});
|
|
25
|
+
};
|