@kangc/skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
103 lines • 4.41 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 });
exports.ContextCarrier = void 0;
var tslib_1 = require("tslib");
var ID_1 = tslib_1.__importDefault(require("../../trace/ID"));
var CarrierItem_1 = require("./CarrierItem");
var ContextCarrier = /** @class */ (function (_super) {
tslib_1.__extends(ContextCarrier, _super);
function ContextCarrier(traceId, segmentId, spanId, service, serviceInstance, endpoint, clientAddress, items) {
if (items === void 0) { items = []; }
var _this = _super.call(this, 'sw8') || this;
_this.traceId = traceId;
_this.segmentId = segmentId;
_this.spanId = spanId;
_this.service = service;
_this.serviceInstance = serviceInstance;
_this.endpoint = endpoint;
_this.clientAddress = clientAddress;
_this.items = items;
_this.encode = function (s) {
return Buffer.from(s).toString('base64');
};
_this.decode = function (s) {
return Buffer.from(s, 'base64').toString();
};
_this.items.push(_this);
return _this;
}
Object.defineProperty(ContextCarrier.prototype, "value", {
get: function () {
var _a, _b, _c;
return this.isValid()
? [
'1',
this.encode(((_a = this.traceId) === null || _a === void 0 ? void 0 : _a.toString()) || ''),
this.encode(((_b = this.segmentId) === null || _b === void 0 ? void 0 : _b.toString()) || ''),
(_c = this.spanId) === null || _c === void 0 ? void 0 : _c.toString(),
this.encode(this.service || ''),
this.encode(this.serviceInstance || ''),
this.encode(this.endpoint || ''),
this.encode(this.clientAddress || ''),
].join('-')
: '';
},
set: function (val) {
if (!val) {
return;
}
var parts = val.split('-');
if (parts.length != 8) {
return;
}
this.traceId = new ID_1.default(this.decode(parts[1]));
this.segmentId = new ID_1.default(this.decode(parts[2]));
this.spanId = Number.parseInt(parts[3], 10);
this.service = this.decode(parts[4]);
this.serviceInstance = this.decode(parts[5]);
this.endpoint = this.decode(parts[6]);
this.clientAddress = this.decode(parts[7]);
},
enumerable: false,
configurable: true
});
ContextCarrier.prototype.isValid = function () {
var _a, _b;
return Boolean(((_a = this.traceId) === null || _a === void 0 ? void 0 : _a.rawId) && ((_b = this.segmentId) === null || _b === void 0 ? void 0 : _b.rawId) &&
this.spanId !== undefined &&
!isNaN(this.spanId) &&
this.service &&
this.endpoint &&
this.clientAddress !== undefined);
};
ContextCarrier.from = function (map) {
if (!Object.prototype.hasOwnProperty.call(map, 'sw8'))
return;
var carrier = new ContextCarrier();
carrier.items
.filter(function (item) { return Object.prototype.hasOwnProperty.call(map, item.key); })
.forEach(function (item) { return (item.value = map[item.key]); });
return carrier;
};
return ContextCarrier;
}(CarrierItem_1.CarrierItem));
exports.ContextCarrier = ContextCarrier;
//# sourceMappingURL=ContextCarrier.js.map