UNPKG

skywalking-backend-js-netease

Version:

The NodeJS agent for Apache SkyWalking

89 lines 3.97 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 }); exports.AWSLambdaTriggerPlugin = void 0; const tslib_1 = require("tslib"); const AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig")); const ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager")); const Component_1 = require("../trace/Component"); const index_1 = tslib_1.__importDefault(require("../index")); class AWSLambdaTriggerPlugin { // default working start function, should be overridden by the various types of lambda trigger subclasses start(event, context) { const span = ContextManager_1.default.current.newEntrySpan(context.functionName ? `/${context.functionName}` : '/'); span.component = Component_1.Component.AWSLAMBDA_FUNCTION; span.peer = 'Unknown'; span.start(); return span; } // default working stop function stop(span, err, res) { span.stop(); } wrap(func) { return async (event, context, callback) => { ContextManager_1.default.removeTailFinishedContexts(); // need this because AWS seems to chain sequential independent operations linearly instead of hierarchically const span = this.start(event, context); let ret; let stop = async (err, res) => { stop = async (err, res) => { }; this.stop(span, err, res); if (AgentConfig_1.default.awsLambdaFlush) { await new Promise((resolve) => setTimeout(resolve, 0)); // 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 const p = index_1.default.flush(); // flush all data before aws freezes the process on exit if (p) await p; } return res; }; let resolve; let reject; let callbackDone = false; const callbackPromise = new Promise((_resolve, _reject) => { resolve = _resolve; reject = _reject; }); try { ret = func(event, context, (err, res) => { if (!callbackDone) { callbackDone = true; if (err) reject(err); else resolve(res); } }); if (typeof (ret === null || ret === void 0 ? void 0 : ret.then) !== 'function') // generic Promise check ret = callbackPromise; return await stop(null, await ret); } catch (e) { span.error(e); await stop(e, null); throw e; } }; } } exports.AWSLambdaTriggerPlugin = AWSLambdaTriggerPlugin; // noinspection JSUnusedGlobalSymbols exports.default = new AWSLambdaTriggerPlugin(); //# sourceMappingURL=AWSLambdaTriggerPlugin.js.map