Skip to content

Commit

Permalink
feat: add shopware exception ignoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Mar 4, 2024
1 parent f41f7a4 commit 0b27798
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Shopware Sentry Bundle

This plugin integrates the [Sentry](https://sentry.io) error tracking service into Shopware 6.

## Installation

```bash
composer require frosh/sentry-bundle
```

## Configuration

After installation, create a `config/packages/sentry.yaml` file in your Shopware installation and add the following configuration:

```yaml
parameters:
env(SENTRY_DSN): ''
env(SENTRY_RELEASE): ''

# Tells Shopware to forward traces to Sentry
shopware:
profiler:
integrations:
- Sentry

sentry:
dsn: "%env(SENTRY_DSN)%"
tracing:
enabled: true
dbal:
enabled: false
cache:
enabled: false
twig:
enabled: false
http_client:
enabled: true
messenger:
enabled: true
options:
integrations:
# Use default exception ignore list of Shopware
- 'Frosh\SentryBundle\Integration\UseShopwareExceptionIgnores'
environment: '%kernel.environment%'
release: '%env(SENTRY_RELEASE)%'
# Trace 10% of requests
traces_sample_rate: 0.1
sample_rate: 0.1
```
### Pictures
![img](https://i.imgur.com/KUwUkxA.png)
![img](https://i.imgur.com/Jm7tjqB.png)
19 changes: 19 additions & 0 deletions src/CompilerPass/ExceptionConfigCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Frosh\SentryBundle\CompilerPass;

use Shopware\Core\Framework\DependencyInjection\CompilerPass\CompilerPassConfigTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ExceptionConfigCompilerPass implements CompilerPassInterface
{
use CompilerPassConfigTrait;

public function process(ContainerBuilder $container): void
{
$config = $this->getConfig($container, 'framework');

$container->setParameter('frosh_sentry.exclude_exceptions', $config['exceptions']);
}
}
39 changes: 39 additions & 0 deletions src/Integration/UseShopwareExceptionIgnores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Frosh\SentryBundle\Integration;

use Sentry\Event;
use Sentry\Integration\IntegrationInterface;
use Sentry\State\Scope;

class UseShopwareExceptionIgnores implements IntegrationInterface
{
public function __construct(private readonly array $exceptions)
{
}

public function setupOnce(): void
{
$exceptions = $this->exceptions;

Scope::addGlobalEventProcessor(function (Event $event) use($exceptions): ?Event {
$eventExceptions = $event->getExceptions()[0];

if ($eventExceptions === null) {
return $event;
}

$config = $exceptions[$eventExceptions->getType()] ?? [];

if (!isset($config['log_level'])) {
return $event;
}

if ($config['log_level'] === 'notice') {
return null;
}

return $event;
});
}
}
41 changes: 41 additions & 0 deletions src/Listener/FixRequestUrlListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Frosh\SentryBundle\Listener;

use Sentry\State\HubInterface;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
* This listener overwrites the URL determined by Sentry to the original URL of the request.
*/
class FixRequestUrlListener
{
public function __construct(private readonly HubInterface $hub)
{

}

public function __invoke(RequestEvent $event): void
{
$span = $this->hub->getSpan();

if ($span === null) {
return;
}

if ($span->getOp() !== 'http.server') {
return;
}

$request = $event->getRequest();

$url = $request->attributes->get(RequestTransformer::ORIGINAL_REQUEST_URI);

if ($url === null) {
return;
}

$span->setData(['http.url' => $request->getSchemeAndHttpHost() . $url]);
}
}
17 changes: 17 additions & 0 deletions src/ShopwareSentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
namespace Frosh\SentryBundle;

use Frosh\SentryBundle\Instrumentation\SentryProfiler;
use Frosh\SentryBundle\Integration\UseShopwareExceptionIgnores;
use Frosh\SentryBundle\Listener\FixRequestUrlListener;
use Frosh\SentryBundle\Listener\SalesChannelContextListener;
use Shopware\Core\System\SalesChannel\Event\SalesChannelContextCreatedEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Sentry\State\HubInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

class ShopwareSentryBundle extends Bundle
{
Expand All @@ -17,8 +23,19 @@ public function build(ContainerBuilder $container): void
->addTag('kernel.event_listener', ['event' => SalesChannelContextCreatedEvent::class, 'method' => '__invoke'])
->addTag('kernel.reset', ['method' => 'reset']);

$container
->register(FixRequestUrlListener::class)
->addArgument(new Reference(HubInterface::class))
->addTag('kernel.event_listener', ['event' => RequestEvent::class, 'method' => '__invoke', 'priority' => 3]);

$container
->register(SentryProfiler::class)
->addTag('shopware.profiler', ['integration' => 'Sentry']);

$container
->register(UseShopwareExceptionIgnores::class)
->addArgument('%frosh_sentry.exclude_exceptions%');

$container->addCompilerPass(new CompilerPass\ExceptionConfigCompilerPass());
}
}

0 comments on commit 0b27798

Please sign in to comment.