Skip to content

Commit

Permalink
Do not overshadow errors thrown by DescribeLogGroups. (#68)
Browse files Browse the repository at this point in the history
If DescribeLogGroups throws an error (e.g. AccessDeniedException), the retry logic
will still call DescribeLogStreams, which will also fail, either with
AccessDeniedException, or, more confusingly, with "The specified log group
does not exist." if the log group has never been created.

This change makes sure that the original error is thrown.
  • Loading branch information
KiNgMaR authored and maxbanton committed Sep 11, 2019
1 parent b7fee45 commit f53e83e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Handler/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ private function addToBuffer(array $record)
private function flushBuffer()
{
if (!empty($this->buffer)) {
if (false === $this->initialized) {
$this->initialize();
}

// send items, retry once with a fresh sequence token
try {
$this->send($this->buffer);
Expand Down Expand Up @@ -254,10 +258,6 @@ private function formatRecords(array $entry)
*/
private function send(array $entries)
{
if (false === $this->initialized) {
$this->initialize();
}

$data = [
'logGroupName' => $this->group,
'logStreamName' => $this->stream,
Expand Down Expand Up @@ -317,8 +317,6 @@ function ($group) {
}

$this->refreshSequenceToken();

$this->initialized = true;
}

private function refreshSequenceToken()
Expand Down
27 changes: 27 additions & 0 deletions tests/Handler/CloudWatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Aws\CloudWatchLogs\Exception\CloudWatchLogsException;
use Aws\Result;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Formatter\LineFormatter;
Expand Down Expand Up @@ -301,6 +302,32 @@ public function testFormatter()
$this->assertEquals($expected, $formatter);
}

public function testExceptionFromDescribeLogGroups()
{
// e.g. 'User is not authorized to perform logs:DescribeLogGroups'
$awsException = $this->getMockBuilder(CloudWatchLogsException::class)
->disableOriginalConstructor()
->getMock();

// if this fails ...
$this
->clientMock
->expects($this->atLeastOnce())
->method('describeLogGroups')
->will($this->throwException($awsException));

// ... this should not be called:
$this
->clientMock
->expects($this->never())
->method('describeLogStreams');

$this->expectException(CloudWatchLogsException::class);

$handler = $this->getCUT(0);
$handler->handle($this->getRecord(Logger::INFO));
}

private function prepareMocks()
{
$logGroupsResult = new Result(['logGroups' => [['logGroupName' => $this->groupName]]]);
Expand Down

0 comments on commit f53e83e

Please sign in to comment.