diff --git a/CHANGELOG.md b/CHANGELOG.md index dd4ed75..25d10e1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ # Changelog +## [3.6.0] - 2024-03-29 +This release drops support for php 7.4 as it has been completely EOL for over a year. [https://www.php.net/supported-versions.php](https://www.php.net/supported-versions.php) +### Fixed + * Check if Sentry is defined before running init (https://github.com/justbetter/magento2-sentry/pull/129) thanks to https://github.com/netzkollektiv +### Changed + * Use property promotions (https://github.com/justbetter/magento2-sentry/pull/130) thanks to https://github.com/cirolosapio + * Raised sentry/sdk version to 4.0+ ## [3.5.2] - 2024-03-18 ### Fixed * Fix start errors without database connection (https://github.com/justbetter/magento2-sentry/pull/125) thanks to https://github.com/fredden @@ -22,7 +29,6 @@ ## [3.2.0] - 2022-07-07 ### Fixed * Changed addAlert to addRecord for Test error (https://github.com/justbetter/magento2-sentry/pull/98) thanks to https://github.com/peterjaap - ### Added * Send context data top Sentry as Custom Data (https://github.com/justbetter/magento2-sentry/pull/97) thanks to https://github.com/oneserv-heuser ## [3.1.0] - 2022-06-14 diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index 6ce2b98..61800eb 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -6,18 +6,18 @@ // phpcs:disable Magento2.Functions.DiscouragedFunction +use function Sentry\captureException; +use function Sentry\configureScope; +use function Sentry\init; use Magento\Authorization\Model\UserContextInterface; use Magento\Backend\Model\Auth\Session as AdminSession; use Magento\Customer\Model\Session as CustomerSession; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Exception\LocalizedException; +use ReflectionClass; use Sentry\State\Scope; -use function Sentry\captureException; -use function Sentry\configureScope; -use function Sentry\init; - class SentryInteraction { public function __construct( @@ -34,7 +34,7 @@ public function initialize($config) private function canGetUserData() { try { - return @$this->appState->getAreaCode(); + return in_array(@$this->appState->getAreaCode(), [Area::AREA_ADMINHTML, Area::AREA_FRONTEND]); } catch (LocalizedException $ex) { return false; } @@ -47,8 +47,15 @@ private function getSessionUserData() } $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $reflectionClass = new ReflectionClass($objectManager); + $sharedInstances = $reflectionClass->getProperty('_sharedInstances'); + $sharedInstances->setAccessible(true); if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) { + if (!array_key_exists(ltrim(AdminSession::class, '\\'), $sharedInstances->getValue($objectManager))) { + // Don't intitialise session if it has not already been started, this causes problems with dynamic resources. + return []; + } $adminSession = $objectManager->get(AdminSession::class); if ($adminSession->isLoggedIn()) { @@ -61,6 +68,9 @@ private function getSessionUserData() } if ($this->appState->getAreaCode() === Area::AREA_FRONTEND) { + if (!array_key_exists(ltrim(CustomerSession::class, '\\'), $sharedInstances->getValue($objectManager))) { + return []; + } $customerSession = $objectManager->get(CustomerSession::class); if ($customerSession->loggedIn()) { @@ -83,6 +93,7 @@ public function addUserContext() $userType = null; $userData = []; + \Magento\Framework\Profiler::start('SENTRY::add_user_context'); try { $userId = $this->userContext->getUserId(); if ($userId) { @@ -90,8 +101,8 @@ public function addUserContext() } if ($this->canGetUserData() && count($userData = $this->getSessionUserData())) { - $userId = $userData['id'] || $userId; - $userType = $userData['user_type'] || $userType; + $userId = $userData['id'] ?? $userId; + $userType = $userData['user_type'] ?? $userType; unset($userData['user_type']); } @@ -114,6 +125,7 @@ public function addUserContext() }); } catch (\Throwable $e) { } + \Magento\Framework\Profiler::stop('SENTRY::add_user_context'); } public function captureException(\Throwable $ex) diff --git a/composer.json b/composer.json index 0bf93a6..bf4fcad 100755 --- a/composer.json +++ b/composer.json @@ -2,11 +2,11 @@ "name": "justbetter/magento2-sentry", "description": "Magento 2 Logger for Sentry", "type": "magento2-module", - "version": "3.5.2", + "version": "3.6.0", "license": "MIT", "require": { "php": ">=8.0", - "sentry/sdk": "^3.0", + "sentry/sdk": "^4.0", "monolog/monolog": ">=2.7.0", "magento/framework": "*", "nyholm/psr7": "^1.2",