Skip to content

Commit fd5b29f

Browse files
committed
STAC-22297 Added documentation for auto-instrumentation of AWS Lambdas with OTEL.
1 parent 42d8296 commit fd5b29f

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

.gitbook/assets/otel/aws_nodejs_otel_auto_instrumentation.svg

Lines changed: 10 additions & 0 deletions
Loading
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Auto-Instrumenting a NodeJS Lambda
2+
3+
# Introduction
4+
5+
This document guides you through auto-instrumenting NodeJS Lambda functions using OpenTelemetry. Auto-instrumentation simplifies the process of adding observability to your Lambda functions by automatically capturing performance metrics and tracing information.
6+
7+
# **Prerequisites**
8+
9+
Before you begin, ensure you have the following:
10+
11+
- **AWS Lambda function:** The function you want to instrument.
12+
- **OpenTelemetry SDK:** Installed in your Lambda function.
13+
- **OpenTelemetry Collector:** Deployed and configured.
14+
- **SUSE Observability:** An account with SUSE Observability where you'll send your telemetry data.
15+
- **Memory:** Enough memory to run the Lambda’s including the instrumentation.
16+
17+
# Values supplied by the environment
18+
19+
OpenTelemetry relies on various configuration values to function correctly. These values control aspects like data collection, exporting, and communication with backend systems. To make your OpenTelemetry deployment flexible and adaptable to different environments, you can provide these settings through environment variables. This approach offers several benefits:
20+
21+
- **Dynamic Configuration:** Easily adjust settings without code changes.
22+
- **Environment-Specific Settings:** Configure OpenTelemetry differently for development, testing, and production.
23+
- **Secret Management:** Securely store sensitive information like API keys.
24+
25+
For the OpenTelemetry setup described in this documentation, you'll need to define the following environment variables:
26+
27+
- **`VERBOSITY`:** Controls the level of detail in OpenTelemetry logs.
28+
- **`OTLP_API_KEY`:** Authenticates your Lambda function to send data to SUSE Observability.
29+
- **`OTLP_ENDPOINT`:** Specifies the address of your SUSE Observability instance.
30+
- **`OPENTELEMETRY_COLLECTOR_CONFIG_FILE`:** Points to the configuration file for the OpenTelemetry Collector.
31+
- **`AWS_LAMBDA_EXEC_WRAPPER`:** Configures the Lambda execution environment to use the OpenTelemetry handler.
32+
- **`OTLP_INSTR_LAYER_ARN`:** Provides the ARN (Amazon Resource Name) of the OpenTelemetry instrumentation layer, which adds the necessary components for auto-instrumentation.
33+
- **`OTLP_COLLECTOR_LAYER_ARN`:** Provides the ARN of the OpenTelemetry collector layer, which is responsible for receiving, processing, and exporting telemetry data.
34+
35+
**Important Considerations:**
36+
37+
- **GRPC Endpoint:** The `OTLP_ENDPOINT` should specify the gRPC endpoint of your SUSE Observability instance without any `http` or `https` prefix. Use port 443 for secure communication.
38+
- **Region-Specific Layers:** Lambda layers are region-bound. Ensure that the ARNs you use for `OTLP_INSTR_LAYER_ARN` and `OTLP_COLLECTOR_LAYER_ARN` match the AWS region where your Lambda function is deployed.
39+
- **Architecture Matching:** The OpenTelemetry Collector layer is architecture-specific. Choose the correct ARN for your Lambda function's architecture (e.g., `amd64` or `arm64`).
40+
41+
**A complete example: be aware you need to input your own values.**
42+
43+
```yaml
44+
VERBOSITY: "normal"
45+
OTLP_API_KEY: "<your api key for sending data to SUSE Observability here>"
46+
OTLP_ENDPOINT: "<your-dns-name-for-suse-observability-here>:443"
47+
OPENTELEMETRY_COLLECTOR_CONFIG_FILE: "/var/task/collector.yaml"
48+
AWS_LAMBDA_EXEC_WRAPPER: "/opt/otel-handler"
49+
OTLP_INSTR_LAYER_ARN: "arn:aws:lambda:<aws-region>:184161586896:layer:opentelemetry-nodejs-0_11_0:1"
50+
OTLP_COLLECTOR_LAYER_ARN: "arn:aws:lambda:<aws-region>:184161586896:layer:opentelemetry-collector-<amd64|arm64>-0_12_0:1"
51+
```
52+
53+
# The collector.yaml file
54+
55+
OTEL collection configuration sets up how the data collected should be distributed. This is done in the collector.yaml file placed in the src directory where the lambda files can be found. Below is an example collector.yaml file.
56+
57+
```yaml
58+
# collector.yaml in the root directory
59+
# Set an environemnt variable 'OPENTELEMETRY_COLLECTOR_CONFIG_FILE' to
60+
# '/var/task/collector.yaml'
61+
62+
receivers:
63+
otlp:
64+
protocols:
65+
grpc:
66+
http:
67+
68+
exporters:
69+
debug:
70+
verbosity: "${env:VERBOSITY}"
71+
otlp/stackstate:
72+
headers:
73+
Authorization: "SUSEObservability ${env:OTLP_API_KEY}"
74+
endpoint: "${env:OTLP_ENDPOINT}"
75+
76+
service:
77+
pipelines:
78+
traces:
79+
receivers: [otlp]
80+
exporters: [debug, otlp/stackstate]
81+
processors: []
82+
metrics:
83+
receivers: [otlp]
84+
exporters: [debug, otlp/stackstate]
85+
processors: []
86+
```
87+
88+
Be aware this collector is used to send the data over to a next collector which then is used for tail sampling, metric aggregation, etc. before sending data over to SUSE Observability. This second collector also needs to run in the customers environment. See this page for [instructions](../../collector.md).
89+
90+
![AWS Lambda Instrumentation With Opentelemetry](/.gitbook/assets/otel/aws_nodejs_otel_auto_instrumentation.svg)
91+
92+
# Package.json
93+
94+
Make sure to add `"@opentelemetry/auto-instrumentations-node": "^0.55.2",` to `package.json` and execute `npm install` to add the auto-instrumentation client libraries to your NodeJS Lambda.
95+
96+
# Troubleshooting Timeouts
97+
98+
If the addition of the OTEL Lambda layers results in lambdas that time out (checking the logs might indicate that the collector was asked to shut down while still busy, e.g. seeing the following log entry):
99+
100+
```json
101+
{
102+
"level": "info",
103+
"ts": 1736867469.2312617,
104+
"caller": "internal/retry_sender.go:126",
105+
"msg": "Exporting failed. Will retry the request after interval.",
106+
"kind": "exporter",
107+
"data_type": "traces",
108+
"name": "otlp/stackstate",
109+
"error": "rpc error: code = Canceled desc = context canceled",
110+
"interval": "5.125929689s"
111+
}
112+
```
113+
114+
shortly after receiving the instruction to shut down:
115+
116+
```json
117+
{
118+
"level": "info",
119+
"ts": 1736867468.4311068,
120+
"logger": "lifecycle.manager",
121+
"msg": "Received SHUTDOWN event"
122+
}
123+
```
124+
125+
The above indicates that the allocated resources of the lambda are not sufficient to allow execution of the lambda and the additional strain added by the OTEL instrumentation. To remedy this, the memory allocation and lambda timeout settings can be adjusted as necessary to allow the lambda to finish its work, while also allowing the telemetry collection to succeed.
126+
127+
Try modifying the MemorySize and TimeOut properties of the lambdas that are failing:
128+
129+
```yaml
130+
MemorySize: 256
131+
Timeout: 25
132+
```
133+
134+
Note the default memory allocation is 128MB
135+
136+
Note the memory increment is 128MB
137+
138+
Note Timeout is an integer value denoting seconds.
139+
140+
# References
141+
142+
Auto-instrumentation docs → [https://opentelemetry.io/docs/faas/lambda-auto-instrument/](https://opentelemetry.io/docs/faas/lambda-auto-instrument/)
143+
144+
Collector docs → [https://opentelemetry.io/docs/faas/lambda-collector/](https://opentelemetry.io/docs/faas/lambda-collector/)
145+
146+
Github Releases Page (for finding latest ARNs) → https://github.com/open-telemetry/opentelemetry-lambda/releases
147+
148+
OTLP Exporter Configuration → [https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/)

0 commit comments

Comments
 (0)