UNPKG

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-

312 lines (257 loc) 13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var opentracing = _interopRequireWildcard(require("opentracing")); var coerce = _interopRequireWildcard(require("./coerce")); var constants = _interopRequireWildcard(require("../constants")); var _each2 = _interopRequireDefault(require("../_each")); var _platform_abstraction_layer = require("../platform_abstraction_layer"); var _log_record_imp = _interopRequireDefault(require("./log_record_imp")); var _util = _interopRequireDefault(require("./util/util")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var SpanImp = /*#__PURE__*/function (_opentracing$Span) { _inherits(SpanImp, _opentracing$Span); var _super = _createSuper(SpanImp); // ---------------------------------------------------------------------- // // Private methods // ---------------------------------------------------------------------- // function SpanImp(tracer, name, spanContext) { var _this; _classCallCheck(this, SpanImp); _this = _super.call(this); console.assert(_typeof(tracer) === 'object', 'Invalid runtime'); // eslint-disable-line no-console _this._tracerImp = tracer; _this._ctx = spanContext; _this._ended = false; _this._operationName = name; _this._tags = {}; _this._beginMicros = tracer._platform.nowMicros(); _this._endMicros = 0; _this._errorFlag = false; _this._log_records = null; return _this; } // ---------------------------------------------------------------------- // // LightStep Extensions // ---------------------------------------------------------------------- // _createClass(SpanImp, [{ key: "_tracer", value: // ---------------------------------------------------------------------- // // opentracing.Span SPI // ---------------------------------------------------------------------- // function _tracer() { return this._tracerImp; } }, { key: "_context", value: function _context() { return this._ctx; } }, { key: "_setOperationName", value: function _setOperationName(name) { this._operationName = "".concat(name); } }, { key: "_setBaggageItem", value: function _setBaggageItem(key, value) { this._ctx.setBaggageItem(key, value); } }, { key: "_getBaggageItem", value: function _getBaggageItem(key) { return this._ctx.getBaggageItem(key); } }, { key: "_addTags", value: function _addTags(keyValuePairs) { var self = this; (0, _each2["default"])(keyValuePairs, function (value, key) { self._tags[key] = value; }); } }, { key: "_log", value: function _log(keyValuePairs, timestamp) { var self = this; var argumentType = _typeof(keyValuePairs); if (argumentType !== 'object') { self._tracerImp._error('Span.log() expects an object as its first argument'); return; } var tsMicros = timestamp ? timestamp * 1000 : self._tracerImp._platform.nowMicros(); var record = new _log_record_imp["default"](self._tracerImp.getLogFieldKeyHardLimit(), self._tracerImp.getLogFieldValueHardLimit(), tsMicros, keyValuePairs); self._log_records = self._log_records || []; self._log_records.push(record); self._tracerImp.emit('log_added', record); } }, { key: "_finish", value: function _finish(finishTime) { return this.end(finishTime); } }, { key: "getOperationName", value: function getOperationName() { return this._operationName; } // Getter only. The GUID is immutable once set internally. }, { key: "guid", value: function guid() { return this._ctx._guid; } }, { key: "traceGUID", value: function traceGUID() { return this._ctx._traceGUID; } }, { key: "parentGUID", value: function parentGUID() { return this._tags.parent_span_guid; } }, { key: "setParentGUID", value: function setParentGUID(guid) { this._tags.parent_span_guid = coerce.toString(guid); return this; } }, { key: "beginMicros", value: function beginMicros() { return this._beginMicros; } }, { key: "setBeginMicros", value: function setBeginMicros(micros) { this._beginMicros = micros; return this; } }, { key: "endMicros", value: function endMicros() { return this._endMicros; } }, { key: "setEndMicros", value: function setEndMicros(micros) { this._endMicros = micros; return this; } }, { key: "isSampled", value: function isSampled() { return this._ctx._sampled; } /** * Returns a URL to the trace containing this span. * * Unlike most methods, it *is* safe to call this method after `finish()`. * * @return {string} the absolute URL for the span */ }, { key: "generateTraceURL", value: function generateTraceURL() { var micros; if (this._beginMicros > 0 && this._endMicros > 0) { micros = Math.floor((this._beginMicros + this._endMicros) / 2); } else { micros = this._tracerImp._platform.nowMicros(); } var urlPrefix = constants.LIGHTSTEP_APP_URL_PREFIX; var accessToken = encodeURIComponent(this._tracerImp.options().access_token); var guid = encodeURIComponent(this.guid()); return "".concat(urlPrefix, "/").concat(accessToken, "/trace?span_guid=").concat(guid, "&at_micros=").concat(micros); } }, { key: "getTags", value: function getTags() { return this._tags; } /** * Finishes the span. * * @param {Number} finishTime * Optional Unix timestamp in milliseconds setting an explicit * finish time for the span. */ }, { key: "end", value: function end(finishTime) { // Ensure a single span is not recorded multiple times if (this._ended) { return; } this._ended = true; if (finishTime !== undefined) { this.setEndMicros(Math.floor(finishTime * 1000)); } // Do not set endMicros if it has already been set. This accounts for // the case of a span that has had it's times set manually (i.e. allows // for retroactively created spans that might not be possible to create // in real-time). if (this._endMicros === 0) { this.setEndMicros(this._tracerImp._platform.nowMicros()); } if (_util["default"].shouldSendMetaSpan(this._tracer().options(), this.getTags())) { var _tags; this._tracerImp.startSpan(constants.LS_META_SP_FINISH, { tags: (_tags = {}, _defineProperty(_tags, constants.LS_META_EVENT_KEY, true), _defineProperty(_tags, constants.LS_META_TRACE_KEY, this.traceGUID()), _defineProperty(_tags, constants.LS_META_SPAN_KEY, this.guid()), _tags) }).finish(); } // Only record span if sampled if (this.isSampled()) { this._tracerImp._addSpanRecord(this); } } }, { key: "_toThrift", value: function _toThrift() { var _this2 = this; var attributes = []; (0, _each2["default"])(this._tags, function (value, key) { // eslint-disable-next-line camelcase attributes.push(new _platform_abstraction_layer.crouton_thrift.KeyValue({ Key: coerce.toString(key), Value: coerce.toString(value) })); }); var logs = []; (0, _each2["default"])(this._log_records, function (logRecord) { var logThrift = logRecord.toThrift(); _this2._tracerImp._counters['logs.keys.over_limit'] += logRecord.getNumKeysOverLimit(); _this2._tracerImp._counters['logs.values.over_limit'] += logRecord.getNumValuesOverLimit(); logs.push(logThrift); }); // eslint-disable-next-line camelcase return new _platform_abstraction_layer.crouton_thrift.SpanRecord({ span_guid: this.guid(), trace_guid: this.traceGUID(), runtime_guid: this._tracerImp.guid(), span_name: this._operationName, oldest_micros: this._beginMicros, youngest_micros: this._endMicros, attributes: attributes, error_flag: this._errorFlag, log_records: logs }); } }]); return SpanImp; }(opentracing.Span); exports["default"] = SpanImp; module.exports = exports.default; //# sourceMappingURL=span_imp.js.map