OpenTelemetry is an open source observability framework that provides IT teams with standardized protocols and tools for collecting and routing telemetry data.
This page discusses using OpenTelemetry with Datadog Serverless Monitoring for AWS Lambda. For more information, including how to use OpenTelemetry in non-serverless environments, see OpenTelemetry in Datadog.
The Datadog tracing library, which is included in the Datadog Lambda Extension upon installation, accepts custom spans and traces created with OpenTelemetry-instrumented code, processes the telemetry, and sends it to Datadog.
You can use this approach if, for example, your main goal is to code has already been instrumented with the OpenTelemetry API. This means you can maintain vendor-neutral instrumentation of all your services, while still taking advantage of Datadog’s native implementation, tagging, and features.
To instrument AWS Lambda with the OpenTelemetry API, set the environment variable DD_TRACE_OTEL_ENABLED to true in your Lambda function, and see Custom instrumentation with the OpenTelemetry API for runtime-specific instructions.
This approach is analogous to OLTP Ingest in the Datadog Agent. It is recommended in situations where tracing support may not be available for your runtime (for example, Rust or PHP).
Note: Sending custom metrics from the OTLP endpoint in the extension is not supported.
Tell OpenTelemetry to export spans to the Datadog Lambda Extension. Then, add OpenTelemetry’s instrumentation for AWS Lambda.
fromopentelemetry.instrumentation.botocoreimportBotocoreInstrumentorfromopentelemetry.instrumentation.aws_lambdaimportAwsLambdaInstrumentorfromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.exporter.otlp.trace_exporterimportOTLPExporterfromopentelemetry.sdk.trace.exportimportSimpleSpanProcessorfromopentelemetry.resourceimportResourcefromopentelemetry.semconv.resourceimport(SERVICE_NAME,SemanticResourceAttributes,)# Create a TracerProvidertracer_provider=TracerProvider(resource=Resource.create({SERVICE_NAME:<YOUR_SERVICE_NAME>}))# Add a span processor with an OTLP exportertracer_provider.add_span_processor(SimpleSpanProcessor(OTLPExporter(endpoint="http://localhost:4318/v1/traces")))# Register the providertrace.set_tracer_provider(tracer_provider)# Instrument AWS SDK and AWS LambdaBotocoreInstrumentor().instrument(tracer_provider=tracer_provider)AwsLambdaInstrumentor().instrument(tracer_provider=tracer_provider)
Modify serverless.yml to apply instrumentation at runtime, add the Datadog Extension v53+, and enable OpenTelemetry in the Datadog Extension with the environment variable DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT set to localhost:4318 (for HTTP) or DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_GRPC_ENDPOINT set to localhost:4317 (for gRPC). Do not add the Datadog tracing layer.
service:<YOUR_SERVICE_NAME>provider:name:awsregion:<YOUR_REGION>runtime:python3.8 # or the Python version you are usingenvironment:DD_API_KEY:${env:DD_API_KEY}DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT:localhost:4318layers:- arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension:53functions:python:handler:handler.handlerenvironment:INSTRUMENTATION_FLAG:true
Then, update your Python code accordingly. For example, in handler.py:
importosdefhandler(event,context):ifos.environ.get('INSTRUMENTATION_FLAG')=='true':# Perform instrumentation logic hereprint("Instrumentation is enabled")# Your normal handler logic hereprint("Handling the event")
# serverless.ymlservice:<YOUR_SERVICE_NAME>provider:name:awsregion:<YOUR_REGION>runtime:nodejs18.x# or the Node.js version you are usingenvironment:DD_API_KEY:${env:DD_API_KEY}DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT:localhost:4318layers:- arn:aws:lambda:sa-east-1:464622532012:layer:Datadog-Extension:53functions:node:handler:handler.handlerenvironment:NODE_OPTIONS:--require instrument