@kangc/skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
131 lines • 4.86 kB
JavaScript
"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 AgentConfig_1 = tslib_1.__importDefault(require("../../config/AgentConfig"));
var Component_1 = require("../Component");
var Tracing_pb_1 = require("../../proto/language-agent/Tracing_pb");
var logging_1 = require("../../logging");
var packageInfo = tslib_1.__importStar(require("../../../package.json"));
var logger = logging_1.createLogger(__filename);
var Span = /** @class */ (function () {
function Span(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;
}
Span.prototype.start = function () {
if (++this.depth === 1) {
this.startTime = new Date().getTime();
this.context.start(this);
}
};
Span.prototype.stop = function () {
if (--this.depth === 0)
this.context.stop(this);
};
Span.prototype.async = function () {
this.context.async(this);
};
Span.prototype.resync = function () {
this.context.resync(this);
};
Span.prototype.finish = function (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
Span.prototype.inject = function () {
throw new Error("\n can only inject context carrier into ExitSpan, this may be a potential bug in the agent,\n please report this in " + packageInfo.bugs.url + " if you encounter this.\n ");
};
Span.prototype.extract = function (carrier) {
this.context.segment.relate(carrier.traceId);
return this;
};
Span.prototype.hasTag = function (key) {
return !this.tags.every(function (t) { return t.key !== key; });
};
Span.prototype.tag = function (tag, insert) {
if (tag.overridable) {
var sameTags = this.tags.filter(function (it) { return it.key === tag.key; });
if (sameTags.length) {
sameTags.forEach(function (it) { return (it.val = tag.val); });
return this;
}
}
var tagObj = Object.assign({}, tag);
if (!insert)
this.tags.push(tagObj);
else
this.tags.unshift(tagObj);
return this;
};
Span.prototype.log = function (key, val) {
this.logs.push({
timestamp: new Date().getTime(),
items: [{ key: key, val: "" + val }]
});
return this;
};
Span.prototype.error = function (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;
};
Span.prototype.refer = function (ref) {
if (!this.refs.includes(ref)) {
this.refs.push(ref);
}
return this;
};
return Span;
}());
exports.default = Span;
//# sourceMappingURL=Span.js.map