Skip to content

Add API Gateway request timestamp to server variables #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Runtime/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static function fromLambdaEvent(array $event, array $serverVariables = []
$serverVariables['SCRIPT_FILENAME'] = $handler;
}

if ($timestamp = self::extractRequestTimestamp($event)) {
$serverVariables['AWS_API_GATEWAY_REQUEST_TIME'] = $timestamp;
}

[$headers, $serverVariables] = static::ensureContentTypeIsSet(
$event, $headers, $serverVariables
);
Expand Down Expand Up @@ -266,4 +270,21 @@ protected static function ensureSourceIpAddressIsSet(array $event, array $header

return $headers;
}

/**
* Extracts the time (millisecond epoch) when the request was received by the API Gateway.
*
* @param array $event
* @return int|null
*/
protected static function extractRequestTimestamp(array $event)
{
if (! isset($event['requestContext'])) {
return null;
}

return $event['requestContext']['requestTimeEpoch'] // REST API (V1)
?? $event['requestContext']['timeEpoch'] // HTTP API (V2)
?? null;
}
}
45 changes: 45 additions & 0 deletions tests/Unit/FpmRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace Laravel\Vapor\Tests\Unit;

use Carbon\Carbon;
use Laravel\Vapor\Runtime\Fpm\FpmRequest;
use Mockery;
use PHPUnit\Framework\TestCase;

class FpmRequestTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Carbon::setTestNow('2021-01-01 00:00:00');
}

protected function tearDown(): void
{
Mockery::close();
Expand Down Expand Up @@ -182,4 +190,41 @@ public function test_request_content_length_is_numeric()

$this->assertSame(0, $request->getContentLength());
}

public function test_api_gateway_v1_request_time_is_set()
{
$epoch = Carbon::now()->getPreciseTimestamp(3);

$request = FpmRequest::fromLambdaEvent([
'httpMethod' => 'GET',
'requestContext' => [
'requestTimeEpoch' => $epoch,
],
]);

$this->assertSame($epoch, $request->serverVariables['AWS_API_GATEWAY_REQUEST_TIME']);
}

public function test_api_gateway_v2_request_time_is_set()
{
$epoch = Carbon::now()->getPreciseTimestamp(3);

$request = FpmRequest::fromLambdaEvent([
'httpMethod' => 'GET',
'requestContext' => [
'timeEpoch' => $epoch,
],
]);

$this->assertSame($epoch, $request->serverVariables['AWS_API_GATEWAY_REQUEST_TIME']);
}

public function test_elb_request_time_is_not_set()
{
$request = FpmRequest::fromLambdaEvent([
'httpMethod' => 'GET',
]);

$this->assertArrayNotHasKey('AWS_API_GATEWAY_REQUEST_TIME', $request->serverVariables);
}
}