Skip to content

Commit

Permalink
added context sent to the Sentry to provide more information for debu…
Browse files Browse the repository at this point in the history
…gging
  • Loading branch information
TomasLudvik committed Dec 20, 2024
1 parent 08430bf commit d4a1ba9
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Component/AddSentryContextSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Component;

use App\Environment;
use RedisException;
use Sentry\State\Scope;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Domain\Exception\NoDomainSelectedException;
use Shopsys\FrameworkBundle\Component\Localization\DisplayTimeZoneProviderInterface;
use Shopsys\FrameworkBundle\Component\Maintenance\MaintenanceModeSubscriber;
use Shopsys\FrameworkBundle\Component\Redis\RedisClientFacade;
use Shopsys\FrameworkBundle\Component\Redis\RedisFacade;
use Shopsys\FrameworkBundle\ShopsysFrameworkBundle;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpKernel\KernelEvents;
use function Sentry\configureScope;

class AddSentryContextSubscriber implements EventSubscriberInterface
{
/**
* @param \Shopsys\FrameworkBundle\Component\Redis\RedisClientFacade $redisClientFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Component\Localization\DisplayTimeZoneProviderInterface $displayTimeZoneProvider
* @param \Shopsys\FrameworkBundle\Component\Redis\RedisFacade $redisFacade
*/
public function __construct(
protected readonly RedisClientFacade $redisClientFacade,
protected readonly Domain $domain,
protected readonly DisplayTimeZoneProviderInterface $displayTimeZoneProvider,
protected readonly RedisFacade $redisFacade,
) {
}

public function setSentryContext(): void
{
$context = [
'environment' => class_exists('App\Environment') ? Environment::getEnvironment() : '',
'version' => ShopsysFrameworkBundle::VERSION,
'systemTimeZone' => date_default_timezone_get(),
];

try {
$this->redisFacade->pingAllClients();

$context['maintenance-page'] = $this->redisClientFacade->contains(MaintenanceModeSubscriber::MAINTENANCE_KEY);
} catch (RedisException) {
}

try {
$context['currentDomainId'] = $this->domain->getId();
$context['currentDomainName'] = $this->domain->getName();
$context['currentDomainLocale'] = $this->domain->getLocale();
$context['displayTimeZone'] = $this->displayTimeZoneProvider->getDisplayTimeZoneByDomainId($this->domain->getId())->getName();
} catch (NoDomainSelectedException | FileNotFoundException) {
}

configureScope(function (Scope $scope) use ($context): void {
$scope->setContext('Shopsys', $context);
});
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'setSentryContext',
ConsoleEvents::COMMAND => 'setSentryContext',
];
}
}

0 comments on commit d4a1ba9

Please sign in to comment.