Skip to content

Commit

Permalink
Added silencer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilari Moragrega committed Jun 13, 2017
1 parent d67809e commit 1bc21f6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/BalancerBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function it_can_build_the_balancer(Monitor $monitor, LoggerInterface $logger)
{
$this->withLogger($logger)->shouldReturnAnInstanceOf(BalancerBuilder::class);
$this->withMonitor($monitor, "foo")->shouldReturnAnInstanceOf(BalancerBuilder::class);
$this->withoutExceptions()->shouldReturnAnInstanceOf(BalancerBuilder::class);
$this->create([
"feature_1" => ["foo" => 50, "off" => 50],
"feature_2" => ["bar" => 20, "rab" => 40, "arb" => 40]
Expand Down
29 changes: 29 additions & 0 deletions spec/Decorator/ExceptionSilencerDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace spec\Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\Exception\OutOfBoundsException;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;

class ExceptionSilencerDecoratorSpec extends ObjectBehavior
{
function let(BalancerInterface $balancer, LoggerInterface $logger)
{
$this->beConstructedWith($balancer, $logger);
}

function it_silences_exceptions(BalancerInterface $balancer, LoggerInterface $logger)
{
$exception = new OutOfBoundsException("feature foo is not set");
$balancer->get("foo", 123456)->willThrow($exception);

$this->get("foo", 123456)->shouldBe("");

$logger->error("Exception thrown balancing feature 'foo'", [
"feature" => "foo",
"exception" => $exception,
])->shouldHaveBeenCalled();
}
}
20 changes: 20 additions & 0 deletions src/BalancerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cmp\FeatureBalancer;

use Cmp\FeatureBalancer\Config\Identifier;
use Cmp\FeatureBalancer\Decorator\ExceptionSilencerDecorator;
use Cmp\FeatureBalancer\Decorator\LoggerDecorator;
use Cmp\FeatureBalancer\Decorator\MonitoringDecorator;
use Cmp\Monitoring\Monitor;
Expand Down Expand Up @@ -31,6 +32,11 @@ class BalancerBuilder
*/
private $logLevel;

/**
* @var bool
*/
private $exceptions = true;

/**
* @param array $config
*
Expand All @@ -49,6 +55,10 @@ public function create(array $config = [])
$balancer = new LoggerDecorator($balancer, $this->logger, $this->logLevel);
}

if (!$this->exceptions) {
$balancer = new ExceptionSilencerDecorator($balancer, $this->logger);
}

return $balancer;
}

Expand Down Expand Up @@ -80,6 +90,16 @@ public function withLogger(LoggerInterface $logger, $logLevel = LogLevel::INFO)
return $this;
}

/**
* @return $this
*/
public function withoutExceptions()
{
$this->exceptions = false;

return $this;
}

/**
* Add the features to the balancer
*
Expand Down
43 changes: 43 additions & 0 deletions src/Decorator/ExceptionSilencerDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Cmp\FeatureBalancer\Decorator;

use Cmp\FeatureBalancer\BalancerInterface;
use Cmp\FeatureBalancer\Exception\BalancerException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ExceptionSilencerDecorator extends BalancerDecorator implements BalancerInterface
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @param BalancerInterface $balancer
* @param LoggerInterface|null $logger
*/
public function __construct(BalancerInterface $balancer, LoggerInterface $logger = null)
{
parent::__construct($balancer);
$this->logger = $logger ?: new NullLogger();
}

/**
* {@inheritdoc}
*/
public function get($feature, $seed = null)
{
try {
return $this->balancer->get($feature, $seed);
} catch (BalancerException $exception) {
$this->logger->error("Exception thrown balancing feature '$feature'", [
"feature" => $feature,
"exception" => $exception,
]);
}

return "";
}
}

0 comments on commit 1bc21f6

Please sign in to comment.