Skip to content

Commit

Permalink
Added option to disable creating the log group (#75)
Browse files Browse the repository at this point in the history
* Added option to disable creating the log group

* Move create group option to constructor

* Move paragraph to relevant section in README
  • Loading branch information
guillaumesmo authored and maxbanton committed Jan 6, 2020
1 parent 5cb1ca5 commit d62e2fb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ if you prefer to use a separate programmatic IAM user (recommended) or want to d
1. `DescribeLogStreams` [aws docs](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeLogStreams.html)
1. `DescribeLogGroups` [aws docs](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeLogGroups.html)

When setting the `$createGroup` argument to `false`, permissions `DescribeLogGroups` and `CreateLogGroup` can be omitted

## AWS IAM Policy full json example
```json
{
Expand Down
19 changes: 17 additions & 2 deletions src/Handler/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class CloudWatch extends AbstractProcessingHandler
*/
private $tags = [];

/**
* @var bool
*/
private $createGroup;

/**
* Data amount limit (http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html)
*
Expand Down Expand Up @@ -108,6 +113,7 @@ class CloudWatch extends AbstractProcessingHandler
* @param array $tags
* @param int $level
* @param bool $bubble
* @param bool $createGroup
*/
public function __construct(
CloudWatchLogsClient $client,
Expand All @@ -117,7 +123,8 @@ public function __construct(
$batchSize = 10000,
array $tags = [],
$level = Logger::DEBUG,
$bubble = true
$bubble = true,
$createGroup = true
) {
if ($batchSize > 10000) {
throw new \InvalidArgumentException('Batch size can not be greater than 10000');
Expand All @@ -129,6 +136,7 @@ public function __construct(
$this->retention = $retention;
$this->batchSize = $batchSize;
$this->tags = $tags;
$this->createGroup = $createGroup;

parent::__construct($level, $bubble);

Expand Down Expand Up @@ -286,7 +294,7 @@ private function send(array $entries)
$this->sequenceToken = $response->get('nextSequenceToken');
}

private function initialize()
private function initializeGroup()
{
// fetch existing groups
$existingGroups =
Expand Down Expand Up @@ -326,6 +334,13 @@ function ($group) {
);
}
}
}

private function initialize()
{
if ($this->createGroup) {
$this->initializeGroup();
}

$this->refreshSequenceToken();
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Handler/CloudWatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ protected function setUp()
->getMock();
}

public function testInitializeWithCreateGroupDisabled()
{
$this
->clientMock
->expects($this->never())
->method('describeLogGroups');

$this
->clientMock
->expects($this->never())
->method('createLogGroup');

$logStreamResult = new Result([
'logStreams' => [
[
'logStreamName' => $this->streamName,
'uploadSequenceToken' => '49559307804604887372466686181995921714853186581450198322'
]
]
]);

$this
->clientMock
->expects($this->once())
->method('describeLogStreams')
->with([
'logGroupName' => $this->groupName,
'logStreamNamePrefix' => $this->streamName,
])
->willReturn($logStreamResult);

$handler = new CloudWatch($this->clientMock, $this->groupName, $this->streamName, 14, 10000, [], Logger::DEBUG, true, 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]]]);
Expand Down

0 comments on commit d62e2fb

Please sign in to comment.