Serverless Lambda function integration

AWS Lambda lets you run code without provisioning or managing servers. This deployment model is sometimes also referred to as "Serverless" or "Function as a Service" (FaaS). Technically, a Lambda function runs in an application on a container managed by AWS. This lets you focus on writing code without worrying about the underlying application or infrastructure. Lambda functions are ephemeral. This means that the underlying container can be suspended or recycled when there's no request pending. Also, Lambda functions can only process one request at a time. When there are (n) concurrent requests, (n) containers are created. All this is completely transparent to the developer.

Lambda functions written in Node.js can be monitored with the OneAgent NPM module.

Note: This feature is currently available within a Preview. Also read the Disclaimer.

Setting up Lambda monitoring

  1. From the Dynatrace web interface, select Deploy Dynatrace from the navigation menu.
  2. Click Set up Serverless integration.
  3. Select Node.js on the following page.
  4. Follow the displayed instructions

Optimizing the npm module for Lambda

The Dynatrace npm module contains instrumentation code for a variety of Node.js versions. As a Lambda function is always configured to run a specific version of Node.js, it makes sense to only bundle those parts of OneAgent that are applicable to this version.

To do this, go to the base directory of your application and execute:

# To prepare the package for Node.js 8 run
npx dt-oneagent-tailor --AwsLambdaV8

# To prepare the package for Node.js 10 run
npx dt-oneagent-tailor --AwsLambdaV10

# To prepare the package for Node.js 12 run
npx dt-oneagent-tailor --AwsLambdaV12

# To get a list of all options run
npx dt-oneagent-tailor

(npx is bundled with recent versions of npm - if it isn't yet installed, install it with npm install -g npx)

Transactional tracing of Lambda invocations

Lambda functions can be triggered by a variety of so called "Event Sources." For end-to-end monitoring, events coming from API Gateway or AWS SDK are the most relevant. Such calls will be traced through, continuing the transaction from the calling application.

Lambda calls from other event sources (like Alexa) will create a new transaction for each invocation.

Configuring the AWS API Gateway

The API Gateway needs to be configured to pass through the Dynatrace tag. To accomplish that, enable 'Use Lambda Proxy Integration' on the 'Integration Request' configuration page of the API Gateway. If the API Gateway is configured from the Lambda configuration page, this setting will be enabled by default. AWS Lambda proxy integration setting for API Gateway

Tracing calls via AWS SDK

Starting with OneAgent version 1.147, SDK calls from Node.js and Java to Lambda are fully supported and traced end-to-end.

Known issues

Handler functions in sub-folders

Handler functions which are exported by Javascript files in sub-folders might fail with a Runtime.MalformedHandlerName exception (Bad handler) error message.

Given the handler definition lib/index.handler, set up Serverless integraton screen proposes to change the handler definition to node_modules/@dynatrace/oneagent.lib/index$handler. Due to changes in AWS Lambda runtime handler definition decomposition algorithm, Lambda runtime wrongly decomposes the handler definition and fails to load the handler function.

The issue can be avoided with the following procedure:

  1. Add the environment variable DT_LAMBDA_HANDLER, where its value is the function handler definition. In the above example, DT_LAMBDA_HANDLER receives the lib/index.handler value.

  2. Change the function definition to node_modules/@dynatrace/oneagent/index.handler (instead of the proposed node_modules/@dynatrace/oneagent.lib/index$handler).

The remaining procedure described in set up Serverless integraton screen (installing the @dynatrace/oneagent npm module and adding the DT_LAMBDA_OPTIONS environment variable) remain unchanged.