forked from echo-health/node-grpc-interceptors
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server-zipkin-interceptor.js
44 lines (34 loc) · 1.09 KB
/
server-zipkin-interceptor.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
const { option, Instrumentation } = require('zipkin');
const zipkinInterceptor = async function (ctx, next) {
const tracer = this; // 'this' is bound to the tracer created in the module exports
const serviceName = tracer._localEndpoint.serviceName || 'unknown';
const port = process.env.PORT || 0;
const instrumentation = new Instrumentation.HttpServer({
tracer,
serviceName,
port,
});
function readHeader(header) {
const val = ctx.call.metadata.get(header);
if (val.length) {
return new option.Some(val[0]);
}
return option.None;
}
const id = instrumentation.recordRequest(
ctx.service.method,
ctx.service.path,
readHeader
);
try {
await next();
} catch(err) {
instrumentation.recordResponse(id, ctx.status.code, err);
throw err;
}
instrumentation.recordResponse(id, ctx.status.code);
};
module.exports = localServiceName => {
const tracer = require('../zipkin-tracer')(localServiceName);
return zipkinInterceptor.bind(tracer);
};