-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added context sent to the Sentry to provide more information for debu…
…gging
- Loading branch information
1 parent
08430bf
commit d4a1ba9
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |