diff --git a/src/Handler/CloudWatch.php b/src/Handler/CloudWatch.php index 26a1271..59e79ea 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 */ @@ -79,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) * @@ -127,6 +127,7 @@ class CloudWatch extends AbstractProcessingHandler * @param int $level * @param bool $bubble * @param bool $createGroup + * @param bool $createStream * * @throws \Exception */ @@ -139,7 +140,8 @@ public function __construct( array $tags = [], $level = Logger::DEBUG, $bubble = true, - $createGroup = true + $createGroup = true, + $createStream = true, ) { if ($batchSize > 10000) { throw new \InvalidArgumentException('Batch size can not be greater than 10000'); @@ -152,6 +154,7 @@ public function __construct( $this->batchSize = $batchSize; $this->tags = $tags; $this->createGroup = $createGroup; + $this->createStream = $createStream; parent::__construct($level, $bubble); @@ -201,11 +204,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 +311,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 +332,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,11 +384,13 @@ private function initialize(): void if ($this->createGroup) { $this->initializeGroup(); } - - $this->refreshSequenceToken(); + if($this->createStream) { + $this->initializeStream(); + } + $this->initialized = true; } - private function refreshSequenceToken(): void + private function initializeStream(): void { // fetch existing streams $existingStreams = @@ -409,12 +406,6 @@ private function refreshSequenceToken(): void // 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 @@ -431,8 +422,6 @@ function ($stream) { ] ); } - - $this->initialized = true; } /** 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();