Skip to content

Commit

Permalink
feat: Add support SuppressExceptionsMetrics for MetricsFactory (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtARTs36 authored Oct 10, 2023
1 parent 633cd8c commit 8878661
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/MetricsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/MetricsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MetricsOptions
public function __construct(
public readonly int $retryAttempts = 3,
public readonly int $retrySleepMicroseconds = 50,
public readonly bool $suppressExceptions = false,
) {
}
}
43 changes: 43 additions & 0 deletions tests/Unit/MetricsFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Spiral\RoadRunner\Metrics\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Spiral\Goridge\RPC\RPC;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\Metrics\MetricsFactory;
use Spiral\RoadRunner\Metrics\MetricsOptions;
use Spiral\RoadRunner\Metrics\RetryMetrics;
use Spiral\RoadRunner\Metrics\SuppressExceptionsMetrics;

final class MetricsFactoryTest extends TestCase
{
/**
* @dataProvider providerForTestCreate
*/
public function testCreate(MetricsOptions $options, string $expectedClass): void
{
$factory = new MetricsFactory();

$rpc = $this->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,
],
];
}
}

0 comments on commit 8878661

Please sign in to comment.