UNPKG

node-sleuth

Version:

A zipkin sleuth impelement for general node.

46 lines (45 loc) 1.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const zipkin_1 = require("zipkin"); const log = debug_1.default('dubbo:sleuth:tracing:ejector'); class AbstractEjector { constructor(traceIdKey, spanIdKey, parentSpanIdKey, sampledKey, debugKey) { this.traceIdKey = traceIdKey; this.spanIdKey = spanIdKey; this.parentSpanIdKey = parentSpanIdKey; this.sampledKey = sampledKey; this.debugKey = debugKey; } readOption(obj, name) { return obj[name] ? new zipkin_1.option.Some(obj[name]) : zipkin_1.option.None; } stringToBoolean(str) { return str === '1' || str === 'true'; } // private stringToIntOption(str: string): option.IOption<number> { // try { // return new option.Some(Number.parseInt(str)); // } catch (err) { // return option.None; // } // } eject(obj) { const spanId = this.readOption(obj, this.spanIdKey); const traceId = this.readOption(obj, this.traceIdKey).getOrElse(''); const parentSpanId = this.readOption(obj, this.parentSpanIdKey); const sampled = this.readOption(obj, this.sampledKey); const parentId = spanId.map(sid => new zipkin_1.TraceId({ spanId: sid, traceId: traceId, parentId: parentSpanId, sampled: sampled.map(this.stringToBoolean), })); parentId.ifPresent(pid => log(`[eject] ${pid.toString()}`)); return parentId; } } exports.AbstractEjector = AbstractEjector;