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

Remove sequence token for PutLogEvents #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 16 additions & 27 deletions src/Handler/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ class CloudWatch extends AbstractProcessingHandler
*/
private $initialized = false;

/**
* @var string
*/
private $sequenceToken;

/**
* @var int
*/
Expand All @@ -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)
*
Expand Down Expand Up @@ -127,6 +127,7 @@ class CloudWatch extends AbstractProcessingHandler
* @param int $level
* @param bool $bubble
* @param bool $createGroup
* @param bool $createStream
*
* @throws \Exception
*/
Expand All @@ -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');
Expand All @@ -152,6 +154,7 @@ public function __construct(
$this->batchSize = $batchSize;
$this->tags = $tags;
$this->createGroup = $createGroup;
$this->createStream = $createStream;

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

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -431,8 +422,6 @@ function ($stream) {
]
);
}

$this->initialized = true;
}

/**
Expand Down
67 changes: 46 additions & 21 deletions tests/Handler/CloudWatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]);
Expand Down Expand Up @@ -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();

Expand Down