Skip to content

Commit

Permalink
Merge pull request #5 from justbetter/feature/add-option-for-loading-…
Browse files Browse the repository at this point in the history
…external-messages

Add option to load messages from an external link
  • Loading branch information
rbnmulder authored Oct 23, 2024
2 parents 7deced4 + c17fd20 commit f746945
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Model/Cache/Type/SentryFilterEventsCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace JustBetter\SentryFilterEvents\Model\Cache\Type;

use Magento\Framework\App\Cache\Type\FrontendPool;
use Magento\Framework\Cache\Frontend\Decorator\TagScope;

class SentryFilterEventsCache extends TagScope
{
const TYPE_IDENTIFIER = 'justbetter_sentry_filter_events';

const CACHE_TAG = 'JUSTBETTER_SENTRY_FILTER_EVENTS';

public function __construct(FrontendPool $cacheFrontendPool)
{
parent::__construct(
$cacheFrontendPool->get(self::TYPE_IDENTIFIER),
self::CACHE_TAG
);
}
}
59 changes: 55 additions & 4 deletions Observer/BeforeSending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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 [];
}
}
}
9 changes: 7 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
<group id="event_filtering" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Sentry event filtering</label>
<comment><![CDATA[Prevent events from being dispatched to Sentry by filtering on exception message contents]]></comment>
<field id="messages" translate="label" type="text" sortOrder="10" showInDefault="1"
<field id="default_messages_external_location" translate="label" type="text" sortOrder="10" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>External link with default event messages</label>
<comment>Default Event messages that should be filtered for every project (in JSON format)</comment>
</field>
<field id="messages" translate="label" type="text" sortOrder="20" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Messages</label>
<frontend_model>JustBetter\SentryFilterEvents\Block\Adminhtml\System\Config\Form\Field\Messages</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>Event messages to filter</comment>
<comment>Custom Event messages to filter</comment>
</field>
</group>
</section>
Expand Down
7 changes: 7 additions & 0 deletions etc/cache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="justbetter_sentry_filter_events" translate="label,description" instance="JustBetter\SentryFilterEvents\Model\Cache\Type\SentryFilterEventsCache">
<label>Sentry Filter Events</label>
<description>Node for caching external messages</description>
</type>
</config>

0 comments on commit f746945

Please sign in to comment.