Skip to content

Commit 5ed5b65

Browse files
committed
chore: add some more debug logging
1 parent 442ee58 commit 5ed5b65

5 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/Lambda/Handler/Http/AbstractPhpFpmRequestEventHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function createLambdaEventResponse(HttpRequestEvent $event): HttpRespo
5858
$request = FastCgiRequest::createFromInvocationEvent($event, $this->getScriptFilePath($event));
5959
$timeoutMs = max(1000, $event->getContext()->getRemainingTimeInMs() - 1000);
6060

61-
$this->logger->debug('FastCgi request sent:', [
61+
$this->logger->debug('FastCGI request sent', [
6262
'content' => $request->getContent(),
6363
'parameters' => $request->getParams(),
6464
]);

src/Lambda/Handler/Sqs/AbstractSqsHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function handle(InvocationEventInterface $event): ResponseInterface
6363

6464
$event->getRecords()->each(function (SqsRecord $record) use ($event, &$failedRecords): void {
6565
try {
66+
$this->logger->debug(sprintf('Processing SQS message [%s]', $record->getMessageId()), [
67+
'record' => $record->toArray(),
68+
]);
69+
6670
$this->processRecord($event->getContext(), $record);
6771
} catch (\Throwable $exception) {
6872
$this->logger->error(sprintf('Processing SQS message [%s] failed: %s', $record->getMessageId(), $exception->getMessage()), [

src/Lambda/Handler/WarmUpEventHandler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ymir\Runtime\Lambda\InvocationEvent\WarmUpEvent;
2020
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
2121
use Ymir\Runtime\Lambda\Response\ResponseInterface;
22+
use Ymir\Runtime\Logger;
2223

2324
/**
2425
* Lambda invocation event handler for warming up Lambda functions.
@@ -32,12 +33,20 @@ class WarmUpEventHandler implements LambdaEventHandlerInterface
3233
*/
3334
private $lambdaClient;
3435

36+
/**
37+
* The logger that sends logs to CloudWatch.
38+
*
39+
* @var Logger
40+
*/
41+
private $logger;
42+
3543
/**
3644
* Constructor.
3745
*/
38-
public function __construct(LambdaClient $lambdaClient)
46+
public function __construct(LambdaClient $lambdaClient, Logger $logger)
3947
{
4048
$this->lambdaClient = $lambdaClient;
49+
$this->logger = $logger;
4150
}
4251

4352
/**
@@ -69,6 +78,8 @@ public function handle(InvocationEventInterface $event): ResponseInterface
6978
throw new InvalidConfigurationException('"AWS_LAMBDA_FUNCTION_NAME" environment variable is\'t set');
7079
}
7180

81+
$this->logger->debug(sprintf('Warming up %s additional functions', $concurrency));
82+
7283
// The first Lambda function invoked will be the one running this code. So, if we want the number of concurrent
7384
// Lambda functions to match, we need to keep the concurrency number the same and not subtract one from it.
7485
for ($i = 0; $i < $concurrency; ++$i) {

tests/Unit/Lambda/Handler/Sqs/AbstractSqsHandlerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function testHandleCollectsFailures(): void
6262
]);
6363

6464
$logger = $this->getLoggerMock();
65+
$logger->expects($this->exactly(3))
66+
->method('debug')
67+
->with($this->stringContains('Processing SQS message [id'));
6568
$logger->expects($this->once())
6669
->method('error');
6770

tests/Unit/Lambda/Handler/WarmUpEventHandlerTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@
2020
use Ymir\Runtime\Tests\Mock\FunctionMockTrait;
2121
use Ymir\Runtime\Tests\Mock\InvocationEventInterfaceMockTrait;
2222
use Ymir\Runtime\Tests\Mock\LambdaClientMockTrait;
23+
use Ymir\Runtime\Tests\Mock\LoggerMockTrait;
2324
use Ymir\Runtime\Tests\Mock\WarmUpEventMockTrait;
2425

2526
class WarmUpEventHandlerTest extends TestCase
2627
{
2728
use FunctionMockTrait;
2829
use InvocationEventInterfaceMockTrait;
2930
use LambdaClientMockTrait;
31+
use LoggerMockTrait;
3032
use WarmUpEventMockTrait;
3133

3234
public function testCanHandleWarmUpEventType(): void
3335
{
34-
$this->assertTrue((new WarmUpEventHandler($this->getLambdaClientMock()))->canHandle($this->getWarmUpEventMock()));
36+
$this->assertTrue((new WarmUpEventHandler($this->getLambdaClientMock(), $this->getLoggerMock()))->canHandle($this->getWarmUpEventMock()));
3537
}
3638

3739
public function testCanHandleWrongEventType(): void
3840
{
39-
$this->assertFalse((new WarmUpEventHandler($this->getLambdaClientMock()))->canHandle($this->getInvocationEventInterfaceMock()));
41+
$this->assertFalse((new WarmUpEventHandler($this->getLambdaClientMock(), $this->getLoggerMock()))->canHandle($this->getInvocationEventInterfaceMock()));
4042
}
4143

4244
public function testHandleDoesntInvokeAdditionalFunctionsWhenConcurrencyIsOne(): void
@@ -51,7 +53,7 @@ public function testHandleDoesntInvokeAdditionalFunctionsWhenConcurrencyIsOne():
5153
$lambdaClient->expects($this->never())
5254
->method('invoke');
5355

54-
$reponse = (new WarmUpEventHandler($lambdaClient))->handle($event);
56+
$reponse = (new WarmUpEventHandler($lambdaClient, $this->getLoggerMock()))->handle($event);
5557

5658
$this->assertInstanceOf(HttpResponse::class, $reponse);
5759
$this->assertSame([
@@ -69,11 +71,16 @@ public function testHandleInvokesAdditionalFunctions(): void
6971
$event = $this->getWarmUpEventMock();
7072
$getenv = $this->getFunctionMock($this->getNamespace(WarmUpEventHandler::class), 'getenv');
7173
$lambdaClient = $this->getLambdaClientMock();
74+
$logger = $this->getLoggerMock();
7275

7376
$event->expects($this->any())
7477
->method('getConcurrency')
7578
->willReturn(3);
7679

80+
$logger->expects($this->once())
81+
->method('debug')
82+
->with($this->identicalTo('Warming up 3 additional functions'));
83+
7784
$getenv->expects($this->once())
7885
->with($this->identicalTo('AWS_LAMBDA_FUNCTION_NAME'))
7986
->willReturn('function-name');
@@ -85,10 +92,10 @@ public function testHandleInvokesAdditionalFunctions(): void
8592
'Qualifier' => 'deployed',
8693
'InvocationType' => 'Event',
8794
'LogType' => 'None',
88-
'Payload' => '{"ping": true}',
89-
]);
95+
'Payload' => '{"ping": true}',
96+
]);
9097

91-
$reponse = (new WarmUpEventHandler($lambdaClient))->handle($event);
98+
$reponse = (new WarmUpEventHandler($lambdaClient, $logger))->handle($event);
9299

93100
$this->assertInstanceOf(HttpResponse::class, $reponse);
94101
$this->assertSame([
@@ -112,14 +119,14 @@ public function testHandleWithNoEnvironmentVariable(): void
112119
->method('getConcurrency')
113120
->willReturn(3);
114121

115-
(new WarmUpEventHandler($this->getLambdaClientMock()))->handle($event);
122+
(new WarmUpEventHandler($this->getLambdaClientMock(), $this->getLoggerMock()))->handle($event);
116123
}
117124

118125
public function testHandleWithWrongEventType(): void
119126
{
120127
$this->expectException(\InvalidArgumentException::class);
121128
$this->expectExceptionMessage('WarmUpEventHandler can only handle WarmUpEvent objects');
122129

123-
(new WarmUpEventHandler($this->getLambdaClientMock()))->handle($this->getInvocationEventInterfaceMock());
130+
(new WarmUpEventHandler($this->getLambdaClientMock(), $this->getLoggerMock()))->handle($this->getInvocationEventInterfaceMock());
124131
}
125132
}

0 commit comments

Comments
 (0)