From 8878661e62b9333f91443312b0f5c9885f989959 Mon Sep 17 00:00:00 2001 From: Artem Ukrainskiy Date: Tue, 10 Oct 2023 21:59:54 +0300 Subject: [PATCH] feat: Add support SuppressExceptionsMetrics for MetricsFactory (#10) --- src/MetricsFactory.php | 15 ++++++++++- src/MetricsOptions.php | 1 + tests/Unit/MetricsFactoryTest.php | 43 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/MetricsFactoryTest.php diff --git a/src/MetricsFactory.php b/src/MetricsFactory.php index 146b2a8..17ccbca 100644 --- a/src/MetricsFactory.php +++ b/src/MetricsFactory.php @@ -2,16 +2,29 @@ namespace Spiral\RoadRunner\Metrics; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Spiral\Goridge\RPC\RPCInterface; class MetricsFactory { + public function __construct( + private readonly LoggerInterface $logger = new NullLogger(), + ) { + } + public function create(RPCInterface $rpc, MetricsOptions $options): MetricsInterface { - return new RetryMetrics( + $metrics = new RetryMetrics( new Metrics($rpc), $options->retryAttempts, $options->retrySleepMicroseconds, ); + + if ($options->suppressExceptions) { + $metrics = new SuppressExceptionsMetrics($metrics, $this->logger); + } + + return $metrics; } } diff --git a/src/MetricsOptions.php b/src/MetricsOptions.php index 87c71b2..1fa1c98 100644 --- a/src/MetricsOptions.php +++ b/src/MetricsOptions.php @@ -11,6 +11,7 @@ class MetricsOptions public function __construct( public readonly int $retryAttempts = 3, public readonly int $retrySleepMicroseconds = 50, + public readonly bool $suppressExceptions = false, ) { } } diff --git a/tests/Unit/MetricsFactoryTest.php b/tests/Unit/MetricsFactoryTest.php new file mode 100644 index 0000000..91ebd78 --- /dev/null +++ b/tests/Unit/MetricsFactoryTest.php @@ -0,0 +1,43 @@ +createMock(RPCInterface::class); + $rpc->expects($this->once())->method('withServicePrefix') + ->with('metrics') + ->willReturn($rpc); + + self::assertInstanceOf($expectedClass, $factory->create($rpc, $options)); + } + + public static function providerForTestCreate(): array + { + return [ + 'create RetryMetrics' => [ + 'options' => new MetricsOptions(), + 'expectedClass' => RetryMetrics::class, + ], + 'create SuppressExceptionsMetrics' => [ + 'options' => new MetricsOptions(suppressExceptions: true), + 'expectedClass' => SuppressExceptionsMetrics::class, + ], + ]; + } +}