vulcain-corejs
Version:
Vulcain micro-service framework
123 lines (121 loc) • 4.74 kB
JavaScript
"use strict";
const system_1 = require("../configurations/globals/system");
const dynamicConfiguration_1 = require("../configurations/dynamicConfiguration");
const { Annotation, HttpHeaders: Header, option: { Some, None }, TraceId, Tracer, ExplicitContext, ConsoleRecorder, BatchRecorder } = require('zipkin');
const { HttpLogger } = require('zipkin-transport-http');
//const url = require('url');
class ZipkinInstrumentation {
constructor() {
let zipkinAddress = dynamicConfiguration_1.DynamicConfiguration.getPropertyValue("zipkinAgent");
if (zipkinAddress) {
if (!zipkinAddress.startsWith("http://")) {
zipkinAddress = "http://" + zipkinAddress;
}
const ctxImpl = new ExplicitContext();
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: `${zipkinAddress}:9411/api/v1/spans`,
httpInterval: 10000
})
});
this.tracer = new Tracer({ ctxImpl, recorder });
}
}
startTrace(request) {
return this.tracer && new ZipkinTrace(this.tracer, request);
}
}
exports.ZipkinInstrumentation = ZipkinInstrumentation;
class ZipkinTrace {
constructor(tracer, request) {
this.tracer = tracer;
tracer.scoped(() => {
if (this.containsRequiredHeaders(request)) {
// Child span
const spanId = this.readHeader(request, Header.SpanId);
spanId.ifPresent(sid => {
const traceId = this.readHeader(request, Header.TraceId);
const parentSpanId = this.readHeader(request, Header.ParentSpanId);
const sampled = this.readHeader(request, Header.Sampled);
const flags = this.readHeader(request, Header.Flags).flatMap(this.stringToIntOption).getOrElse(0);
const id = new TraceId({
traceId,
parentId: parentSpanId,
spanId: sid,
sampled: sampled.map(this.stringToBoolean),
flags
});
tracer.setId(id);
});
}
else {
// Root span
tracer.setId(tracer.createRootId());
if (request.headers[Header.Flags]) {
const currentId = tracer.id;
const idWithFlags = new TraceId({
traceId: currentId.traceId,
parentId: currentId.parentId,
spanId: currentId.spanId,
sampled: currentId.sampled,
flags: this.readHeader(request, Header.Flags)
});
tracer.setId(idWithFlags);
}
}
this.id = tracer.id;
tracer.recordServiceName(system_1.System.serviceName + "-" + system_1.System.serviceVersion);
// tracer.recordRpc(request.method);
/* tracer.recordBinary('http.url', url.format({
protocol: req.isSecure() ? 'https' : 'http',
host: req.header('host'),
pathname: req.path()
}));*/
tracer.recordAnnotation(new Annotation.ServerRecv());
// tracer.recordAnnotation(new Annotation.LocalAddr({ port }));
if (this.id.flags !== 0 && this.id.flags !== null) {
tracer.recordBinary(Header.Flags, this.id.flags.toString());
}
});
}
setCommand(verb) {
this.tracer.recordBinary("verb", verb);
}
readHeader(request, header) {
const val = request.headers[header];
if (val) {
return new Some(val);
}
else {
return None;
}
}
containsRequiredHeaders(request) {
return request.headers[Header.TraceId] !== undefined && request.headers[Header.SpanId] !== undefined;
}
endTrace(result) {
try {
this.tracer.scoped(() => {
this.tracer.setId(this.id);
if (result.error)
this.tracer.recordBinary('error', result.error);
this.tracer.recordAnnotation(new Annotation.ServerSend());
});
}
catch (e) {
}
}
stringToIntOption(str) {
try {
return new Some(parseInt(str));
}
catch (err) {
return None;
}
}
stringToBoolean(str) {
return str === '1';
}
}
exports.ZipkinTrace = ZipkinTrace;
//# sourceMappingURL=zipkinInstrumentation.js.map