Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sentry + Swarrot Processor #233

Closed
Closed
@voodoo-dn

Description

@voodoo-dn

Hi to everyone. I use very nice bundle https://github.com/swarrot/SwarrotBundle for Symfony.
Because current version of this bundle does not supports newest version of sentry-symfony, I use own sentry processor.

Main problem: all errors sent when consumer is died. Realtime error tracking doesn't works.

Current configuration:

sentry:
    dsn: '%env(SENTRY_DSN)%'
    options:
        release: '%env(RELEASE_NAME)%'
        environment: '%env(SENTRY_ENV)%'
        enable_compression: false

Installation was via that documentation https://docs.sentry.io/platforms/php/symfony/#installation

<?php

declare(strict_types=1);

namespace App\Infrastructure\Swarrot\Processor;

use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Swarrot\Broker\Message;
use Swarrot\Processor\ProcessorInterface;

class SentryProcessor implements ProcessorInterface
{
    /**
     * @var ProcessorInterface
     */
    private $processor;

    /**
     * @var HubInterface
     */
    private $hub;

    /**
     * SentryProcessor constructor.
     *
     * @param ProcessorInterface $processor
     * @param HubInterface       $hub
     */
    public function __construct(ProcessorInterface $processor, HubInterface $hub)
    {
        $this->processor = $processor;
        $this->hub = $hub;
    }

    /**
     * @param Message $message
     * @param array   $options
     *
     * @return bool|null
     *
     * @throws \Throwable
     */
    public function process(Message $message, array $options): ?bool
    {
        try {
            return $this->processor->process($message, $options);
        } catch (\Throwable $e) {
            $this->handleException($e, $message, $options);
        }
    }

    /**
     * @param \Throwable $e
     * @param Message    $message
     * @param array      $options
     *
     * @throws \Throwable
     */
    private function handleException(\Throwable $e, Message $message, array $options): void
    {
        $properties = $message->getProperties();

        $messageBody = $message->getBody();
        $queue = $options['queue'];
        $routingKey = $properties['routing_key'];

        $this->hub->withScope(function (Scope $scope) use ($messageBody, $queue, $routingKey, $e): void {
            $scope->setExtra('message', $messageBody);
            $scope->setExtra('queue', $queue);
            $scope->setExtra('routing_key', $routingKey);

            $this->hub->captureException($e);
        });

        throw $e;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions