brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
314 stars 63 forks source link

Reading the AWS Request ID #145

Open harmenjanssen opened 7 months ago

harmenjanssen commented 7 months ago

Hi,

I was wondering if it's possible to get at the original Lambda Request ID within your Laravel app?

I found the Context class which has a reference to the ID, but I'm unsure if that's accessible from within my Laravel app. (tried dependency-injecting it, but that was not the answer)

Thanks a lot in advance!

tillkruss commented 7 months ago

Yes: https://github.com/brefphp/laravel-bridge/blob/2bff7e67ed11f833329cfd5340f7d3e606347e54/config/bref.php#L31

harmenjanssen commented 7 months ago

Hmm, alright, I have to be more specific I guess: at this point I am not getting the X-Request-ID header in my requests. I turned request_context on, but that doesn't give me any information about the AWS request.

I'm logging all headers coming in to my controller via the request, but it's not there. Is there a way to get at the current context either in a controller, or middleware, or somewhere in that chain?

MantasVaitkunas commented 6 months ago

I am having the same problem. @harmenjanssen can you reopen the issue as ATM it's not clear how to use this config option :/

tillkruss commented 6 months ago

Does your request object not have the X-Request-ID header?

harmenjanssen commented 6 months ago

Does your request object not have the X-Request-ID header?

Nope, it does not.

tillkruss commented 6 months ago

Mhhh, turns out my apps aren't actually using this anymore. @georgeboot Do we have any way to access the AWS request id?

georgeboot commented 6 months ago

Afk atm, but there should be a $_SERVER var called HTTP_X_REQUEST_ID I believe

MantasVaitkunas commented 6 months ago

As @georgeboot said, I had a look into $_SERVER and found out that $_SERVER['LAMBDA_INVOCATION_CONTEXT'] has necessary info:

$requestId = null;
if (isset($_SERVER['LAMBDA_INVOCATION_CONTEXT'])) {
    $lambdaContext = json_decode($_SERVER['LAMBDA_INVOCATION_CONTEXT']);
    $requestId = $lambdaContext?->awsRequestId;
}
tillkruss commented 6 months ago

I feel like we should expose the invocation context a little better, maybe even attach some of the data to the Request object.