Skip to content

Commit

Permalink
[MetricsPower] Fix consumer issue, when redis not available
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalzombie committed Apr 5, 2024
1 parent 17b9e84 commit 0d269d2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Handler/MetricsHandler.php
Expand Up @@ -26,7 +26,8 @@ class MetricsHandler implements MetricsHandlerInterface
{
public function __construct(
private readonly OptionsResolverLocatorInterface $locator,
) {}
) {
}

public function handle(AbstractWorkerMessageEvent|SendMessageToTransportsEvent $event): void
{
Expand Down
11 changes: 7 additions & 4 deletions Helper/CounterHelper.php
Expand Up @@ -27,11 +27,14 @@ final class CounterHelper

public static function makeName(PrometheusOptions $options, ?string $prefix = null, ?string $postfix = null): string
{
$topic = StringHelper::toSnakeCase($options->topic);
$name = StringHelper::toSnakeCase($options->name);

return match (true) {
null !== $prefix && null !== $postfix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s_%s', $prefix, $options->topic, $options->name, $postfix)),
null !== $prefix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s', $prefix, $options->topic, $options->name)),
null !== $postfix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s', $options->topic, $options->name, $postfix)),
default => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s', $options->topic, $options->name)),
null !== $prefix && null !== $postfix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s_%s', $prefix, $topic, $name, $postfix)),
null !== $prefix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s', $prefix, $topic, $name)),
null !== $postfix => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s_%s', $topic, $name, $postfix)),
default => str_replace(self::REPLACE_FROM, self::REPLACE_TO, sprintf('%s_%s', $topic, $name)),
};
}
}
8 changes: 2 additions & 6 deletions OptionsResolver/Resolver/PrometheusOptionsResolver.php
Expand Up @@ -20,10 +20,9 @@
use FRZB\Component\MetricsPower\Attribute\PrometheusOptions;
use FRZB\Component\MetricsPower\Exception\MetricsRegistrationException;
use FRZB\Component\MetricsPower\Helper\CounterHelper;
use FRZB\Component\MetricsPower\Logger\MetricsPowerLoggerInterface;
use Prometheus\Exception\MetricsRegistrationException as BaseMetricsRegistrationException;
use Prometheus\Exception\StorageException;
use Prometheus\RegistryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent;
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
Expand All @@ -39,7 +38,6 @@ public function __construct(
#[Autowire(env: 'PROMETHEUS_NAMESPACE')]
private readonly string $namespace,
private readonly RegistryInterface $registry,
private readonly ?MetricsPowerLoggerInterface $logger = null,
) {}

/** @throws MetricsRegistrationException */
Expand All @@ -59,9 +57,7 @@ public function resolve(AbstractWorkerMessageEvent|SendMessageToTransportsEvent
->inc($options->values);
} catch (BaseMetricsRegistrationException $e) {
throw MetricsRegistrationException::fromThrowable($e);
} catch (\Throwable $e) {
$this->logger?->error($event, $e);
}
} catch (StorageException $e) {}
}

public static function getType(): string
Expand Down
Expand Up @@ -23,6 +23,7 @@
use FRZB\Component\MetricsPower\Tests\Stub\TestConstants;
use Prometheus\Counter;
use Prometheus\Exception\MetricsRegistrationException as BaseMetricsRegistrationException;
use Prometheus\Exception\StorageException;
use Prometheus\RegistryInterface;
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent;
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
Expand Down Expand Up @@ -101,3 +102,18 @@

$this->expectExceptionMessage('something goes wrong');
})->throws(MetricsRegistrationException::class);

test('It can not throw when storage fails', function (): void {
$registry = \Mockery::mock(RegistryInterface::class);
$prometheusOptionResolver = new PrometheusOptionsResolver(TestConstants::DEFAULT_NAMESPACE, $registry);

$event = new WorkerMessageFailedEvent(createTestEnvelope(), TestConstants::DEFAULT_RECEIVER_NAME, SomethingGoesWrongException::wrong());
$options = new PrometheusOptions(TestConstants::DEFAULT_RECEIVER_NAME, 'test-name', 'test-help', ['type'], ['TestMessage']);

$registry->expects('getOrRegisterCounter')
->once()
->andThrow(new StorageException('something goes wrong'));

/** @noinspection PhpUnhandledExceptionInspection */
$prometheusOptionResolver->resolve($event, $options);
});

0 comments on commit 0d269d2

Please sign in to comment.