-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.js
63 lines (58 loc) · 1.99 KB
/
func.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
const {
Annotation,
option: {Some, None},
TraceId
} = require('zipkin')
module.exports = (to_wrap, options) => {
return (params) => {
const { tracer, serviceName } = options
return new Promise((resolve, reject) => {
tracer.scoped(() => {
if (params.hasOwnProperty('_zipkin')) {
const traceId = new Some(params._zipkin.traceId);
const spanId = new Some(params._zipkin.spanId);
const parentId = new Some(params._zipkin.parentId);
tracer.setId(new TraceId({
traceId, spanId: spanId.value, parentId
}))
} else {
tracer.setId(tracer.createRootId());
}
const traceId = tracer.id;
tracer.recordServiceName(serviceName);
tracer.recordRpc('POST');
tracer.recordBinary('openwhisk.id', process.env['__OW_ACTION_NAME']);
tracer.recordBinary('openwhisk.activation', process.env['__OW_ACTIVATION_ID']);
tracer.recordAnnotation(new Annotation.ServerRecv());
const cb = resolver => {
return result => {
tracer.scoped(() => {
tracer.setId(traceId);
tracer.recordAnnotation(new Annotation.ServerSend());
// Timer loop in zipkin transporter does not stop process exiting.
// We need to wait until all spans are recorded before returning.
if (tracer.recorder.logger.queue) {
const queueMonitor = setInterval(() => {
if (!tracer.recorder.logger.queue.length) {
clearInterval(queueMonitor);
resolver(result);
}
}, 100)
} else {
resolver(result)
}
})
}
}
try {
const promiseOrResult = to_wrap(params)
Promise.resolve(promiseOrResult)
.then(cb(resolve))
.catch(cb(reject))
} catch (err) {
cb(reject)(err)
}
})
})
}
}