AWS Lambda Tracing
Configuration¶
Currently, AWS Lambda supports two methods for accessing trace information:
- DDTrace
- Opentelemetry
DDTrace Integration¶
Prepare Lambda Layer¶
Two layers need to be added:
- DataDog Layer: For trace instrumentation
- DataKit Layer: For receiving observability data from the
DataDog Layer
Add DataKit Layer¶
Reference: AWS Lambda Extension > Add DataKit Layer
Add DataDog Layer¶
- AWS Global Regions
Choose the appropriate ARN:
# Use this format for x86-based Lambda deployed in AWS commercial regions
arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-<RUNTIME>:118
# Use this format for arm64-based Lambda deployed in AWS commercial regions
arn:aws:lambda:<AWS_REGION>:464622532012:layer:Datadog-<RUNTIME>-ARM:118
# Use this format for x86-based Lambda deployed in AWS GovCloud regions
arn:aws-us-gov:lambda:<AWS_REGION>:002406178527:layer:Datadog-<RUNTIME>:118
# Use this format for arm64-based Lambda deployed in AWS GovCloud regions
arn:aws-us-gov:lambda:<AWS_REGION>:002406178527:layer:Datadog-<RUNTIME>-ARM:118
Replace <AWS_REGION> with a valid AWS region, such as us-east-1. Available Python38, Python39, Python310, Python311, Python312, Python313.
- AWS China Regions
Since Datadog does not maintain the datadog-lambda-python layer in China regions, we use pip to install the datadog-lambda package and its dependencies locally into your function project folder.
datadog-lambda depends on ddtrace, and ddtrace uses native extensions; therefore, it must be installed and compiled in a Linux environment with the correct architecture (x86_64 or arm64).
Set the function's "Runtime Handler" to datadog_lambda.handler.handler
Environment Variables Configuration¶
- DD_TRACE_ENABLED: Enable distributed tracing, default is true
- DD_LAMBDA_HANDLER: Set to the original handler, e.g.,
lambda_function.lambda_handler - DD_TRACE_AGENT_URL:
http://localhost:9529 - DD_TRACE_DEBUG: Log output, default is off
- ENV_DATAWAY: The DataWay address for reporting data (token is obtained from the workspace)
After configuration, click Test, and then you can log in to the platform to view trace data
Opentelemetry Integration¶
Prepare Lambda Layer¶
Two layers need to be added: - Opentelemetry Layer: For trace instrumentation - DataKit Layer: For receiving observability data from Opentelemetry
Add DataKit Layer¶
Reference: AWS Lambda Extension > Add DataKit Layer
Add Opentelemetry Layer¶
Using the OpenTelemetry automatic instrumentation method enables zero-intrusion Python code for trace tracing. Here, OpenTelemetry can be attached as a layer inside the Lambda function.
AWS Ningxia nodes require manual building of the OpenTelemetry layer. For other nodes, the corresponding ARN can be used directly. This section mainly introduces the manual building method:
- Download the source code
- Build
Before building, ensure relevant AWS tools are installed. Refer to the repository's README file. Assuming the tools are installed, execute the following command to build:
Part of the build log is as follows:
running...
Invoked with: -r cn-northwest-1
sam building...
run.sh: Starting sam build.
Starting Build inside a container
Building layer 'OTelLayer'
For container layer build, first compatible runtime is chosen as build target for container.
Fetching public.ecr.aws/sam/build-python3.9:latest-x86_64 Docker container image......
Mounting /home/liurui/code/opentelemetry-lambda/python/src/otel as /tmp/samcli/source:ro,delegated, inside runtime container
Successfully created/updated stack - otel-stack in cn-northwest-1
OTel Lambda layer ARN:
arn:aws-cn:lambda:cn-northwest-1:888888888:layer:otel-layer:2
After a successful build, it is automatically uploaded to the AWS Layer as otel-layer. Copy this ARN for subsequent operations.
Environment Variables Configuration¶
Add the following to the corresponding function configuration
# otel lambda handler
AWS_LAMBDA_EXEC_WRAPPER = /opt/python/otel-handler
# Gateway address
ENV_DATAWAY = http://open****?token=xxxxxxx
# otel reporting address, no need to adjust
OTEL_EXPORTER_OTLP_ENDPOINT = http://localhost:9529/otel
- Test
Click the Test button to test. After success, you can log in to the platform to view trace data.