UNPKG

skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

111 lines 5.23 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 }); var tslib_1 = require("tslib"); var os_1 = require("os"); var AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig")); 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 AWSLambdaTriggerPlugin_1 = require("../aws/AWSLambdaTriggerPlugin"); var SDK2_1 = require("../aws/SDK2"); var AWS2LambdaPlugin = /** @class */ (function () { function AWS2LambdaPlugin() { this.module = 'aws-sdk'; this.versions = '2.*'; } AWS2LambdaPlugin.prototype.install = function (installer) { var AWS = SDK2_1.getAWS(installer); var _Lambda = AWS.Lambda; function Lambda() { var lambda = _Lambda.apply(this, arguments); var _invoke = lambda.invoke; lambda.invoke = function (params, callback) { if (params.InvocationType === 'DryRun') return _invoke.call(this, params, callback); var funcName = params.FunctionName; var li = funcName.lastIndexOf(':'); var name; if (li === -1) name = funcName; else { // my-function:v1 if (funcName.indexOf(':') === -1) name = funcName.slice(li); else name = funcName.slice(li + 1); // 123456789012:function:my-function, arn:aws:lambda:us-west-2:123456789012:function:my-function } var span = ContextManager_1.default.current.newExitSpan("AWS/Lambda/invoke/" + (name || ''), Component_1.Component.AWSLAMBDA_FUNCTION, Component_1.Component.HTTP); span.component = Component_1.Component.AWSLAMBDA_FUNCTION; span.layer = Tracing_pb_1.SpanLayer.HTTP; if (li !== -1) span.tag(Tag_1.default.arn(funcName)); if (AgentConfig_1.default.awsLambdaChain) { var payload = params.Payload; if (payload instanceof Buffer) payload = payload.toString(); if (typeof payload === 'string') { var traceid = JSON.stringify(span.inject().value + "/" + os_1.hostname()); var keyTrace = JSON.stringify(AWSLambdaTriggerPlugin_1.KeyTrace); var keyParams = JSON.stringify(AWSLambdaTriggerPlugin_1.KeyParams); if (payload.match(/^\s*{\s*}\s*$/)) payload = "{" + keyTrace + ":" + traceid + "}"; else if (payload.match(/^\s*{/)) payload = "{" + keyTrace + ":" + traceid + "," + payload.slice(payload.indexOf('{') + 1); else payload = "{" + keyTrace + ":" + traceid + "," + keyParams + ":" + payload + "}"; params = Object.assign({}, params, { Payload: payload }); } } return SDK2_1.execute(span, this, _invoke, params, callback); }; return lambda; } Object.assign(Lambda, _Lambda); Lambda.prototype = _Lambda.prototype; AWS.Lambda = Lambda; }; return AWS2LambdaPlugin; }()); // noinspection JSUnusedGlobalSymbols exports.default = new AWS2LambdaPlugin(); // // Example code for test maybe: // const AWS = require("aws-sdk"); // AWS.config.update({region: 'your-region'}); // const lambda = new AWS.Lambda(); // function callback(err, data) { // console.log('... callback err:', err); // console.log('... callback data:', data); // } // const params = { // FunctionName: 'function_to_call', // InvocationType: 'RequestResponse', // or 'Event', // // LogType: 'Tail', // Payload: JSON.stringify({arg1: 'args to function'}), // }; // lambda.invoke(params, callback); // // OR: // lambda.invoke(params).send(callback); // // OR: // lambda.invoke(params).promise() // .then(r => { console.log('... promise res:', r); }) // .catch(e => { console.log('... promise err:', e); }); //# sourceMappingURL=AWS2LambdaPlugin.js.map