UNPKG

skywalking-apache

Version:

The NodeJS agent for Apache SkyWalking

152 lines 5.81 kB
"use strict"; /*! * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var Segment_1 = tslib_1.__importDefault(require("../../trace/context/Segment")); var EntrySpan_1 = tslib_1.__importDefault(require("../../trace/span/EntrySpan")); var ExitSpan_1 = tslib_1.__importDefault(require("../../trace/span/ExitSpan")); var LocalSpan_1 = tslib_1.__importDefault(require("../../trace/span/LocalSpan")); var packageInfo = tslib_1.__importStar(require("../../../package.json")); var Buffer_1 = tslib_1.__importDefault(require("../../agent/Buffer")); var logging_1 = require("../../logging"); var async_hooks_1 = require("async_hooks"); var SegmentRef_1 = tslib_1.__importDefault(require("../../trace/context/SegmentRef")); var logger = logging_1.createLogger(__filename); var SpanContext = /** @class */ (function () { function SpanContext(asyncId) { this.asyncId = asyncId; this.spanId = 0; this.spans = []; this.segment = new Segment_1.default(); } Object.defineProperty(SpanContext.prototype, "parent", { get: function () { if (this.spans.length > 0) { return this.spans[this.spans.length - 1]; } return null; }, enumerable: false, configurable: true }); Object.defineProperty(SpanContext.prototype, "parentId", { get: function () { return this.parent ? this.parent.id : -1; }, enumerable: false, configurable: true }); SpanContext.prototype.newEntrySpan = function (operation, carrier) { if (logger.isDebugEnabled()) { logger.debug('Creating entry span', { parentId: this.parentId, executionAsyncId: async_hooks_1.executionAsyncId(), }); } var span = new EntrySpan_1.default({ id: this.spanId++, parentId: this.parentId, context: this, operation: operation, }); if (carrier && carrier.isValid()) { span.inject(carrier); } return span; }; SpanContext.prototype.newExitSpan = function (operation, peer) { if (logger.isDebugEnabled()) { logger.debug('Creating exit span', { parentId: this.parentId, executionAsyncId: async_hooks_1.executionAsyncId(), }); } return new ExitSpan_1.default({ id: this.spanId++, parentId: this.parentId, context: this, peer: peer, operation: operation, }); }; SpanContext.prototype.newLocalSpan = function (operation) { if (logger.isDebugEnabled()) { logger.debug('Creating local span', { parentId: this.parentId, executionAsyncId: async_hooks_1.executionAsyncId(), }); } return new LocalSpan_1.default({ id: this.spanId++, parentId: this.parentId, context: this, operation: operation, }); }; SpanContext.prototype.start = function (span) { if (this.spans.every(function (s) { return s.id !== span.id; })) { this.spans.push(span); } return this; }; SpanContext.prototype.stop = function (span) { logger.info('Stopping span', { span: span, spans: this.spans }); if (this.spans[this.spans.length - 1] !== span) { logger.error("Stopping unexpected span. Consider report this to " + packageInfo.bugs.url); return true; } if (this.tryFinish(span)) { this.spans.splice(0, 1); } return this.spans.length === 0; }; SpanContext.prototype.tryFinish = function (span) { if (span.finish(this.segment)) { if (logger.isDebugEnabled()) { logger.debug('Finishing span', { span: span }); } Buffer_1.default.put(this.segment); return true; } return false; }; SpanContext.prototype.currentSpan = function () { return this.spans[this.spans.length - 1]; }; SpanContext.prototype.capture = function () { var _a, _b; return { segmentId: this.segment.segmentId, spanId: (_b = (_a = this.currentSpan()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : -1, traceId: this.segment.relatedTraces[0], parentEndpoint: this.spans[0].operation, }; }; SpanContext.prototype.restore = function (snapshot) { var _a; var ref = SegmentRef_1.default.fromSnapshot(snapshot); this.segment.refer(ref); (_a = this.currentSpan()) === null || _a === void 0 ? void 0 : _a.refer(ref); this.segment.relate(ref.traceId); }; return SpanContext; }()); exports.default = SpanContext; //# sourceMappingURL=SpanContext.js.map