This repository was archived by the owner on Mar 19, 2022. It is now read-only.
forked from openzipkin/zipkin-js
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZipkinGrpcInterceptor.js
More file actions
103 lines (80 loc) · 3.19 KB
/
Copy pathZipkinGrpcInterceptor.js
File metadata and controls
103 lines (80 loc) · 3.19 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const grpc = require('grpc');
const {
TraceId,
Annotation,
option: {Some}
} = require('zipkin');
class ZipkinGrpcInterceptor {
constructor(tracer) {
this.tracer = tracer;
}
// Call this right before the GRPC client is ready to call the GRPC server service.
beforeClientDoGrpcCall({serviceName = 'unknown', remoteGrpcServiceName = 'unknown',
grpcMetadata}) {
return this.tracer.scoped(() => {
let metadata;
if (grpcMetadata && grpcMetadata instanceof grpc.Metadata) {
metadata = grpcMetadata;
} else {
metadata = new grpc.Metadata();
}
this.tracer.setId(this.tracer.createChildId());
this.traceId = this.tracer.id;
metadata.add('X-B3-TraceId', this.tracer.id.traceId);
metadata.add('X-B3-ParentSpanId', this.tracer.id.parentId);
metadata.add('X-B3-SpanId', this.tracer.id.spanId);
metadata.add('X-B3-Sampled', this.tracer.id.sampled.getOrElse() ? '1' : '0');
this.tracer.recordServiceName(serviceName);
this.tracer.recordRpc(remoteGrpcServiceName);
this.tracer.recordAnnotation(new Annotation.ClientSend());
this.tracer.recordAnnotation(new Annotation.LocalAddr({}));
return metadata;
});
}
// Call this at the first line upon the GRPC server received the call from the GRPC client.
uponServerRecvGrpcCall({serviceName = 'unknown', grpcMetadataFromIncomingCtx}) {
if (!grpcMetadataFromIncomingCtx || !(grpcMetadataFromIncomingCtx instanceof grpc.Metadata)) {
// Should fail silently here, without possibly breaking the actual service call stack.
return;
}
const [ctxTraceId, ctxParentId, ctxSpanId] = [
grpcMetadataFromIncomingCtx.get('x-b3-traceid')[0],
grpcMetadataFromIncomingCtx.get('x-b3-parentspanid')[0],
grpcMetadataFromIncomingCtx.get('x-b3-spanid')[0]
];
if (!ctxTraceId || !ctxParentId || !ctxSpanId) {
// Should fail silently here, without possibly breaking the actual service call stack.
return;
}
let ctxSampledAsString = grpcMetadataFromIncomingCtx.get('x-b3-sampled')[0];
let ctxSampled = ctxSampledAsString === '1' || ctxSampledAsString === 'true';
const ctxTraceInfo = new TraceId({
traceId: new Some(ctxTraceId),
parentId: new Some(ctxParentId),
spanId: ctxSpanId,
sampled: new Some(ctxSampled)
});
this.tracer.scoped(() => {
this.traceId = ctxTraceInfo;
this.tracer.setId(ctxTraceInfo);
this.tracer.recordServiceName(serviceName);
this.tracer.recordAnnotation(new Annotation.ServerRecv());
this.tracer.recordAnnotation(new Annotation.LocalAddr({}));
});
}
// Call this right before the GRPC server has finished all respond.
uponServerFinishRespond() {
this.tracer.scoped(() => {
this.tracer.setId(this.traceId);
this.tracer.recordAnnotation(new Annotation.ServerSend());
});
}
// Call this right after everything of this GRPC call has been finished at GRPC client.
afterGrpcCallFinish() {
this.tracer.scoped(() => {
this.tracer.setId(this.traceId);
this.tracer.recordAnnotation(new Annotation.ClientRecv());
});
}
}
module.exports = ZipkinGrpcInterceptor;