vulcain-corejs
Version:
Vulcain micro-service framework
88 lines • 3.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const system_1 = require("../../globals/system");
const dynamicConfiguration_1 = require("../../configurations/dynamicConfiguration");
const os = require("os");
const common_1 = require("../../instrumentations/common");
const { Annotation, HttpHeaders: Header, option: { Some, None }, TraceId, Tracer, ExplicitContext, ConsoleRecorder, BatchRecorder } = require('zipkin');
const { HttpLogger } = require('zipkin-transport-http');
/**
* Needs a property named : zipkin
*/
class ZipkinInstrumentation {
constructor(recorder) {
this.recorder = recorder;
}
static create() {
let zipkinAddress = dynamicConfiguration_1.DynamicConfiguration.getPropertyValue("zipkin");
if (zipkinAddress) {
if (!zipkinAddress.startsWith("http://")) {
zipkinAddress = "http://" + zipkinAddress;
}
if (!/:[0-9]+/.test(zipkinAddress)) {
zipkinAddress = zipkinAddress + ':9411';
}
system_1.Service.log.info(null, () => `Enabling Zipkin instrumentation at ${zipkinAddress}`);
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: `${zipkinAddress}/api/v1/spans`,
httpInterval: 10000
})
});
return new ZipkinInstrumentation(recorder);
}
return null;
}
startSpan(span, name, action) {
return new ZipkinRequestTracker(this.recorder, span.id, span.kind, name, action);
}
}
exports.ZipkinInstrumentation = ZipkinInstrumentation;
class ZipkinRequestTracker {
constructor(recorder, spanId, kind, name, action) {
this.kind = kind;
this.action = action;
this.tracer = new Tracer({ ctxImpl: new ExplicitContext(), recorder });
this.id = new TraceId({
traceId: new Some(spanId.correlationId),
spanId: spanId.spanId,
parentId: spanId.parentId ? new Some(spanId.parentId) : None,
Sampled: None,
Flags: 0
});
// console.log(`Start span ${name}, action ${action}, id: ${this.id}; kind: ${kind}`);
this.tracer.setId(this.id);
this.tracer.recordRpc(action);
this.tracer.recordServiceName(name);
this.tracer.recordLocalAddr(os.hostname());
if (kind === common_1.SpanKind.Command)
this.tracer.recordAnnotation(new Annotation.ClientSend());
else if (kind === common_1.SpanKind.Event)
this.tracer.recordAnnotation(new Annotation.ServerRecv());
else if (kind === common_1.SpanKind.Task)
this.tracer.recordAnnotation(new Annotation.ServerRecv());
else if (kind === common_1.SpanKind.Request)
this.tracer.recordAnnotation(new Annotation.ServerRecv());
}
log(msg) {
}
addTag(name, value) {
this.tracer.recordBinary(name, value.replace(/[:|,\.?&]/g, '-'));
}
trackError(error, msg) {
this.tracer.recordBinary("error", error.message || error);
}
finish() {
// console.log(`End span ${this.name}, action ${this.action}, id: ${this.id}; kind: ${this.kind}`);
if (this.kind === common_1.SpanKind.Command)
this.tracer.recordAnnotation(new Annotation.ClientRecv());
else if (this.kind === common_1.SpanKind.Event)
this.tracer.recordAnnotation(new Annotation.ServerSend());
else if (this.kind === common_1.SpanKind.Task)
this.tracer.recordAnnotation(new Annotation.ServerSend());
else if (this.kind === common_1.SpanKind.Request)
this.tracer.recordAnnotation(new Annotation.ServerSend());
this.tracer = null;
}
}
//# sourceMappingURL=zipkinInstrumentation.js.map