Instrumenting Node.js code in AWS Lambda
Lambda integrates with AWS X-Ray to help you trace, debug, and optimize Lambda applications. You can use X-Ray to trace a request as it traverses resources in your application, which may include Lambda functions and other AWS services.
To send tracing data to X-Ray, you can use one of two SDK libraries:
-
AWS Distro for OpenTelemetry (ADOT)
– A secure, production-ready, AWS-supported distribution of the OpenTelemetry (OTel) SDK.
Sections
- Using ADOT to instrument your Node.js functions
- Using the X-Ray SDK to instrument your Node.js functions
- Activating tracing with the Lambda console
- Activating tracing with the Lambda API
- Activating tracing with AWS CloudFormation
- Interpreting an X-Ray trace
- Storing runtime dependencies in a layer (X-Ray SDK)
Using ADOT to instrument your Node.js functions
ADOT provides fully managed Lambda Custom configuration for the ADOT Collector on Lambda
For Node.js runtimes, you can add the AWS managed Lambda layer for ADOT
Javascript to automatically instrument your functions. For detailed instructions on how to
add this layer, see AWS
Distro for OpenTelemetry Lambda Support for JavaScript
Using the X-Ray SDK to instrument your Node.js functions
To record details about calls that your Lambda function makes to other resources in your application,
you can also use the AWS X-Ray SDK for Node.js. To get the SDK, add the aws-xray-sdk-core package to
your application's dependencies.
Example blank-nodejs/package.json
{ "name": "blank-nodejs", "version": "1.0.0", "private": true, "devDependencies": { "aws-sdk": "2.631.0", "jest": "25.4.0" }, "dependencies": {"aws-xray-sdk-core": "1.1.2"}, "scripts": { "test": "jest" } }
To instrument AWS SDK clients, wrap the aws-sdk library with the captureAWS
method.
Example blank-nodejs/function/index.js
const AWSXRay = require('aws-xray-sdk-core') const AWS = AWSXRay.captureAWS(require('aws-sdk'))// Create client outside of handler to reuse const lambda = new AWS.Lambda() // Handler exports.handler = async function(event, context) { event.Records.forEach(record => { ...
The Lambda runtime sets some environment variables to configure the X-Ray SDK. For example, Lambda sets
AWS_XRAY_CONTEXT_MISSING to LOG_ERROR to avoid throwing runtime errors from the
X-Ray SDK. To set a custom context missing strategy, override the environment variable in your function
configuration to have no value, and then you can set the context missing strategy programmatically.
Example initialization code
const AWSXRay = require('aws-xray-sdk-core'); // Configure the context missing strategy to do nothing AWSXRay.setContextMissingStrategy(() => {});
For more information, see Activating tracing with the Lambda console
To toggle active tracing on your Lambda function with the console, follow these steps: To turn on active tracing Open the Functions page Choose a function. Choose Configuration and then choose Monitoring and operations tools. Choose Edit. Under X-Ray, toggle on Active tracing. Choose Save. Configure tracing on your Lambda function with the AWS CLI or AWS SDK, use the following API operations: Tracing mode is part of the version-specific configuration when you publish a version of your function.
You can't change the tracing mode on a published version. To activate tracing on an Example function-inline.yml For an AWS Serverless Application Model (AWS SAM) Example template.yml Your function needs permission to upload trace data to X-Ray. When you activate tracing in the Lambda
console, Lambda adds the required permissions to your function's execution role After you've configured active tracing, you can observe specific requests
through your application. The
X-Ray doesn't trace all requests to your application. X-Ray applies a sampling algorithm
to ensure that tracing is efficient, while still providing a representative sample of all requests. The sampling rate is
1 request per second and 5 percent of additional requests. You cannot configure the X-Ray sampling rate for your functions. When using active tracing, Lambda records 2 segments per trace, which creates
two nodes on the service graph. The following image highlights these two nodes for the primary function from the
error processor example above. The first node on the left represents the Lambda service, which receives the invocation request. The second
node represents your specific Lambda function. The following example shows a trace with these two segments. Both
are named my-function, but one has an origin of This example expands the function segment to show its three subsegments: Initialization – Represents time spent loading your function and
running
Invocation – Represents the time spent running your handler code. Overhead – Represents the time the Lambda runtime spends preparing
to handle the next event. You can also instrument HTTP clients, record SQL queries, and create custom subsegments
with annotations and metadata. For more information, see the You can use X-Ray tracing for free each month up to a certain limit as part of the AWS Free Tier. Beyond that threshold, X-Ray charges for trace storage and
retrieval. For more information, see AWS X-Ray pricingActivating tracing with the Lambda API
aws lambda update-function-configuration --function-name my-function \
--tracing-config Mode=ActiveActivating tracing with AWS CloudFormation
AWS::Lambda::Function resource in an AWS CloudFormation template, use the
TracingConfig property.Resources:
function:
Type: TracingConfig:
Mode: Active
...AWS::Serverless::Function resource, use the Tracing
property.Resources:
function:
Type: Tracing: Active
...Interpreting an X-Ray trace
AWS::Lambda and the other has
origin AWS::Lambda::Function.
Storing runtime dependencies in a layer (X-Ray SDK)
If you use the X-Ray SDK to instrument AWS SDK clients your function code, your deployment package can become quite large. To avoid uploading runtime dependencies every time you update your function code, package the X-Ray SDK in a AWS::Serverless::LayerVersion resource that stores the AWS X-Ray SDK for Node.js.
Example template.yml
Resources: function: Type: Layers: - !Ref libs...libs: Type: blank-nodejssample application.