diff --git a/CHANGELOG.md b/CHANGELOG.md index d9809e2..5dd63e3 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog +## [1.1.0] - 2024-10-22 +### Added +* Add option for loading messages from an external location ## [1.0.0] - 2023-06-15 ### Added * Initial commit module - basic working module diff --git a/Model/Cache/Type/SentryFilterEventsCache.php b/Model/Cache/Type/SentryFilterEventsCache.php new file mode 100644 index 0000000..84042c4 --- /dev/null +++ b/Model/Cache/Type/SentryFilterEventsCache.php @@ -0,0 +1,21 @@ +get(self::TYPE_IDENTIFIER), + self::CACHE_TAG + ); + } +} diff --git a/Observer/BeforeSending.php b/Observer/BeforeSending.php index 7ca51df..dc594f0 100644 --- a/Observer/BeforeSending.php +++ b/Observer/BeforeSending.php @@ -2,22 +2,31 @@ namespace JustBetter\SentryFilterEvents\Observer; +use Laminas\Http\Request; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Serialize\Serializer\Json; +use Magento\Framework\HTTP\Adapter\CurlFactory; +use JustBetter\SentryFilterEvents\Model\Cache\Type\SentryFilterEventsCache; +use InvalidArgumentException; class BeforeSending implements ObserverInterface { + protected const CACHE_TTL = 604800; /** * @param ScopeConfigInterface $scopeConfig * @param Json $json */ public function __construct( - protected ScopeConfigInterface $scopeConfig, - protected Json $json - ) { } + protected ScopeConfigInterface $scopeConfig, + protected Json $json, + protected CurlFactory $curlFactory, + protected SentryFilterEventsCache $cache + ) + { + } /** * Filter event before dispatching it to sentry @@ -38,7 +47,8 @@ public function execute(Observer $observer): void $observer->getEvent()->getSentryEvent()->setEvent($event); } - $messages = $this->json->unserialize($this->scopeConfig->getValue('sentry/event_filtering/messages')); + $messages = array_merge($this->getCustomMessages(), $this->getDefaultMessages()); + foreach ($messages as $message) { if (str_contains($hintMessage, $message['message'])) { $observer->getEvent()->getSentryEvent()->unsEvent(); @@ -47,4 +57,45 @@ public function execute(Observer $observer): void } } + + private function getCustomMessages(): array + { + return array_values($this->json->unserialize($this->scopeConfig->getValue('sentry/event_filtering/messages'))); + } + + private function getDefaultMessages(): array + { + if ($cachedDefaultMessages = $this->cache->load(SentryFilterEventsCache::TYPE_IDENTIFIER)) { + return $this->json->unserialize((string)$cachedDefaultMessages, true) ?? []; + } + + $url = $this->scopeConfig->getValue('sentry/event_filtering/default_messages_external_location'); + + if (!$url) { + return []; + } + + /** @var \Magento\Framework\HTTP\Adapter\Curl $curl */ + $curl = $this->curlFactory->create(); + $curl->setOptions([ + 'timeout' => 10, + 'header' => false, + 'verifypeer' => false + ]); + $curl->write(Request::METHOD_GET, $url, '1.1', []); + + $responseData = $curl->read(); + + if ($responseData === false || !$responseData) { + return []; + } + $curl->close(); + $this->cache->save($responseData, SentryFilterEventsCache::TYPE_IDENTIFIER, [SentryFilterEventsCache::CACHE_TAG], self::CACHE_TTL); + + try { + return $this->json->unserialize((string)$responseData, true) ?? []; + } catch (InvalidArgumentException) { + return []; + } + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index bce0eb2..a780484 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -5,12 +5,17 @@ - + + Default Event messages that should be filtered for every project (in JSON format) + + JustBetter\SentryFilterEvents\Block\Adminhtml\System\Config\Form\Field\Messages Magento\Config\Model\Config\Backend\Serialized\ArraySerialized - Event messages to filter + Custom Event messages to filter diff --git a/etc/cache.xml b/etc/cache.xml new file mode 100644 index 0000000..9e1a3bd --- /dev/null +++ b/etc/cache.xml @@ -0,0 +1,7 @@ + + + + + Node for caching external messages + +