Download PDF
Download page Node.js Serverless Tracer.
Node.js Serverless Tracer
This page describes how to use the Node.js Serverless Tracer with AWS Lambda functions during development. The preferred method is to use the Splunk AppDynamics AWS Lambda Extension. See Use the Cisco AppDynamics AWS Lambda Extension to Instrument Serverless APM at Runtime.
Before You Begin
After you have subscribed to Serverless APM for AWS Lambda and set up your environment variables, you can begin instrumentation. Ensure that your setup meets the following requirements:
- Existing AWS Lambda functions built with Node.js versions 16.x, 18.x, and 20.x.
- Active Serverless APM for AWS Lambda subscription
- Splunk AppDynamics SaaS Controller >= 4.5.11
This document contains links to Amazon Web Services (AWS) documentation. Splunk AppDynamicsmakes no representation as to the accuracy of Amazon documentation because Amazon controls its own documentation.
Install the Node.js Serverless Tracer Using npm During Development
You can instrument your ES6 lambda either using the manual instrumentation or automatic instrumentation.
Manual Instrumentation
Perform these steps based on your choice of Java Script modules.
To instrument your ES6 lambda, you can install the Node.js Serverless Tracer using these steps.
Run the following command to install the Node.js serverless tracer using
npm
during development for ES6.npm install appdynamics-lambda-tracer --save
JSRun the following command to import the tracer:
import tracer from "appdynamics-lambda-tracer"; tracer.init();
JSInstrument the Function with the Node.js Serverless Tracer. The code snippet below demonstrates how to instrument a serverless function with the tracer:
export const handler = tracer.functionWrapper(async (event) => { // YOUR CODE HERE return "Hello World"; });
JS
To instrument your CommonJS lambda we recommend the AWS Lambda Extension. You can also install the Node.js Serverless Tracer using these steps:
To install the tracer, navigate to the directory where the
package.json
file resides. Run the followingnpm
command:npm install appdynamics-lambda-tracer --save
JSThe command downloads the tracer dependency into a
node_modules/
folder.Initialize the Node.js Serverless Tracer below the require statement, by calling the
tracer.init
method.tracer = require('appdynamics-lambda-tracer'); //Initialize the tracer tracer.init();
JSTo complete instrumentation, call
tracer.mainModule(module)
as the last line of your code. The code snippet below demonstrates how to instrument a serverless function with the tracer:// Your AWS Lambda handler function code here, for example: exports.handler = async (event) => { // TODO implement const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; }; //Complete the instrumentation tracer.mainModule(module);
JS
To instrument your TypeScript lambda, you can install the Node.js Serverless Tracer using these steps.
Run the following command to install the Node.js serverless tracer using
npm
during development.npm install appdynamics-lambda-tracer -–save npm install -D @types/aws-lambda esbuild
JSRun the following command to import the tracer:
import tracer from "appdynamics-lambda-tracer"; tracer.init();
JSInstrument the Function with the Node.js Serverless Tracer. The code snippet below demonstrates how to instrument a serverless function with the tracer:
export const handler = tracer.functionWrapper ( async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => { return { statusCode: 200, body: JSON.stringify({ message: 'hello world', }), }; });
JS
Automatic Instrumentation
For automatic instrumentation you must configure the following environment variables.
APPDYNAMICS_IS_ESM_ENABLE
- The value of this variable depends on the Java Script module that you are utilizing. If you are using ESM module, set the variable totrue
. However, if you are not using ESM module, set the variable tofalse
.APPDYNAMICS_APPLICATION_LAMBDA_HANDLER
- You must define this variable to execute the lambda. Specify the handler function of the lambda in this variable.
Ensure to remove the AWS_LAMBDA_EXEC_WRAPPER
environment variable.
To complete the instrumentation, perform the following steps based on your choice of Java Script modules.
In the Layers section, add theSplunk AppDynamics layer. Give it the name
appdynamics-lambda-extension
, and the Version ARN value as shown below:arn:aws:lambda:us-west-<###>:layer:appdynamics-lambda-nodejs-<###>
JSIn the Runtime settings section, update the lambda handler to theSplunk AppDynamics specific handler,
appdynamics-lambda-nodejs-tracer.handler
.appdynamics-lambda-nodejs-tracer.handler
JSIn the Configuration section, add the following environment variables to the otherSplunk AppDynamics variables.
APPDYNAMICS_APPLICATION_LAMBDA_HANDLER
= Lambda handlerAPPDYNAMICS_IS_ESM_ENABLE
=true
In the Layers section, add theSplunk AppDynamics layer. Give it the name
appdynamics-lambda-extension
, and the Version ARN value as shown below:arn:aws:lambda:us-west-<###>:layer:appdynamics-lambda-nodejs-<###>
JSIn the Runtime settings section, update the lambda handler to Splunk AppDynamics specific handler,
appdynamics-lambda-nodejs-tracer.handler
.appdynamics-lambda-nodejs-tracer.handler
JSIn the Configuration section, add the following environment variables to the otherSplunk AppDynamics variables.
APPDYNAMICS_APPLICATION_LAMBDA_HANDLER
= Lambda handlerAPPDYNAMICS_IS_ESM_ENABLE
=false
In the Layers section, add theSplunk AppDynamics layer. Give it the name
appdynamics-lambda-extension
, and the Version ARN value as shown below.arn:aws:lambda:us-west-<###>:layer:appdynamics-lambda-nodejs-<###>
JSIn the Runtime settings section, update the lambda handler to Splunk AppDynamics specific handler,
appdynamics-lambda-nodejs-tracer.handler
.appdynamics-lambda-nodejs-tracer.handler
JSIn the Configuration section, add the following environment variables to the otherSplunk AppDynamics variables.
APPDYNAMICS_APPLICATION_LAMBDA_HANDLER
= Lambda handlerAPPDYNAMICS_IS_ESM_ENABLE
=false
, (true
, if you are using ES Module)
By default, the Node.js Serverless Tracer automatically discovers exit calls passed over HTTP or inter-AWS Lambda calls. You need to create an exit call if your function does not use these protocols. See Node.js Serverless Tracer API.
Next Steps
You are ready to Verify the Serverless Tracer Instrumentation.
You can also Integrate the Node.js Serverless Tracer with End User Monitoring or use the Node.js Serverless Tracer API.
Amazon Web Services, the AWS logo, AWS, and any other AWS Marks used in these materials are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries.