Datadog APM can create synthetic root spans for requests that pass through Amazon API Gateway to container- or EC2-hosted services. The spans power end-to-end traces, service maps, and sampling based on the gateway itself.
If your API Gateway integrates with AWS Lambda, do not follow the instructions on this page. Datadog Lambda layers already emit inferred API Gateway spans; adding the proxy headers described here can create duplicate or conflicting traces.
Prerequisites
Amazon API Gateway is deployed as a REST API (v1) or HTTP API (v2).
Note: If using HTTP API (v2), context.requestTimeEpoch provides second-level granularity, unlike REST APIs (v1) which provide millisecond precision. This means span duration is approximate.
DD_TRACE_INFERRED_PROXY_SERVICES_ENABLED is set in the application container:
To create inferred spans, API Gateway must pass the following headers to your backend services:
Header
Value
x-dd-proxy
'aws-apigateway'
Note: Single quotes must be included.
x-dd-proxy-request-time-ms
context.requestTimeEpoch
x-dd-proxy-domain-name
context.domainName
x-dd-proxy-httpmethod
context.httpMethod
x-dd-proxy-path
context.path
x-dd-proxy-stage
context.stage
To pass in the required headers, you can use the AWS CDK or AWS Console:
AWS CDK
Add the headers under requestParameters and use $context variables:
constresource=api.root.addResource('myresource');resource.addMethod('ANY',newapigateway.Integration({# other configurationsoptions:{# other optionsrequestParameters:{"integration.request.header.x-dd-proxy":"'aws-apigateway'","integration.request.header.x-dd-proxy-request-time-ms":"context.requestTimeEpoch","integration.request.header.x-dd-proxy-domain-name":"context.domainName","integration.request.header.x-dd-proxy-httpmethod":"context.httpMethod","integration.request.header.x-dd-proxy-path":"context.path","integration.request.header.x-dd-proxy-stage":"context.stage",}}})# other settings here});
AWS Console
In the AWS Management Console, navigate to API Gateway and go to your API’s Resources page.
Go to Integration request and click Edit.
Under Edit integration request, go to URL request headers parameters. Click Add request header parameter.
To create inferred spans, API Gateway must pass the following headers to your backend services:
Header
Value
x-dd-proxy
aws-apigateway
x-dd-proxy-request-time-ms
${context.requestTimeEpoch}000
x-dd-proxy-domain-name
$context.domainName
x-dd-proxy-httpmethod
$context.httpMethod
x-dd-proxy-path
$context.path
x-dd-proxy-stage
$context.stage
Note: context.requestTimeEpoch returns a timestamp in seconds in v2 APIs. Datadog expects milliseconds, so you must multiply it by 1000 by appending 000.
Attach the parameter mapping that injects the headers:
Head-based sampling still applies when using API Gateway tracing. Because the synthetic span becomes the new trace root, update your rules so the service value matches the API Gateway service name shown in Datadog.