This page describes how to use the Node.js Serverless Tracer with AWS Lambda functions during development. The preferred method is to use the AppDynamics AWS Lambda Extension. See Use the 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:

This document contains links to Amazon Web Services (AWS) documentation. AppDynamics makes 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

To instrument your lambda we recommend the AWS Lambda Extension. You can also install the Node.js Serverless Tracer using these steps:

  1. Download the Node.js Serverless Tracer
  2. Add the Require Statement
  3. Instrument the Function with the Node.js Serverless Tracer

Download the Node.js Serverless Tracer

To install the tracer, navigate to the directory where the package.json file resides. Run the following npm command:

npm install appdynamics-lambda-tracer --save
JS

The command downloads the tracer dependency into a node_modules/ folder.

Add the Require Statement

To add the tracer to your application, add a require statement in your application code's primary file that includes the AWS Lambda handler function:

const tracer = require('appdynamics-lambda-tracer');
JS

Remember to add the tracer's require statement before any other require statement. 

Instrument the Function with the Node.js Serverless Tracer

Initialize the Node.js Serverless Tracer below the require statement, by calling the tracer.init method.

To 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:

const tracer = require('appdynamics-lambda-tracer'); 
//Initialize the tracer
tracer.init();
 
// 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

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.