This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
forked from speedymonster/node-grpc-interceptors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-zipkin-interceptor.js
59 lines (47 loc) · 1.83 KB
/
client-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const grpc = require('@grpc/grpc-js');
const { Instrumentation } = require('zipkin');
const zipkinInterceptor = function (options, nextCall) {
const tracer = this; // 'this' is bound to the tracer created in the module exports
const components = options.method_definition.path.split('/');
const remoteMethodName = components[2] || 'unknown';
const instrumentation = new Instrumentation.HttpClient({ tracer });
return new grpc.InterceptingCall(nextCall(options), {
start: function (metadata, listener, next) {
// add zipkin trace data to request metadata
const { headers } = instrumentation.recordRequest(
{},
options.method_definition.path,
remoteMethodName,
);
for(const k in headers) {
metadata.add(k, headers[k]);
}
next(metadata, {
onReceiveMetadata: function (metadata, next) {
next(metadata);
},
onReceiveMessage: function (message, next) {
next(message);
},
onReceiveStatus: function (status, next) {
if (status.code !== grpc.status.OK) {
instrumentation.recordError(tracer.id, status.details);
} else {
instrumentation.recordResponse(tracer.id, status.code);
}
next(status);
},
});
},
sendMessage: function (message, next) {
next(message);
},
halfClose: function (next) {
next();
},
});
};
module.exports = localServiceName => {
const tracer = require('../zipkin-tracer')(localServiceName);
return zipkinInterceptor.bind(tracer);
};