skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
166 lines • 8.94 kB
JavaScript
;
/*!
*
* 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.KeyParams = exports.KeyTrace = exports.AWSLambdaTriggerPlugin = void 0;
var tslib_1 = require("tslib");
var perf_hooks_1 = require("perf_hooks");
var AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig"));
var ContextCarrier_1 = require("../trace/context/ContextCarrier");
var ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager"));
var Component_1 = require("../trace/Component");
var Tag_1 = tslib_1.__importDefault(require("../Tag"));
var Tracing_pb_1 = require("../proto/language-agent/Tracing_pb");
var index_1 = tslib_1.__importDefault(require("../index"));
var _lastTimestamp = -Infinity;
var KeyTrace = '__revdTraceId';
exports.KeyTrace = KeyTrace;
var KeyParams = '__revdParams'; // original params (if not originally an object)
exports.KeyParams = KeyParams;
var AWSLambdaTriggerPlugin = /** @class */ (function () {
function AWSLambdaTriggerPlugin() {
}
// default working start function, should be overridden by the various types of lambda trigger subclasses
AWSLambdaTriggerPlugin.prototype.start = function (event, context) {
var peer = 'Unknown';
var carrier = undefined;
if (event && typeof event === 'object') {
// pull traceid out of params if it is in there
var traceId = event[KeyTrace];
if (traceId && typeof traceId === 'string') {
var idx = traceId.lastIndexOf('/');
if (idx !== -1) {
peer = traceId.slice(idx + 1);
traceId = traceId.slice(0, idx);
carrier = ContextCarrier_1.ContextCarrier.from({ sw8: traceId });
if (carrier) {
if (!carrier.isValid())
carrier = undefined;
else {
var params = event[KeyParams];
if (params !== undefined)
event = params;
else
delete event[KeyTrace];
}
}
}
}
}
var span = ContextManager_1.default.current.newEntrySpan('AWS/Lambda/' + (context.functionName || ''), carrier);
span.component = Component_1.Component.AWSLAMBDA_FUNCTION;
span.peer = peer;
span.start();
return [span, event];
};
// default working stop function
AWSLambdaTriggerPlugin.prototype.stop = function (span, err, res) {
span.stop();
};
AWSLambdaTriggerPlugin.prototype.wrap = function (func) {
var _this = this;
return function (event, context, callback) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var ts, done, cbdone, _done, _a, span, _event, ret, e_1;
var _this = this;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
ts = perf_hooks_1.performance.now() / 1000;
done = function (err, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var p;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
done = function (err, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, res];
}); }); };
if (err)
span.error(err);
this.stop(span, err, res);
if (!(AgentConfig_1.default.awsLambdaFlush >= 0)) return [3 /*break*/, 4];
if (!(ts - _lastTimestamp >= AgentConfig_1.default.awsLambdaFlush)) return [3 /*break*/, 3];
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 0); })];
case 1:
_a.sent(); // child spans of this span may have finalization waiting in the event loop in which case we give them a chance to run so that the segment can be archived properly for flushing
p = index_1.default.flush();
if (!p) return [3 /*break*/, 3];
return [4 /*yield*/, p];
case 2:
_a.sent();
_a.label = 3;
case 3:
_lastTimestamp = perf_hooks_1.performance.now() / 1000;
_a.label = 4;
case 4: return [2 /*return*/, res];
}
});
}); };
cbdone = function (err, res) {
// for the weird AWS done function behaviors
cbdone = function (err, res) { return ({ finally: function () { return undefined; } }); };
return done(err, res);
};
ContextManager_1.default.clearAll(); // need this because AWS seems to chain sequential independent operations linearly instead of hierarchically
_done = context.done;
_a = this.start(event, context), span = _a[0], _event = _a[1];
_b.label = 1;
case 1:
_b.trys.push([1, 5, , 7]);
event = _event;
span.layer = Tracing_pb_1.SpanLayer.HTTP;
if (context.invokedFunctionArn)
span.tag(Tag_1.default.arn(context.invokedFunctionArn));
context.done = function (err, res) {
cbdone(err, res).finally(function () { return _done(err, res); });
};
context.succeed = function (res) {
cbdone(null, res).finally(function () { return _done(null, res); });
};
context.fail = function (err) {
cbdone(err).finally(function () { return _done(err); });
};
ret = func(event, context, function (err, res) {
cbdone(err, res).finally(function () { return callback(err, res); });
});
if (!(typeof (ret === null || ret === void 0 ? void 0 : ret.then) === 'function')) return [3 /*break*/, 3];
return [4 /*yield*/, ret];
case 2:
// generic Promise check
ret = _b.sent();
_b.label = 3;
case 3: return [4 /*yield*/, done(null, ret)];
case 4: return [2 /*return*/, _b.sent()];
case 5:
e_1 = _b.sent();
return [4 /*yield*/, done(e_1, null)];
case 6:
_b.sent();
throw e_1;
case 7: return [2 /*return*/];
}
});
}); };
};
return AWSLambdaTriggerPlugin;
}());
exports.AWSLambdaTriggerPlugin = AWSLambdaTriggerPlugin;
// noinspection JSUnusedGlobalSymbols
exports.default = new AWSLambdaTriggerPlugin();
//# sourceMappingURL=AWSLambdaTriggerPlugin.js.map