From 52635f5f2fdd3aa81b7d8bdc7eceb9a37482d008 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 26 Sep 2026 23:36:30 +0200 Subject: [PATCH] Pass _X_AMZN_TRACE_ID to PHP-FPM The function and console runtimes set it on every invocation, and the documentation says Bref exposes it on every invocation, but PHP-FPM workers did not receive it. Claude-Session: https://claude.ai/code/session_01Awk33iZ9mDSg1xgSAPkgGK --- docs/runtimes/fpm-runtime.mdx | 2 +- src/FpmRuntime/FpmHandler.php | 3 ++- tests/FpmRuntime/FpmHandlerTest.php | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/runtimes/fpm-runtime.mdx b/docs/runtimes/fpm-runtime.mdx index 2f868aa6c..fdc6b092e 100644 --- a/docs/runtimes/fpm-runtime.mdx +++ b/docs/runtimes/fpm-runtime.mdx @@ -70,7 +70,7 @@ Here is an example to retrieve it: $lambdaContext = json_decode($_SERVER['LAMBDA_INVOCATION_CONTEXT'], true); ``` -The request ID of the invocation is also available directly in `$_SERVER['LAMBDA_REQUEST_ID']`, like in [the other runtimes](./runtimes-details.mdx#environment-variables). +The request ID of the invocation is also available directly in `$_SERVER['LAMBDA_REQUEST_ID']`, and the X-Ray trace ID in `$_SERVER['_X_AMZN_TRACE_ID']`, like in [the other runtimes](./runtimes-details.mdx#environment-variables). ### Request context diff --git a/src/FpmRuntime/FpmHandler.php b/src/FpmRuntime/FpmHandler.php index c1b0049fd..d6a0fb7a7 100644 --- a/src/FpmRuntime/FpmHandler.php +++ b/src/FpmRuntime/FpmHandler.php @@ -253,8 +253,9 @@ private function eventToFastCgiRequest(HttpRequestEvent $event, Context $context $request->setCustomVar('PATH_INFO', $event->getPath()); $request->setCustomVar('QUERY_STRING', $event->getQueryString()); $request->setCustomVar('LAMBDA_INVOCATION_CONTEXT', json_encode($context, JSON_THROW_ON_ERROR)); - // Same variable as in the other runtimes, see LambdaRuntime::processNextEvent() + // Same variables as in the other runtimes, see LambdaRuntime::processNextEvent() $request->setCustomVar('LAMBDA_REQUEST_ID', $context->getAwsRequestId()); + $request->setCustomVar('_X_AMZN_TRACE_ID', $context->getTraceId()); $request->setCustomVar('LAMBDA_REQUEST_CONTEXT', json_encode($event->getRequestContext(), JSON_THROW_ON_ERROR)); $contentType = $event->getContentType(); diff --git a/tests/FpmRuntime/FpmHandlerTest.php b/tests/FpmRuntime/FpmHandlerTest.php index 840e8ba0a..19f5420fd 100644 --- a/tests/FpmRuntime/FpmHandlerTest.php +++ b/tests/FpmRuntime/FpmHandlerTest.php @@ -1326,6 +1326,7 @@ private function assertCommonServerVariables(array $response, array $expectedGlo 'GATEWAY_INTERFACE' => 'FastCGI/1.0', 'FCGI_ROLE' => 'RESPONDER', 'LAMBDA_REQUEST_ID' => 'abc', + '_X_AMZN_TRACE_ID' => 'abc', ]; // Allow to override some keys