UNPKG

skywalking-backend-js-netease

Version:

The NodeJS agent for Apache SkyWalking

133 lines 4.42 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 }); const tslib_1 = require("tslib"); const AgentConfig_1 = tslib_1.__importDefault(require("../../config/AgentConfig")); const Component_1 = require("../Component"); const Tracing_pb_1 = require("../../proto/language-agent/Tracing_pb"); const logging_1 = require("../../logging"); const packageInfo = tslib_1.__importStar(require("../../../package.json")); const logger = logging_1.createLogger(__filename); class Span { constructor(options) { this.id = -1; this.parentId = -1; this.peer = ''; this.layer = Tracing_pb_1.SpanLayer.UNKNOWN; this.component = Component_1.Component.UNKNOWN; this.depth = 0; this.isCold = false; this.tags = []; this.logs = []; this.refs = []; this.startTime = 0; this.endTime = 0; this.errored = false; this.lastError = null; this.context = options.context; this.operation = options.operation; this.type = options.type; if (options.id !== undefined) this.id = options.id; if (options.parentId !== undefined) this.parentId = options.parentId; if (options.peer) this.peer = options.peer; if (options.layer) this.layer = options.layer; if (options.component) this.component = options.component; } start() { if (++this.depth === 1) { this.startTime = new Date().getTime(); this.context.start(this); } } stop() { if (--this.depth === 0) this.context.stop(this); } async() { this.context.async(this); } resync() { this.context.resync(this); } finish(segment) { if (this.isCold && AgentConfig_1.default.coldEndpoint) this.operation = this.operation + '<cold>'; this.endTime = new Date().getTime(); segment.archive(this); return true; } // noinspection JSUnusedLocalSymbols inject() { throw new Error(` can only inject context carrier into ExitSpan, this may be a potential bug in the agent, please report this in ${packageInfo.bugs.url} if you encounter this. `); } extract(carrier) { this.context.segment.relate(carrier.traceId); return this; } hasTag(key) { return !this.tags.every((t) => t.key !== key); } tag(tag, insert) { if (tag.overridable) { const sameTags = this.tags.filter((it) => it.key === tag.key); if (sameTags.length) { sameTags.forEach((it) => (it.val = tag.val)); return this; } } const tagObj = Object.assign({}, tag); if (!insert) this.tags.push(tagObj); else this.tags.unshift(tagObj); return this; } log(key, val) { this.logs.push({ timestamp: new Date().getTime(), items: [{ key, val: `${val}` }] }); return this; } error(error) { if (error === this.lastError) // don't store duplicate identical error twice return this; this.errored = true; this.lastError = error; this.log('Stack', (error === null || error === void 0 ? void 0 : error.stack) || ''); return this; } refer(ref) { if (!this.refs.includes(ref)) { this.refs.push(ref); } return this; } } exports.default = Span; //# sourceMappingURL=Span.js.map