lightstep-tracer
Version:
> ❗ **This instrumentation is no longer recommended**. Please review [documentation on setting up and configuring the OpenTelemetry Node.js Launcher](https://github.com/lightstep/otel-launcher-node) or [OpenTelemetry JS (Browser)](https://github.com/open-
151 lines (112 loc) • 5.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _each2 = _interopRequireDefault(require("../_each"));
var _span_context_imp = _interopRequireDefault(require("./span_context_imp"));
var _propagator_ls = _interopRequireDefault(require("./propagator_ls"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var CARRIER_DD_TRACER_STATE_PREFIX = 'x-datadog-';
var DDPropagator = /*#__PURE__*/function () {
function DDPropagator(tracer) {
_classCallCheck(this, DDPropagator);
this._tracer = tracer;
this._baggagePrefix = _propagator_ls["default"];
this._carrierPrefix = CARRIER_DD_TRACER_STATE_PREFIX;
}
_createClass(DDPropagator, [{
key: "inject",
value: function inject(spanContext, carrier) {
var _this = this;
if (!carrier) {
this._tracer._error('Unexpected null carrier in call to inject');
return;
}
if (_typeof(carrier) !== 'object') {
this._tracer._error("Unexpected '".concat(_typeof(carrier), "' FORMAT_TEXT_MAP carrier in call to inject"));
return;
} // eslint-disable-next-line max-len
// Per https://github.com/lightstep/lightstep-tracer-javascript/blob/master/src/imp/platform/node/platform_node.js#L91
// all generated GUIDs are base 16 strings.
// DD headers expect integers, not base 16 values.
carrier["".concat(this._carrierPrefix, "parent-id")] = parseInt(spanContext._guid, 16).toString();
carrier["".concat(this._carrierPrefix, "trace-id")] = parseInt(spanContext.traceGUID(), 16).toString();
if (spanContext._sampled) {
carrier["".concat(this._carrierPrefix, "sampling-priority")] = '1';
} else {
carrier["".concat(this._carrierPrefix, "sampling-priority")] = '0';
}
spanContext.forEachBaggageItem(function (key, value) {
carrier["".concat(_this._baggagePrefix).concat(key)] = value;
});
return carrier;
}
}, {
key: "extract",
value: function extract(carrier) {
var _this2 = this;
// Iterate over the contents of the carrier and set the properties
// accordingly.
var foundFields = 0;
var spanGUID = null;
var traceGUID = null;
var sampled = true;
(0, _each2["default"])(carrier, function (value, key) {
key = key.toLowerCase();
if (key.substr(0, _this2._carrierPrefix.length) !== _this2._carrierPrefix) {
return;
}
var suffix = key.substr(_this2._carrierPrefix.length);
switch (suffix) {
case 'trace-id':
foundFields++;
traceGUID = parseInt(value, 10).toString(16);
break;
case 'parent-id':
foundFields++;
spanGUID = parseInt(value, 10).toString(16);
break;
case 'sampling-priority':
// TODO: support sampling priority somehow. This is a float64 that does not
// necessarily represent the sampling decision
if (value === 0) {
sampled = false;
}
break;
default:
_this2._tracer._error("Unrecognized carrier key '".concat(key, "' with recognized prefix. Ignoring."));
break;
}
});
if (foundFields === 0) {
// This is not an error per se, there was simply no SpanContext
// in the carrier.
return null;
}
if (foundFields < 2) {
// A partial SpanContext suggests some sort of data corruption.
this._tracer._error("Only found a partial SpanContext: ".concat(carrier));
return null;
}
var spanContext = new _span_context_imp["default"](spanGUID, traceGUID, sampled);
(0, _each2["default"])(carrier, function (value, key) {
key = key.toLowerCase();
if (key.substr(0, _this2._baggagePrefix.length) !== _this2._baggagePrefix) {
return;
}
var suffix = key.substr(_this2._baggagePrefix.length);
spanContext.setBaggageItem(suffix, value);
});
return spanContext;
}
}]);
return DDPropagator;
}();
exports["default"] = DDPropagator;
module.exports = exports.default;
//# sourceMappingURL=propagator_dd.js.map
;