@weave-js/core 0.9.4 → 0.11.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/lib/ExtendableError.js +33 -0
- package/lib/broker/context.js +25 -13
- package/lib/broker/{default-options.js → defaultOptions.js} +25 -6
- package/lib/broker/index.js +224 -194
- package/lib/buildRuntime.js +70 -0
- package/lib/cache/adapters/base.js +143 -0
- package/lib/cache/adapters/inMemory.js +154 -0
- package/lib/cache/adapters/inMemoryLru.js +140 -0
- package/lib/cache/adapters/index.js +46 -0
- package/lib/cache/base.js +49 -44
- package/lib/cache/lock.js +21 -4
- package/lib/constants.js +8 -5
- package/lib/errorHandler.js +19 -0
- package/lib/errors.js +17 -10
- package/lib/helper/{define-action.js → defineAction.js} +3 -1
- package/lib/helper/{define-broker-options.js → defineBrokerOptions.js} +2 -0
- package/lib/helper/{define-service.js → defineService.js} +4 -1
- package/lib/index.js +17 -92
- package/lib/logger/format/asHumanReadable.js +4 -3
- package/lib/logger/index.js +7 -1
- package/lib/logger/levels.js +8 -7
- package/lib/logger/tools.js +4 -0
- package/lib/logger/utils/format.js +18 -4
- package/lib/metrics/constants.js +8 -6
- package/lib/metrics/types/gauge.js +1 -1
- package/lib/middlewares/action-hooks/index.js +62 -14
- package/lib/middlewares/bulkhead/index.js +5 -4
- package/lib/middlewares/cache/index.js +106 -0
- package/lib/middlewares/circuit-breaker/index.js +41 -16
- package/lib/middlewares/context-tracker/index.js +3 -3
- package/lib/middlewares/error-handler/index.js +3 -3
- package/lib/middlewares/index.js +2 -1
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +2 -2
- package/lib/middlewares/metrics/index.js +2 -2
- package/lib/middlewares/retry/index.js +11 -10
- package/lib/middlewares/timeout/index.js +5 -5
- package/lib/middlewares/tracing/index.js +27 -14
- package/lib/middlewares/tracing/tags.js +43 -0
- package/lib/middlewares/validator/index.js +6 -6
- package/lib/registry/{action-endpoint.js → actionEndpoint.js} +0 -0
- package/lib/registry/collections/{action-collection.js → actionCollection.js} +2 -2
- package/lib/registry/collections/{endpoint-collection.js → endpointCollection.js} +19 -3
- package/lib/registry/collections/{event-collection.js → eventCollection.js} +15 -9
- package/lib/registry/collections/{node-collection.js → nodeCollection.js} +1 -1
- package/lib/registry/collections/{service-collection.js → serviceCollection.js} +2 -2
- package/lib/registry/{event-endpoint.js → eventEndpoint.js} +1 -1
- package/lib/registry/node.js +1 -1
- package/lib/registry/registry.js +19 -38
- package/lib/registry/service/service.js +2 -2
- package/lib/registry/{service-item.js → serviceItem.js} +1 -1
- package/lib/runtime/{init-action-invoker.js → initActionInvoker.js} +3 -3
- package/lib/runtime/initCache.js +19 -0
- package/lib/runtime/{init-context-factory.js → initContextFactory.js} +2 -2
- package/lib/runtime/{init-eventbus.js → initEventbus.js} +11 -10
- package/lib/runtime/{init-logger.js → initLogger.js} +0 -0
- package/lib/runtime/{init-metrics.js → initMetrics.js} +11 -10
- package/lib/runtime/{init-middleware-manager.js → initMiddlewareManager.js} +1 -1
- package/lib/runtime/{init-registry.js → initRegistry.js} +1 -1
- package/lib/runtime/{init-service-manager.js → initServiceManager.js} +0 -0
- package/lib/runtime/{init-tracing.js → initTracing.js} +2 -1
- package/lib/runtime/{init-transport.js → initTransport.js} +0 -0
- package/lib/runtime/{init-uuid-factory.js → initUuidFactory.js} +0 -0
- package/lib/runtime/{init-validator.js → initValidator.js} +1 -1
- package/lib/tracing/collectors/base.js +11 -19
- package/lib/tracing/collectors/event.js +2 -2
- package/lib/tracing/collectors/index.js +2 -2
- package/lib/tracing/span.js +8 -6
- package/lib/transport/InboundTransformStream.js +75 -0
- package/lib/transport/adapters/{adapter-base.js → adapteBase.js} +16 -1
- package/lib/transport/adapters/adapters.js +1 -1
- package/lib/transport/adapters/dummy/index.js +3 -3
- package/lib/transport/adapters/index.js +1 -1
- package/lib/transport/adapters/tcp/discovery/index.js +45 -31
- package/lib/transport/adapters/tcp/index.js +20 -13
- package/lib/transport/adapters/tcp/tcpReader.js +1 -0
- package/lib/transport/adapters/tcp/tcpWriteStream.js +1 -1
- package/lib/transport/adapters/tcp/tcpWriter.js +2 -2
- package/lib/transport/createMessage.js +11 -0
- package/lib/transport/createTransport.js +215 -172
- package/lib/transport/errorPayloadFactory.js +18 -0
- package/lib/transport/{message-handlers.js → messageHandlers.js} +92 -23
- package/lib/transport/messageTypes.js +23 -0
- package/lib/types.js +86 -75
- package/lib/utils/index.js +1 -1
- package/lib/utils/options.js +4 -2
- package/lib/utils/{restore-error.js → restoreError.js} +1 -1
- package/package.json +6 -12
- package/CHANGELOG.md +0 -1317
- package/lib/cache/index.js +0 -48
- package/lib/cache/memory.js +0 -91
- package/lib/middlewares/tracing/buildTags.js +0 -17
- package/lib/new-errors.js +0 -25
- package/lib/runtime/init-cache.js +0 -10
- 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/message-types.js +0 -59
- package/lib/transport/message.js +0 -16
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/*
|
|
4
4
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
5
5
|
* -----
|
|
6
|
-
* Copyright
|
|
6
|
+
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
const { omit, match } = require('@weave-js/utils')
|
|
18
|
-
const { createEndpointList } = require('./
|
|
18
|
+
const { createEndpointList } = require('./endpointCollection')
|
|
19
19
|
|
|
20
20
|
const broadcastEvents = ['broadcast', 'localBroadcast']
|
|
21
21
|
/**
|
|
@@ -51,7 +51,7 @@ exports.createEventCollection = (registry) => {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
eventCollection.remove = (node, eventName) => {
|
|
54
|
-
events.map(list => {
|
|
54
|
+
events.map((list) => {
|
|
55
55
|
if (list.name === eventName) {
|
|
56
56
|
list.removeByNodeId(node.id)
|
|
57
57
|
}
|
|
@@ -59,17 +59,23 @@ exports.createEventCollection = (registry) => {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
eventCollection.removeByService = (service) => {
|
|
62
|
-
events.map(list => {
|
|
62
|
+
events.map((list) => {
|
|
63
63
|
list.removeByService(service)
|
|
64
64
|
})
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
eventCollection.getBalancedEndpoints = (eventName, groups) => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
.
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
const result = []
|
|
69
|
+
getAllEventsByEventName(eventName)
|
|
70
|
+
.forEach((endpointList) => {
|
|
71
|
+
if (groups == null || groups.length === 0 || groups.indexOf(endpointList.groupName) !== -1) {
|
|
72
|
+
const endpoint = endpointList.getNextAvailableEndpoint()
|
|
73
|
+
if (endpoint && endpoint.isAvailable()) {
|
|
74
|
+
result.push([endpoint, endpointList.groupName])
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
return result
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
eventCollection.getAllEndpoints = (eventName) => {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/*
|
|
4
4
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
5
5
|
* -----
|
|
6
|
-
* Copyright
|
|
6
|
+
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
// const { createEndpointCollection } = require('./endpoint-collection')
|
|
15
15
|
const { omit, remove } = require('@weave-js/utils')
|
|
16
|
-
const { createServiceItem } = require('../
|
|
16
|
+
const { createServiceItem } = require('../serviceItem')
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Service collection factory
|
package/lib/registry/node.js
CHANGED
package/lib/registry/registry.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/*
|
|
3
3
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
4
4
|
* -----
|
|
5
|
-
* Copyright
|
|
5
|
+
* Copyright 2021 Fachwerk
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
const { safeCopy } = require('@weave-js/utils')
|
|
22
22
|
|
|
23
23
|
// own packages
|
|
24
|
-
const { createNodeCollection } = require('./collections/
|
|
25
|
-
const { createServiceCollection } = require('./collections/
|
|
26
|
-
const { createActionCollection } = require('./collections/
|
|
27
|
-
const { createEventCollection } = require('./collections/
|
|
28
|
-
const { createActionEndpoint } = require('./
|
|
24
|
+
const { createNodeCollection } = require('./collections/nodeCollection')
|
|
25
|
+
const { createServiceCollection } = require('./collections/serviceCollection')
|
|
26
|
+
const { createActionCollection } = require('./collections/actionCollection')
|
|
27
|
+
const { createEventCollection } = require('./collections/eventCollection')
|
|
28
|
+
const { createActionEndpoint } = require('./actionEndpoint')
|
|
29
29
|
const { createNode } = require('./node')
|
|
30
30
|
const { WeaveServiceNotFoundError, WeaveServiceNotAvailableError } = require('../errors')
|
|
31
31
|
|
|
@@ -47,24 +47,12 @@ exports.createRegistry = (runtime) => {
|
|
|
47
47
|
log: runtime.createLogger('REGISTRY'),
|
|
48
48
|
/**
|
|
49
49
|
* Initialize the registry
|
|
50
|
-
* @param {
|
|
50
|
+
* @param {Runtime} runtime Runtime
|
|
51
51
|
* @param {MiddlewareHandler} middlewareHandler Middleware handler
|
|
52
52
|
* @param {ServiceChangedDelegate} serviceChanged Service changed delegate
|
|
53
53
|
* @returns {void}
|
|
54
54
|
*/
|
|
55
55
|
init (runtime) {
|
|
56
|
-
/**
|
|
57
|
-
* @type {Broker}
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @type {MiddlewareHandler}
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* @type {ServiceChangedDelegate}
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
56
|
// init collections
|
|
69
57
|
this.nodeCollection = createNodeCollection(this)
|
|
70
58
|
this.serviceCollection = createServiceCollection(this)
|
|
@@ -141,7 +129,6 @@ exports.createRegistry = (runtime) => {
|
|
|
141
129
|
}
|
|
142
130
|
|
|
143
131
|
if (oldActions) {
|
|
144
|
-
// this.deregisterAction()
|
|
145
132
|
Object.keys(oldActions).forEach(actionName => {
|
|
146
133
|
if (!service.actions[actionName]) {
|
|
147
134
|
this.actionCollection.remove(actionName, node)
|
|
@@ -160,19 +147,22 @@ exports.createRegistry = (runtime) => {
|
|
|
160
147
|
|
|
161
148
|
// remove old services
|
|
162
149
|
const oldServices = Array.from(this.serviceCollection.services)
|
|
163
|
-
oldServices.forEach((
|
|
164
|
-
if (
|
|
150
|
+
oldServices.forEach((oldService) => {
|
|
151
|
+
if (oldService.node.id !== node.id) {
|
|
152
|
+
return
|
|
153
|
+
}
|
|
165
154
|
|
|
166
155
|
let isExisting = false
|
|
167
156
|
|
|
157
|
+
// check if the old service exists in the new services.
|
|
168
158
|
services.forEach((svc) => {
|
|
169
|
-
if (
|
|
159
|
+
if (oldService.equals(svc.name, svc.version)) {
|
|
170
160
|
isExisting = true
|
|
171
161
|
}
|
|
172
162
|
})
|
|
173
163
|
|
|
174
164
|
if (!isExisting) {
|
|
175
|
-
this.deregisterService(
|
|
165
|
+
this.deregisterService(oldService.name, oldService.version, node.id)
|
|
176
166
|
}
|
|
177
167
|
})
|
|
178
168
|
|
|
@@ -199,9 +189,9 @@ exports.createRegistry = (runtime) => {
|
|
|
199
189
|
}
|
|
200
190
|
|
|
201
191
|
if (node.isLocal) {
|
|
202
|
-
action.handler = middlewareHandler.wrapHandler('localAction', action.handler, action)
|
|
192
|
+
action.handler = middlewareHandler.wrapHandler('localAction', action.handler, action)
|
|
203
193
|
} else {
|
|
204
|
-
action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.
|
|
194
|
+
action.handler = middlewareHandler.wrapHandler('remoteAction', runtime.transport.sendRequest.bind(runtime.transport), action)
|
|
205
195
|
}
|
|
206
196
|
|
|
207
197
|
this.actionCollection.add(node, service, action)
|
|
@@ -209,13 +199,10 @@ exports.createRegistry = (runtime) => {
|
|
|
209
199
|
service.addAction(action)
|
|
210
200
|
})
|
|
211
201
|
},
|
|
212
|
-
getActionList (options) {
|
|
213
|
-
return this.actionCollection.list(options)
|
|
214
|
-
},
|
|
215
202
|
deregisterService (name, version, nodeId) {
|
|
216
203
|
this.serviceCollection.remove(nodeId || runtime.nodeId, name, version)
|
|
217
204
|
|
|
218
|
-
// It must be a local service
|
|
205
|
+
// It must be a local service if there is no node ID.
|
|
219
206
|
if (!nodeId) {
|
|
220
207
|
const serviceToRemove = this.nodeCollection.localNode.services.find(service => service.name === name)
|
|
221
208
|
this.nodeCollection.localNode.services.splice(this.nodeCollection.localNode.services.indexOf(serviceToRemove), 1)
|
|
@@ -369,9 +356,9 @@ exports.createRegistry = (runtime) => {
|
|
|
369
356
|
}
|
|
370
357
|
|
|
371
358
|
// todo: Handle multiple nodes with the same ID.
|
|
372
|
-
const
|
|
359
|
+
const updateNecessary = node.update(payload, isReconnected)
|
|
373
360
|
|
|
374
|
-
if (
|
|
361
|
+
if (updateNecessary && node.services) {
|
|
375
362
|
this.registerRemoteServices(node, node.services)
|
|
376
363
|
}
|
|
377
364
|
|
|
@@ -399,12 +386,6 @@ exports.createRegistry = (runtime) => {
|
|
|
399
386
|
this.nodeCollection.remove(nodeId)
|
|
400
387
|
runtime.eventBus.broadcastLocal('$node.removed', { nodeId })
|
|
401
388
|
this.log.warn(`Node "${nodeId}" removed.`)
|
|
402
|
-
},
|
|
403
|
-
getNodeList (options) {
|
|
404
|
-
return this.nodeCollection.list(options)
|
|
405
|
-
},
|
|
406
|
-
getServiceList (options) {
|
|
407
|
-
return this.serviceCollection.list(options)
|
|
408
389
|
}
|
|
409
390
|
}
|
|
410
391
|
|
|
@@ -9,7 +9,7 @@ const { WeaveError } = require('../../errors')
|
|
|
9
9
|
const { parseAction } = require('./parseAction')
|
|
10
10
|
const { parseEvent } = require('./parseEvent')
|
|
11
11
|
const { reduceMixins } = require('./reduceMixins')
|
|
12
|
-
const { createEventEndpoint } = require('../
|
|
12
|
+
const { createEventEndpoint } = require('../eventEndpoint')
|
|
13
13
|
/**
|
|
14
14
|
* Service factory
|
|
15
15
|
* @param {Runtime} runtime Broker instance
|
|
@@ -133,7 +133,7 @@ exports.createServiceFromSchema = (runtime, schema) => {
|
|
|
133
133
|
context = runtime.contextFactory.create(endpoint, data, options || {})
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
return wrappedAction(context)
|
|
136
|
+
return wrappedAction(context, { service, runtime, errors: {}})
|
|
137
137
|
}
|
|
138
138
|
})
|
|
139
139
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const errors = require('../errors')
|
|
2
2
|
|
|
3
3
|
exports.initActionInvoker = (runtime) => {
|
|
4
4
|
const { registry, contextFactory, log, handleError } = runtime
|
|
@@ -36,7 +36,7 @@ exports.initActionInvoker = (runtime) => {
|
|
|
36
36
|
log.debug({ action: context.action.name, nodeId, requestId: context.requestId }, 'Call action on remote node.')
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const p = action.handler(context,
|
|
39
|
+
const p = action.handler(context, { service: context.action.service, runtime, errors })
|
|
40
40
|
p.context = context
|
|
41
41
|
|
|
42
42
|
return p
|
|
@@ -51,7 +51,7 @@ exports.initActionInvoker = (runtime) => {
|
|
|
51
51
|
if (Array.isArray(actions)) {
|
|
52
52
|
return Promise.all(actions.map(item => call(item.actionName, item.params, item.options)))
|
|
53
53
|
} else {
|
|
54
|
-
return Promise.reject(new WeaveError('Actions need to be an Array'))
|
|
54
|
+
return Promise.reject(new errors.WeaveError('Actions need to be an Array'))
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const { isFunction } = require('@weave-js/utils')
|
|
2
|
+
const { WeaveError } = require('../errors')
|
|
3
|
+
|
|
4
|
+
exports.initCache = (runtime) => {
|
|
5
|
+
if (runtime.options.cache && runtime.options.cache.enabled) {
|
|
6
|
+
if (!isFunction(runtime.options.cache.adapter)) {
|
|
7
|
+
throw new WeaveError('Invalid cache adapter.')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Init cache adapter
|
|
11
|
+
const cache = runtime.options.cache.adapter(runtime, runtime.options.cache)
|
|
12
|
+
|
|
13
|
+
runtime.log.info(`Cache: ${cache.name}`)
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(runtime, 'cache', {
|
|
16
|
+
value: cache
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Author: Kevin Ries (kevin@fachw3rk.de)
|
|
3
3
|
* -----
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
'use strict'
|
|
7
7
|
|
|
@@ -12,7 +12,7 @@ const { createContext } = require('../broker/context')
|
|
|
12
12
|
* @typedef {import('../types').Context} Context
|
|
13
13
|
* @typedef {import('../types').Endpoint} Endpoint
|
|
14
14
|
* @typedef {import('../types').ActionOptions} ActionOptions
|
|
15
|
-
|
|
15
|
+
*/
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Init context factory.
|
|
@@ -15,7 +15,11 @@ exports.initEventbus = (runtime) => {
|
|
|
15
15
|
options = {}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
// Emit local events
|
|
19
|
+
if (/^\$/.test(eventName)) {
|
|
20
|
+
bus.emit(eventName, payload)
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
// todo: create an event context object
|
|
20
24
|
const context = contextFactory.create(null, payload, options)
|
|
21
25
|
|
|
@@ -23,23 +27,20 @@ exports.initEventbus = (runtime) => {
|
|
|
23
27
|
context.eventName = eventName
|
|
24
28
|
context.eventGroups = options.groups
|
|
25
29
|
|
|
26
|
-
// Emit system events
|
|
27
|
-
if (/^\$/.test(eventName)) {
|
|
28
|
-
bus.emit(eventName, payload)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
30
|
const endpoints = registry.eventCollection.getBalancedEndpoints(eventName, options.groups)
|
|
32
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)
|
|
38
39
|
promises.push(endpoint.action.handler(context))
|
|
39
40
|
} else {
|
|
40
41
|
const e = groupedEndpoints[endpoint.node.id]
|
|
41
42
|
if (e) {
|
|
42
|
-
e.push(groupName)
|
|
43
|
+
e.groups.push(groupName)
|
|
43
44
|
} else {
|
|
44
45
|
groupedEndpoints[endpoint.node.id] = {
|
|
45
46
|
endpoint,
|
|
@@ -50,7 +51,7 @@ exports.initEventbus = (runtime) => {
|
|
|
50
51
|
}
|
|
51
52
|
})
|
|
52
53
|
|
|
53
|
-
//
|
|
54
|
+
// Send remote events
|
|
54
55
|
if (runtime.transport) {
|
|
55
56
|
Object.values(groupedEndpoints)
|
|
56
57
|
.forEach(groupedEndpoint => {
|
|
@@ -69,7 +70,7 @@ exports.initEventbus = (runtime) => {
|
|
|
69
70
|
* @param {String} eventName Name of the event
|
|
70
71
|
* @param {any} payload Payload
|
|
71
72
|
* @param {*} [options=null] Options
|
|
72
|
-
* @returns {
|
|
73
|
+
* @returns {Promise<any>} Promise
|
|
73
74
|
*/
|
|
74
75
|
const broadcastLocal = (eventName, payload, options) => {
|
|
75
76
|
// If the given group is no array - wrap it.
|
|
@@ -96,7 +97,7 @@ exports.initEventbus = (runtime) => {
|
|
|
96
97
|
* @param {String} eventName Name of the event
|
|
97
98
|
* @param {any} payload Payload
|
|
98
99
|
* @param {*} [options=null] Groups
|
|
99
|
-
* @returns {
|
|
100
|
+
* @returns {Promise<any>} Promise
|
|
100
101
|
*/
|
|
101
102
|
const broadcast = (eventName, payload, options) => {
|
|
102
103
|
if (Array.isArray(options)) {
|
|
File without changes
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { isPlainObject, isFunction } = require('@weave-js/utils')
|
|
2
|
+
const { WeaveError } = require('../errors')
|
|
2
3
|
const { registerCommonMetrics, updateCommonMetrics } = require('../metrics/common')
|
|
3
4
|
const MetricTypes = require('../metrics/types')
|
|
4
5
|
|
|
@@ -32,7 +33,7 @@ exports.initMetrics = (runtime) => {
|
|
|
32
33
|
// Load adapters
|
|
33
34
|
if (metricOptions.adapters) {
|
|
34
35
|
if (!Array.isArray(metricOptions.adapters)) {
|
|
35
|
-
runtime.handleError(new
|
|
36
|
+
runtime.handleError(new WeaveError('Metic adapter needs to be an Array.'))
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
this.adapters = metricOptions.adapters.map(adapter => {
|
|
@@ -50,21 +51,21 @@ exports.initMetrics = (runtime) => {
|
|
|
50
51
|
},
|
|
51
52
|
register (obj) {
|
|
52
53
|
if (!isPlainObject(obj)) {
|
|
53
|
-
runtime.handleError(new
|
|
54
|
+
runtime.handleError(new WeaveError('Param needs to be an object.'))
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
if (!obj.type) {
|
|
57
|
-
runtime.handleError(new
|
|
58
|
+
runtime.handleError(new WeaveError('Type is missing.'))
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
if (!obj.name) {
|
|
61
|
-
runtime.handleError(new
|
|
62
|
+
runtime.handleError(new WeaveError('Name is missing.'))
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
const createMetricType = MetricTypes.resolve(obj.type)
|
|
65
66
|
|
|
66
67
|
if (!createMetricType) {
|
|
67
|
-
runtime.handleError(new
|
|
68
|
+
runtime.handleError(new WeaveError('Unknown metric type.'))
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
const type = createMetricType(this, obj)
|
|
@@ -81,7 +82,7 @@ exports.initMetrics = (runtime) => {
|
|
|
81
82
|
const item = this.storage.get(name)
|
|
82
83
|
|
|
83
84
|
if (!item) {
|
|
84
|
-
runtime.handleError(new
|
|
85
|
+
runtime.handleError(new WeaveError('Item not found.'))
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
item.increment(labels, value, timestamp)
|
|
@@ -94,7 +95,7 @@ exports.initMetrics = (runtime) => {
|
|
|
94
95
|
const item = this.storage.get(name)
|
|
95
96
|
|
|
96
97
|
if (!item) {
|
|
97
|
-
runtime.handleError(new
|
|
98
|
+
runtime.handleError(new WeaveError('Item not found.'))
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
item.decrement(labels, value, timestamp)
|
|
@@ -107,7 +108,7 @@ exports.initMetrics = (runtime) => {
|
|
|
107
108
|
const item = this.storage.get(name)
|
|
108
109
|
|
|
109
110
|
if (!isFunction(item.set)) {
|
|
110
|
-
runtime.handleError(new
|
|
111
|
+
runtime.handleError(new WeaveError('Invalid metric type'))
|
|
111
112
|
}
|
|
112
113
|
|
|
113
114
|
item.set(value, labels, timestamp)
|
|
@@ -137,12 +138,12 @@ exports.initMetrics = (runtime) => {
|
|
|
137
138
|
const item = this.storage.get(name)
|
|
138
139
|
|
|
139
140
|
if (!item) {
|
|
140
|
-
runtime.handleError(new
|
|
141
|
+
runtime.handleError(new WeaveError('Item not found.'))
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
return item
|
|
144
145
|
},
|
|
145
|
-
list (options = {}) {
|
|
146
|
+
list (/* options = {} */) {
|
|
146
147
|
const results = []
|
|
147
148
|
|
|
148
149
|
this.storage.forEach(metric => {
|
|
File without changes
|
|
@@ -9,6 +9,8 @@ exports.initTracer = (runtime) => {
|
|
|
9
9
|
|
|
10
10
|
Object.defineProperty(runtime, 'tracer', {
|
|
11
11
|
value: {
|
|
12
|
+
runtime,
|
|
13
|
+
options,
|
|
12
14
|
log,
|
|
13
15
|
async stop () {
|
|
14
16
|
if (collectors.length > 0) {
|
|
@@ -55,7 +57,6 @@ exports.initTracer = (runtime) => {
|
|
|
55
57
|
collectors = options.collectors
|
|
56
58
|
.map(entry => {
|
|
57
59
|
const initCollector = resolveCollector(runtime, entry, this)
|
|
58
|
-
// collector.init(this)
|
|
59
60
|
initCollector.init(runtime)
|
|
60
61
|
return initCollector
|
|
61
62
|
})
|
|
File without changes
|
|
File without changes
|
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
const { isObject } = require('@weave-js/utils')
|
|
2
|
-
const { WeaveError } = require('../../errors')
|
|
1
|
+
const { isObject, pick } = require('@weave-js/utils')
|
|
3
2
|
|
|
4
|
-
exports.createBaseTracingCollector = (
|
|
3
|
+
exports.createBaseTracingCollector = (runtime) => {
|
|
5
4
|
const baseTracingCollector = Object.create(null)
|
|
6
5
|
|
|
7
|
-
baseTracingCollector.options = options
|
|
6
|
+
baseTracingCollector.options = runtime.tracer.options
|
|
8
7
|
|
|
9
|
-
baseTracingCollector.init = (runtime
|
|
8
|
+
baseTracingCollector.init = (runtime) => {
|
|
10
9
|
baseTracingCollector.runtime = runtime
|
|
11
|
-
baseTracingCollector.tracer = tracer
|
|
10
|
+
baseTracingCollector.tracer = runtime.tracer
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
baseTracingCollector.startedSpan = () => {
|
|
15
|
-
throw new WeaveError('not implemented.')
|
|
14
|
+
// throw new WeaveError('not implemented.')
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
baseTracingCollector.finishedSpan = () => {
|
|
19
|
-
throw new WeaveError('not implemented.')
|
|
18
|
+
// throw new WeaveError('not implemented.')
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
baseTracingCollector.stop = () => {
|
|
23
|
-
throw new WeaveError('not implemented.')
|
|
22
|
+
// throw new WeaveError('not implemented.')
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
baseTracingCollector.flattenTags = (obj, convertToString = false, path = '') => {
|
|
@@ -42,18 +41,11 @@ exports.createBaseTracingCollector = (options) => {
|
|
|
42
41
|
}, {})
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
baseTracingCollector.getErrorFields = (
|
|
46
|
-
if (!
|
|
44
|
+
baseTracingCollector.getErrorFields = (err, fields) => {
|
|
45
|
+
if (!err) {
|
|
47
46
|
return null
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
const picked = {}
|
|
51
|
-
|
|
52
|
-
for (const field of fields) {
|
|
53
|
-
picked[field] = error[field]
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return picked
|
|
48
|
+
return pick(err, fields)
|
|
57
49
|
}
|
|
58
50
|
|
|
59
51
|
return baseTracingCollector
|
|
@@ -13,7 +13,7 @@ const mergeDefaultOptions = (options) => {
|
|
|
13
13
|
module.exports = (options) => (runtime, tracer) => {
|
|
14
14
|
options = mergeDefaultOptions(options)
|
|
15
15
|
|
|
16
|
-
const exporter = createBaseTracingCollector(
|
|
16
|
+
const exporter = createBaseTracingCollector(runtime)
|
|
17
17
|
|
|
18
18
|
exporter.init(runtime, tracer)
|
|
19
19
|
|
|
@@ -28,7 +28,7 @@ module.exports = (options) => (runtime, tracer) => {
|
|
|
28
28
|
const newSpan = Object.assign({}, span)
|
|
29
29
|
|
|
30
30
|
if (newSpan.error) {
|
|
31
|
-
newSpan.error = exporter.getErrorFields(newSpan.error)
|
|
31
|
+
newSpan.error = exporter.getErrorFields(newSpan.error, exporter.options.errors.fields)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
return newSpan
|
|
@@ -11,14 +11,14 @@ const getByName = name => {
|
|
|
11
11
|
return collectors[n]
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
exports.resolveCollector = (runtime, collector
|
|
14
|
+
exports.resolveCollector = (runtime, collector) => {
|
|
15
15
|
let CollectorClass
|
|
16
16
|
if (typeof collector === 'string') {
|
|
17
17
|
CollectorClass = getByName(collector)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
if (isFunction(collector)) {
|
|
21
|
-
return collector(runtime
|
|
21
|
+
return collector(runtime)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
if (typeof collector === 'object') {
|
package/lib/tracing/span.js
CHANGED
|
@@ -3,8 +3,8 @@ const hrTime = require('./time')
|
|
|
3
3
|
exports.createSpan = (tracer, name, options) => {
|
|
4
4
|
const span = Object.assign({}, {
|
|
5
5
|
name,
|
|
6
|
-
id: options.id || tracer.
|
|
7
|
-
traceId: options.traceId || tracer.
|
|
6
|
+
id: options.id || tracer.runtime.generateUUID(),
|
|
7
|
+
traceId: options.traceId || tracer.runtime.generateUUID(),
|
|
8
8
|
parentId: options.parentId,
|
|
9
9
|
type: options.type,
|
|
10
10
|
sampled: options.sampled || tracer.shouldSample(),
|
|
@@ -12,10 +12,12 @@ exports.createSpan = (tracer, name, options) => {
|
|
|
12
12
|
tags: {}
|
|
13
13
|
})
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
if (options.service) {
|
|
16
|
+
span.service = {
|
|
17
|
+
name: options.service.name,
|
|
18
|
+
version: options.service.version,
|
|
19
|
+
fullyQualifiedName: options.service.fullyQualifiedName
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
span.addTags = (tags) => {
|