From 8eaf3ac1d86eace68ba1073e4a78713c8c339136 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Tue, 21 Feb 2023 14:17:15 -0800 Subject: [PATCH 1/3] Remove sequence token --- src/Handler/CloudWatch.php | 61 ++------------------------------------ 1 file changed, 2 insertions(+), 59 deletions(-) diff --git a/src/Handler/CloudWatch.php b/src/Handler/CloudWatch.php index 26a1271..51c4723 100755 --- a/src/Handler/CloudWatch.php +++ b/src/Handler/CloudWatch.php @@ -54,11 +54,6 @@ class CloudWatch extends AbstractProcessingHandler */ private $initialized = false; - /** - * @var string - */ - private $sequenceToken; - /** * @var int */ @@ -201,11 +196,10 @@ private function flushBuffer(): void $this->initialize(); } - // send items, retry once with a fresh sequence token + // send items, retry once if failed try { $this->send($this->buffer); } catch (\Aws\CloudWatchLogs\Exception\CloudWatchLogsException $e) { - $this->refreshSequenceToken(); $this->send($this->buffer); } @@ -309,8 +303,7 @@ private function formatRecords(array $entry): array * * @param array $entries * - * @throws \Aws\CloudWatchLogs\Exception\CloudWatchLogsException Thrown by putLogEvents for example in case of an - * invalid sequence token + * @throws \Aws\CloudWatchLogs\Exception\CloudWatchLogsException Thrown by putLogEvents for example */ private function send(array $entries): void { @@ -331,15 +324,9 @@ private function send(array $entries): void 'logEvents' => $entries ]; - if (!empty($this->sequenceToken)) { - $data['sequenceToken'] = $this->sequenceToken; - } - $this->checkThrottle(); $response = $this->client->putLogEvents($data); - - $this->sequenceToken = $response->get('nextSequenceToken'); } private function initializeGroup(): void @@ -389,50 +376,6 @@ private function initialize(): void if ($this->createGroup) { $this->initializeGroup(); } - - $this->refreshSequenceToken(); - } - - private function refreshSequenceToken(): void - { - // fetch existing streams - $existingStreams = - $this - ->client - ->describeLogStreams( - [ - 'logGroupName' => $this->group, - 'logStreamNamePrefix' => $this->stream, - ] - )->get('logStreams'); - - // extract existing streams names - $existingStreamsNames = array_map( - function ($stream) { - - // set sequence token - if ($stream['logStreamName'] === $this->stream && isset($stream['uploadSequenceToken'])) { - $this->sequenceToken = $stream['uploadSequenceToken']; - } - - return $stream['logStreamName']; - }, - $existingStreams - ); - - // create stream if not created - if (!in_array($this->stream, $existingStreamsNames, true)) { - $this - ->client - ->createLogStream( - [ - 'logGroupName' => $this->group, - 'logStreamName' => $this->stream - ] - ); - } - - $this->initialized = true; } /** From da9cf2f146277adf6f1416b0eb0f8ad61ca4782a Mon Sep 17 00:00:00 2001 From: Jason Young Date: Tue, 21 Feb 2023 15:10:16 -0800 Subject: [PATCH 2/3] Update tests; ensure streams are created when needed --- src/Handler/CloudWatch.php | 60 ++++++++++++++++++++++++---- tests/Handler/CloudWatchTest.php | 67 ++++++++++++++++++++++---------- 2 files changed, 99 insertions(+), 28 deletions(-) diff --git a/src/Handler/CloudWatch.php b/src/Handler/CloudWatch.php index 51c4723..6254173 100755 --- a/src/Handler/CloudWatch.php +++ b/src/Handler/CloudWatch.php @@ -74,6 +74,11 @@ class CloudWatch extends AbstractProcessingHandler */ private $createGroup; + /** + * @var bool + */ + private $createStream; + /** * Data amount limit (http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html) * @@ -122,19 +127,21 @@ class CloudWatch extends AbstractProcessingHandler * @param int $level * @param bool $bubble * @param bool $createGroup + * @param bool $createStream * * @throws \Exception */ public function __construct( CloudWatchLogsClient $client, - $group, - $stream, - $retention = 14, - $batchSize = 10000, + $group, + $stream, + $retention = 14, + $batchSize = 10000, array $tags = [], - $level = Logger::DEBUG, - $bubble = true, - $createGroup = true + $level = Logger::DEBUG, + $bubble = true, + $createGroup = true, + $createStream = true, ) { if ($batchSize > 10000) { throw new \InvalidArgumentException('Batch size can not be greater than 10000'); @@ -147,6 +154,7 @@ public function __construct( $this->batchSize = $batchSize; $this->tags = $tags; $this->createGroup = $createGroup; + $this->createStream = $createStream; parent::__construct($level, $bubble); @@ -376,6 +384,44 @@ private function initialize(): void if ($this->createGroup) { $this->initializeGroup(); } + if($this->createStream) { + $this->initializeStream(); + } + $this->initialized = true; + } + + private function initializeStream(): void + { + // fetch existing streams + $existingStreams = + $this + ->client + ->describeLogStreams( + [ + 'logGroupName' => $this->group, + 'logStreamNamePrefix' => $this->stream, + ] + )->get('logStreams'); + + // extract existing streams names + $existingStreamsNames = array_map( + function ($stream) { + return $stream['logStreamName']; + }, + $existingStreams + ); + + // create stream if not created + if (!in_array($this->stream, $existingStreamsNames, true)) { + $this + ->client + ->createLogStream( + [ + 'logGroupName' => $this->group, + 'logStreamName' => $this->stream + ] + ); + } } /** diff --git a/tests/Handler/CloudWatchTest.php b/tests/Handler/CloudWatchTest.php index 985241f..3ab30b7 100644 --- a/tests/Handler/CloudWatchTest.php +++ b/tests/Handler/CloudWatchTest.php @@ -93,6 +93,31 @@ public function testInitializeWithCreateGroupDisabled() $reflectionMethod->invoke($handler); } + public function testInitializeWithCreateStreamDisabled() + { + $this + ->clientMock + ->expects($this->never()) + ->method('describeLogGroups'); + + $this + ->clientMock + ->expects($this->never()) + ->method('createLogGroup'); + + $this + ->clientMock + ->expects($this->never()) + ->method('describeLogStreams'); + + $handler = new CloudWatch($this->clientMock, $this->groupName, $this->streamName, 14, 10000, [], Logger::DEBUG, true, false, false); + + $reflection = new \ReflectionClass($handler); + $reflectionMethod = $reflection->getMethod('initialize'); + $reflectionMethod->setAccessible(true); + $reflectionMethod->invoke($handler); + } + public function testInitializeWithExistingLogGroup() { $logGroupsResult = new Result(['logGroups' => [['logGroupName' => $this->groupName]]]); @@ -450,34 +475,34 @@ public function testSendsBatchesSpanning24HoursOrLess() $this ->clientMock - ->expects($this->exactly(3)) - ->method('PutLogEvents') - ->willReturnCallback(function (array $data) { - /** @var int|null */ - $earliestTime = null; + ->expects($this->exactly(3)) + ->method('PutLogEvents') + ->willReturnCallback(function (array $data) { + /** @var int|null */ + $earliestTime = null; - /** @var int|null */ - $latestTime = null; + /** @var int|null */ + $latestTime = null; - foreach ($data['logEvents'] as $logEvent) { - $logTimestamp = $logEvent['timestamp']; + foreach ($data['logEvents'] as $logEvent) { + $logTimestamp = $logEvent['timestamp']; - if (!$earliestTime || $logTimestamp < $earliestTime) { - $earliestTime = $logTimestamp; - } + if (!$earliestTime || $logTimestamp < $earliestTime) { + $earliestTime = $logTimestamp; + } - if (!$latestTime || $logTimestamp > $latestTime) { - $latestTime = $logTimestamp; - } + if (!$latestTime || $logTimestamp > $latestTime) { + $latestTime = $logTimestamp; } + } - $this->assertNotNull($earliestTime); - $this->assertNotNull($latestTime); - $this->assertGreaterThanOrEqual($earliestTime, $latestTime); - $this->assertLessThanOrEqual(24 * 60 * 60 * 1000, $latestTime - $earliestTime); + $this->assertNotNull($earliestTime); + $this->assertNotNull($latestTime); + $this->assertGreaterThanOrEqual($earliestTime, $latestTime); + $this->assertLessThanOrEqual(24 * 60 * 60 * 1000, $latestTime - $earliestTime); - return $this->awsResultMock; - }); + return $this->awsResultMock; + }); $handler = $this->getCUT(); From 276d8b683d01b709feef357be3217d8855b006b7 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Tue, 21 Feb 2023 15:34:11 -0800 Subject: [PATCH 3/3] Fix weird indentation :) --- src/Handler/CloudWatch.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Handler/CloudWatch.php b/src/Handler/CloudWatch.php index 6254173..59e79ea 100755 --- a/src/Handler/CloudWatch.php +++ b/src/Handler/CloudWatch.php @@ -133,15 +133,15 @@ class CloudWatch extends AbstractProcessingHandler */ public function __construct( CloudWatchLogsClient $client, - $group, - $stream, - $retention = 14, - $batchSize = 10000, + $group, + $stream, + $retention = 14, + $batchSize = 10000, array $tags = [], - $level = Logger::DEBUG, - $bubble = true, - $createGroup = true, - $createStream = true, + $level = Logger::DEBUG, + $bubble = true, + $createGroup = true, + $createStream = true, ) { if ($batchSize > 10000) { throw new \InvalidArgumentException('Batch size can not be greater than 10000');