aws / aws-lambda-runtime-interface-emulator

Apache License 2.0
915 stars 95 forks source link

`tracing` field missing from Lambda extension INVOKE event #109

Open ramosbugs opened 8 months ago

ramosbugs commented 8 months ago

The AWS Lambda Extensions API seems to set the tracing field in the INVOKE event for Lambda extensions even if X-Ray active tracing is disabled for the Lambda function and the requesting service (e.g., API Gateway) doesn't provide tracing headers. It's unclear where the trace ID originates, but I confirmed that it's present even when X-Ray is disabled. When I searched the trace ID in the X-Ray console, it didn't find anything, suggesting that the AWS Lambda service generates a new trace ID when none is provided.

However, the emulator omits this field when the request doesn't include an X-Amzn-Trace-Id header: https://github.com/aws/aws-lambda-runtime-interface-emulator/blob/2057adac54dbd781b51431f775363ec021648c77/lambda/rapi/rendering/rendering.go#L144 https://github.com/aws/aws-lambda-runtime-interface-emulator/blob/2057adac54dbd781b51431f775363ec021648c77/lambda/rapi/model/tracing.go#L30-L33

This behavior breaks compatibility with the AWS Lambda Rust Runtime, which expects the tracing field (cc @calavera): https://github.com/awslabs/aws-lambda-rust-runtime/blob/ab3b809d63fad05369596d17ad4fc449f7035e5e/lambda-extension/src/events.rs#L23

When locally testing Lambda extensions using the Rust runtime and the AWS SAM CLI's sam local command, extensions fail with a "missing field tracing" error. The same extensions work fine in production.

Since this incompatibility is due to a behavioral difference between the AWS Lambda service in production and the emulator, I think it's ultimately a bug in the emulator, rather than the Rust runtime being overly strict.

I'd suggest changing the NewXRayTracing function to generate a trace ID when none is provided, rather than returning nil.