Other execution environmentsedit
AWS Lambda Supportedit
AWS Lambda functions can be instrumented with OpenTelemetry and monitored with Elastic Observability.
To get started, follow the official AWS Distro for OpenTelemetry Lambda getting started documentation and configure the OpenTelemetry Collector to output traces and metrics to your Elastic cluster.
Instrumenting AWS Lambda Java functionsedit
For a better startup time, we recommend using SDK-based instrumentation, i.e. manual instrumentation of the code, rather than auto instrumentation.
To instrument AWS Lambda Java functions, follow the official AWS Distro for OpenTelemetry Lambda Support For Java.
Noteworthy configuration elements:
-
AWS Lambda Java functions should extend
com.amazonaws.services.lambda.runtime.RequestHandler
,public class ExampleRequestHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) { // add your code ... } }
-
When using SDK-based instrumentation, frameworks you want to gain visibility of should be manually instrumented
-
The below example instruments OkHttpClient with the OpenTelemetry instrument io.opentelemetry.instrumentation:opentelemetry-okhttp-3.0:1.3.1-alpha
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTracing; OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(OkHttpTracing.create(GlobalOpenTelemetry.get()).newInterceptor()) .build();
-
-
The configuration of the OpenTelemetry Collector, with the definition of the Elastic Observability endpoint, can be added to the root directory of the Lambda binaries (e.g. defined in
src/main/resources/opentelemetry-collector.yaml
)# Copy opentelemetry-collector.yaml in the root directory of the lambda function # Set an environment variable 'OPENTELEMETRY_COLLECTOR_CONFIG_FILE' to '/var/task/opentelemetry-collector.yaml' receivers: otlp: protocols: http: grpc: exporters: logging: loglevel: debug otlp/elastic: # Elastic APM server https endpoint without the "https://" prefix endpoint: "${ELASTIC_OTLP_ENDPOINT}" headers: # Elastic APM Server secret token Authorization: "Bearer ${ELASTIC_OTLP_TOKEN}" service: pipelines: traces: receivers: [otlp] exporters: [logging, otlp/elastic] metrics: receivers: [otlp] exporters: [logging, otlp/elastic] logs: receivers: [otlp] exporters: [logging, otlp/elastic]
-
Configure the AWS Lambda Java function with:
-
Function
layer: The latest AWS
Lambda layer for OpenTelemetry (e.g.
arn:aws:lambda:eu-west-1:901920570463:layer:aws-otel-java-wrapper-ver-1-2-0:1
) -
TracingConfig
/ Mode set toPassTrough
-
FunctionConfiguration
/ Timeout set to more than 10 seconds to support the longer cold start inherent to the Lambda Java Runtime -
Export the environment variables:
-
AWS_LAMBDA_EXEC_WRAPPER="/opt/otel-proxy-handler"
for wrapping handlers proxied through the API Gateway (see here) -
OTEL_PROPAGATORS="tracecontext, baggage"
to override the default setting that also enables X-Ray headers causing interferences between OpenTelemetry and X-Ray -
OPENTELEMETRY_COLLECTOR_CONFIG_FILE="/var/task/opentelemetry-collector.yaml"
to specify the path to your OpenTelemetry Collector configuration
-
-
Function
layer: The latest AWS
Lambda layer for OpenTelemetry (e.g.
Instrumenting AWS Lambda Java functions with Terraformedit
We recommend using an infrastructure as code solution like Terraform or Ansible to manage the configuration of your AWS Lambda functions.
Here is an example of AWS Lambda Java function managed with Terraform and the AWS Provider / Lambda Functions:
- Sample Terraform code: https://github.com/cyrille-leclerc/my-serverless-shopping-cart/tree/main/checkout-function/deploy
- Note that the Terraform code to manage the HTTP API Gateway (here) is copied from the official OpenTelemetry Lambda sample here
Instrumenting AWS Lambda Node.js functionsedit
For a better startup time, we recommend using SDK-based instrumentation for manual instrumentation of the code rather than auto instrumentation.
To instrument AWS Lambda Node.js functions, see AWS Distro for OpenTelemetry Lambda Support For JavaScript.
The configuration of the OpenTelemetry Collector, with the definition of the Elastic Observability endpoint, can be added to the root directory of the Lambda binaries: src/main/resources/opentelemetry-collector.yaml
.
# Copy opentelemetry-collector.yaml in the root directory of the lambda function # Set an environment variable 'OPENTELEMETRY_COLLECTOR_CONFIG_FILE' to '/var/task/opentelemetry-collector.yaml' receivers: otlp: protocols: http: grpc: exporters: logging: loglevel: debug otlp/elastic: # Elastic APM server https endpoint without the "https://" prefix endpoint: "${ELASTIC_OTLP_ENDPOINT}" headers: # Elastic APM Server secret token Authorization: "Bearer ${ELASTIC_OTLP_TOKEN}" service: pipelines: traces: receivers: [otlp] exporters: [logging, otlp/elastic] metrics: receivers: [otlp] exporters: [logging, otlp/elastic] logs: receivers: [otlp] exporters: [logging, otlp/elastic]
Environment-specific configuration parameters can be conveniently passed in as environment variables: |
Configure the AWS Lambda Node.js function:
-
Function
layer: The latest AWS
Lambda layer for OpenTelemetry. For example,
arn:aws:lambda:eu-west-1:901920570463:layer:aws-otel-nodejs-ver-0-23-0:1
) -
TracingConfig
/ Mode set toPassTrough
-
FunctionConfiguration
/ Timeout set to more than 10 seconds to support the cold start of the Lambda JavaScript Runtime -
Export the environment variables:
-
AWS_LAMBDA_EXEC_WRAPPER="/opt/otel-handler"
for wrapping handlers proxied through the API Gateway. See enable auto instrumentation for your lambda-function. -
OTEL_PROPAGATORS="tracecontext"
to override the default setting that also enables X-Ray headers causing interferences between OpenTelemetry and X-Ray -
OPENTELEMETRY_COLLECTOR_CONFIG_FILE="/var/task/opentelemetry-collector.yaml"
to specify the path to your OpenTelemetry Collector configuration. -
OTEL_TRACES_SAMPLER="AlwaysOn"
define the required sampler strategy if it is not sent from the caller. Note thatAlways_on
can potentially create a very large amount of data, so in production set the correct sampling configuration, as per the specification.
-
Instrumenting AWS Lambda Node.js functions with Terraformedit
To manage the configuration of your AWS Lambda functions, we recommend using an infrastructure as code solution like Terraform or Ansible.
Here is an example of AWS Lambda Node.js function managed with Terraform and the AWS Provider / Lambda Functions:
Next stepsedit
- Collect metrics
- Add Resource attributes
- Learn about the limitations of this integration