Skip to content

Demo OpenTelemetry instrumentation of AWS Lambda Java functions

Notifications You must be signed in to change notification settings

cyrille-leclerc/my-serverless-shopping-cart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Instrumenting AWS Lambda Java functions with OpenTelemetry SDK and Elastic Observability

Getting started

ℹ️ This tutorial explains how to instrument Java lambda functions with the OpenTelemetry SDK (ie manual instrumentation of the code). For auto instrumentation via the OpenTelemetry Auto Instrumentation Java Agent, see AWS Distro for OpenTelemetry Lambda Support For Java (Auto-instrumentation Agent)

public class ShoppingCartCheckoutRequestHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
        // add your code ...
    }
}
    
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTracing;

OkHttpClient httpClient = new OkHttpClient.Builder()
        .addInterceptor(OkHttpTracing.create(GlobalOpenTelemetry.get()).newInterceptor())
        .build();
  • Add in the root directory of your lambda function (e.g. src/main/resources/opentelemetry-collector.yaml) the configuration of the AWS Distro for OpenTelemetry Collector to export the data to Elastic Observability
# 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:
      # 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]
  • Configure you AWS Lambda 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 to PassTrough
    • 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
  • Deploy your Lambda function, test it and visualize it in Elastic Observability's APM view:

  • Verify in Elastic Observability / APM

    • Example distributed trace chaining 2 lambda functions

    • Example service map chaining 2 lambda functions

Terraform Example

See

Learn more

https://docs.aws.amazon.com/lambda/latest/dg/java-package.html

About

Demo OpenTelemetry instrumentation of AWS Lambda Java functions

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published