AWS Lambda
This guide walks you through instrumenting AWS Lambda with OpenTelemetry and Middleware. These instructions can also be found on the Installation page in your Middleware Account.
Introduction
OpenTelemetry provides a standardized way to instrument applications for collecting telemetry data. However, implementing OpenTelemetry in AWS Lambda presents unique challenges due to the serverless nature of the environment. This guide aims to create a seamless OpenTelemetry experience for Lambda users for different programming languages
Key Challenges
- Early Function Exit: Lambda functions may exit before traces can be exported.
- Container Freezing: AWS Lambda may freeze the container if no new requests arrive, suspending all processes.
- Performance Concerns: The function should remain as lightweight as possible for optimal Lambda performance.
Prerequisites
- Node.js 18 +:
Features Supported
Traces | Metrics | App Logs | Custom Logs | Profiling |
---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✖ |
Installation
1 Add OpenTelemetry (OTel) Layer to Lambda
a: Access the OTel Layer
- Navigate to the OpenTelemetry Lambda Repository.
- Locate the appropriate layer for your environment.
b: Match AWS Region and CPU Architecture
Copy the Amazon Resource Name (ARN) and set the configuration to match your Lambda function’s region and CPU architecture. Example ARN:
arn:aws:lambda:<region>:184161586896:layer:opentelemetry-nodejs-0_9_0:4
For example, for the us-east-1
region:
arn:aws:lambda:us-east-1:184161586896:layer:opentelemetry-nodejs-0_9_0:4
c: Add the OTel Layer to Lambda
- In the AWS Console, navigate to Lambda → Functions → your specific Lambda function.
- Go to the Layers section and click on Add Layer.
d: Specify the ARN
- In the Choose Layer section, specify the ARN from Step 1b.
- Click Add to save the layer.
e: Verify and Add OTel Layer
Review the added layer and ensure it matches the ARN. Finally, click Add to complete the process.
2 Set Environment Variables
Set the following environment variables in your Lambda function configuration:
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler NODE_OPTIONS=--require ./lambda-config.js OTEL_SERVICE_NAME=your-service-name OTEL_EXPORTER_OTLP_ENDPOINT=https://<MW_UID>.middleware.io:443 OTEL_RESOURCE_ATTRIBUTES=mw.account_key=<MW_API_KEY> OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION=true OTEL_PROPAGATORS=tracecontext
3 Create lambda-config.js
Create a file named lambda-config.js
in your Lambda function's root directory with the following content:
const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base"); const { OTLPTraceExporter, } = require("@opentelemetry/exporter-trace-otlp-proto"); global.configureTracerProvider = (tracerProvider) => { console.log("Configuring tracer provider with Simple Span Processor"); const spanProcessor = new SimpleSpanProcessor(new OTLPTraceExporter()); tracerProvider.addSpanProcessor(spanProcessor); };
This configuration overrides the default Batch Span Processor with a Simple Span Processor, ensuring immediate processing and exporting of spans.
4 Enable X-Ray Tracing
Ensure X-Ray tracing is enabled for your Lambda function to get the full benefit of the instrumentation.
5 Add Custom Instrumentation (Optional)
If you need to add custom spans or attributes, you can use the OpenTelemetry API as follows:
const { trace } = require("@opentelemetry/api"); const tracer = trace.getTracer("{SERVICE-NAME}", "1.0.0"); exports.handler = async (event, context) => { const span = tracer.startSpan("lambdaHandler"); try { span.setAttribute("event", JSON.stringify(event));
Troubleshooting
If you encounter missing spans:
- Verify that all environment variables are correctly set.
- Check that the
lambda-config.js
file is in the root directory of your Lambda function. - Ensure that the OpenTelemetry layer is correctly added to your function.
- Review CloudWatch logs for any error messages related to OpenTelemetry.
Verify Installation
Deploy and Test your application .Check your Middleware account for traces by navigating to APM -> Services and finding the name of your function in the services list.
Need assistance or want to learn more about Middleware? Contact our support team in Slack.