-
Notifications
You must be signed in to change notification settings - Fork 63
/
CustomRequester.js
69 lines (58 loc) · 2.19 KB
/
CustomRequester.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const Cote = require('cote')
const apm = require('elastic-apm-node')
const { isActive: isApmActive } = require('../../apm')
const { logError } = require('../../logger')
const { getListPermissions } = require('../permissions')
class CustomRequester extends Cote.Requester {
// Inherit send method to log the processing duration
async send (...args) {
const sendParams = args[0] || {}
const name = `Requester send: ${this.advertisement.name} | type: ${sendParams.type}`
const apmSpan = apm.startSpan(name)
// used to link to the source APM transaction across network (see custom responder)
if (apm.currentTransaction) {
sendParams._apmTraceparent = apm.currentTransaction.traceparent
}
try {
const result = await super.send(...args)
return result
} finally {
if (isApmActive && !apmSpan) {
if (!apm.currentTransaction) {
logError(new Error(`No APM transaction available in requester "${name}"`))
} else {
logError(new Error(`Empty apm span in requester "${name}"`))
}
}
// check the existence of apm span just in case (should always be defined)
apmSpan && apmSpan.end()
}
}
communicate (req = {}) {
const requestContext = getRequestContext(req)
return (requestParams) => {
return this.send(Object.assign({}, requestContext, requestParams))
}
}
}
function getRequestContext (req) {
return {
_readNamespaces: ['*'],
_editNamespaces: ['*'],
_requestId: req._requestId,
platformId: req.platformId,
env: req.env,
_systemHash: req._systemHash,
// Granting all existing permissions to ease maintenance
// Stelace services can communicate with each other thanks to `customRequester.communicate()`.
// However, even if the request is considered as a system request,
// some permissions may protect service logic.
// By automatically granting all permissions, should the permissions needed by a service change,
// there will be no need to update permissions at every location calling this service.
_matchedPermissions: getListPermissions().reduce((o, p) => {
o[p] = true
return o
}, {})
}
}
module.exports = CustomRequester