Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to disable creating the log group #75

Merged
merged 3 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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