Skip to content

Commit a2162bd

Browse files
Dmitry TarasovDmitry Tarasov
authored andcommitted
Add error governor functionality
1 parent eb21540 commit a2162bd

7 files changed

Lines changed: 69 additions & 7 deletions

File tree

src/Stackify/Log/Builder/BuilderInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stackify\Log\Builder;
44

5+
use Stackify\Log\Entities\Api\LogMsg;
56
use Stackify\Log\Entities\LogEntryInterface;
67

78
interface BuilderInterface
@@ -10,7 +11,7 @@ interface BuilderInterface
1011
/**
1112
* @return string Formatted JSON
1213
*/
13-
public function getAgentMessage(LogEntryInterface $logEntry);
14+
public function getAgentMessage(LogMsg $logMsg);
1415

1516
/**
1617
* @param \Stackify\Log\Entities\Api\LogMsg[] $logMsgs

src/Stackify/Log/Builder/MessageBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ public function __construct($loggerName, $appName, $environmentName = null)
3232
EnvironmentDetail::getInstance()->init($appName, $environmentName);
3333
}
3434

35-
public function getAgentMessage(LogEntryInterface $logEntry)
35+
public function getAgentMessage(LogMsg $logMsg)
3636
{
37-
$logMsg = $this->createLogMsg($logEntry);
3837
$message = new AgentMessage($this->loggerName, $this->appName, $this->environmentName, $logMsg);
3938
return $this->encodeJSON($message). PHP_EOL;
4039
}

src/Stackify/Log/Builder/NullBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stackify\Log\Builder;
44

5+
use Stackify\Log\Entities\Api\LogMsg;
56
use Stackify\Log\Entities\LogEntryInterface;
67

78
/**
@@ -12,7 +13,7 @@ class NullBuilder implements BuilderInterface
1213

1314
public function createLogMsg(LogEntryInterface $logEntry) {}
1415

15-
public function getAgentMessage(LogEntryInterface $logEntry) {}
16+
public function getAgentMessage(LogMsg $logMsg) {}
1617

1718
public function getApiMessage(array $logMsgs) {}
1819

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Stackify\Log\Filters;
4+
5+
use Stackify\Log\Entities\Api\LogMsg;
6+
use Stackify\Log\Entities\Api\ErrorItem;
7+
use Stackify\Log\Entities\Api\StackifyError;
8+
9+
class ErrorGovernor
10+
{
11+
12+
const MAX_DUPLICATES = 100;
13+
14+
private $counter = array();
15+
16+
public function shouldBeSent(LogMsg $logMsg = null)
17+
{
18+
if (!isset($logMsg->Ex)) {
19+
return true;
20+
}
21+
$errorItem = $this->getBaseError($logMsg->Ex);
22+
$key = $this->getUniqueKey($errorItem);
23+
if (!isset($this->counter[$key])) {
24+
$this->counter[$key] = 0;
25+
}
26+
return (++$this->counter[$key] <= self::MAX_DUPLICATES);
27+
}
28+
29+
/**
30+
* @return \Stackify\Log\Entities\Api\ErrorItem
31+
*/
32+
private function getBaseError(StackifyError $error)
33+
{
34+
$errorItem = $error->Error;
35+
while (null !== $errorItem->InnerError) {
36+
$errorItem = $errorItem->InnerError;
37+
}
38+
return $errorItem;
39+
}
40+
41+
private function getUniqueKey(ErrorItem $item)
42+
{
43+
$key = sprintf('%s-%s-%s', $item->ErrorType, $item->ErrorTypeCode, $item->SourceMethod);
44+
return md5($key);
45+
}
46+
47+
}

src/Stackify/Log/Transport/AbstractApiTransport.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public function __construct($apiKey, array $options = array())
2323

2424
public function addEntry(LogEntryInterface $logEntry)
2525
{
26-
$this->queue[] = $this->messageBuilder->createLogMsg($logEntry);
26+
$logMsg = $this->messageBuilder->createLogMsg($logEntry);
27+
if ($this->errorGovernor->shouldBeSent($logMsg)) {
28+
$this->queue[] = $logMsg;
29+
}
2730
}
2831

2932
public function finish()

src/Stackify/Log/Transport/AbstractTransport.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Stackify\Log\Builder\BuilderInterface;
66
use Stackify\Log\Builder\NullBuilder;
7+
use Stackify\Log\Filters\ErrorGovernor;
78
use Stackify\Exceptions\InitializationException;
89

910
abstract class AbstractTransport implements TransportInterface
@@ -13,13 +14,20 @@ abstract class AbstractTransport implements TransportInterface
1314
* @var \Stackify\Log\Builder\BuilderInterface
1415
*/
1516
protected $messageBuilder;
17+
18+
/**
19+
* @var \Stackify\Log\Filters\ErrorGovernor
20+
*/
21+
protected $errorGovernor;
22+
1623
protected $debug = false;
1724
private $debugLogPath;
1825

1926
public function __construct()
2027
{
2128
$ds = DIRECTORY_SEPARATOR;
2229
$this->debugLogPath = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
30+
$this->errorGovernor = new ErrorGovernor();
2331
// add empty implementation to avoid method calls on non-object
2432
$this->setMessageBuilder(new NullBuilder());
2533
}

src/Stackify/Log/Transport/AgentTransport.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ public function __construct(array $options = array())
3232

3333
public function addEntry(LogEntryInterface $logEntry)
3434
{
35-
$data = $this->messageBuilder->getAgentMessage($logEntry);
36-
$this->send($data);
35+
$logMsg = $this->messageBuilder->createLogMsg($logEntry);
36+
if ($this->errorGovernor->shouldBeSent($logMsg)) {
37+
$data = $this->messageBuilder->getAgentMessage($logMsg);
38+
$this->send($data);
39+
}
3740
}
3841

3942
public function finish()

0 commit comments

Comments
 (0)