The otel-serverless-plugin
is a plugin for the Serverless Framework that automates the configuration of layers and environment variables required to instrument AWS Lambda functions with OpenTelemetry. It is designed to be flexible and highly configurable, allowing different clients and environments to be easily managed.
- ✅ Automatically adds OpenTelemetry layers to Lambda functions.
- ✅ Configures environment variables specific to each Lambda function.
- ✅ Supports custom configurations such as
tenant
,token
,endpoint
, andenv
. - ✅ Works seamlessly with any number of Lambda functions without manual adjustments.
- ✅ Easy to integrate into the Serverless Framework.
-
Add the plugin to your Serverless project
Install the plugin via npm:
npm install --save-dev otel-serverless-plugin
-
Configure your
serverless.yml
Add the plugin to the plugins section in the
serverless.yml
file:plugins: - otel-serverless-plugin
Add the custom.otelLayerPlugin
section to your serverless.yml
file to customize the configuration:
custom:
otelLayerPlugin:
tenant: "production-tenant"
token: "super-secret-token"
endpoint: "https://otel-endpoint:4318/"
env: prod
region: us-east-1
These configurations will automatically be applied to all Lambda functions' environment variables.
Here's a complete example of how to use the plugin in your serverless.yml
:
service: otel-service
plugins:
- otel-serverless-plugin
custom:
otelLayerPlugin:
tenant: "production-tenant"
token: "super-secret-token"
endpoint: "https://otel-endpoint:4318/"
env: prod
region: us-east-1
provider:
name: aws
runtime: nodejs18.x
region: us-east-1
environment:
STAGE: ${opt:stage, 'dev'}
functions:
myLambdaFunction:
handler: handler.main
anotherLambdaFunction:
handler: another.handler
-
Layer Addition:
- The plugin automatically adds the following layers to your Lambda functions:
- OpenTelemetry layer:
opentelemetry-nodejs
- Logs collector layer:
collector-logs-js
- OpenTelemetry layer:
- You can also specify custom layers in the configuration.
- The plugin automatically adds the following layers to your Lambda functions:
-
Environment Variable Configuration:
- The plugin configures environment variables such as:
{ "TENANT_ID": "production-tenant", "API_TOKEN": "super-secret-token", "OTEL_EXPORTER_OTLP_ENDPOINT": "https://otel-endpoint:4318/", "OTEL_SERVICE_NAME": "myLambdaFunction", "OTEL_RESOURCE_ATTRIBUTES": "service.name=myLambdaFunction,environment=prod" }
- The plugin configures environment variables such as:
-
Environment-Based Configuration:
- Values like
tenant
,token
,env
,region
, andlayers
can be customized for each client or environment directly in theserverless.yml
.
- Values like
Parameter | Description | Default Value |
---|---|---|
tenant |
Unique identifier for the client or environment. | "default-tenant" |
token |
Authentication token, if required. |
"" (empty) |
endpoint |
OpenTelemetry Collector URL for telemetry data export. | "http://default-endpoint:4318/" |
env |
Current environment, such as prod , dev , or qa . |
"dev" |
region |
AWS Lambda region. | "us-east-1" |
layers |
Custom layers for OpenTelemetry and logs. | Default layers for metrics and logs: arn:aws:lambda:${region}:184161586896:layer:opentelemetry-nodejs-0_11_0:1 arn:aws:lambda:${region}:204595508824:layer:collector-logs-js:14
|
-
Package the Service:
serverless package
-
Verify the Result:
- Confirm that the layers and environment variables were added to the Lambdas in the generated
.serverless/cloudformation-template-update-stack.json
file.
- Confirm that the layers and environment variables were added to the Lambdas in the generated
-
Deploy to AWS:
serverless deploy
-
Validate in the AWS Console:
- Go to the AWS Lambda Console.
- Verify that the layers and environment variables are correctly configured.
The plugin will use default values defined in the code:
-
tenant
:"default-tenant"
-
token
:""
-
endpoint
:"http://default-endpoint:4318/"
-
env
:"dev"
-
region
:"us-east-1"
-
layer
:arn:aws:lambda:${region}:184161586896:layer:opentelemetry-nodejs-0_11_0:1
arn:aws:lambda:${region}:204595508824:layer:collector-logs-js:17
Yes, as long as the layers are compatible with the runtime used.
If you're using npm, simply update the version:
npm install --save-dev otel-serverless-plugin@latest
Feel free to open issues or pull requests in the repository to suggest improvements or fix bugs.
This project is licensed under the MIT License.