-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to filter out events #89
Comments
I fully agree with the ability to filter out the errors in PHP and some of these are already in the Sentry module as well. Currently we have And While you could add a plugin on the initialize function and do it that way i believe events will make it more extendable. |
Yes |
Example extension;
<?xml version="1.0" ?>
<config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sentry_before_init">
<observer disabled="true" name="elgentos_sentrybeforeinithooks_observer_sentry_beforeinit_sentry_before_init" instance="Elgentos\SentryBeforeInitHooks\Observer\Sentry\BeforeInit"/>
</event>
</config>
<?php
declare(strict_types=1);
namespace Elgentos\SentryBeforeInitHooks\Observer\Sentry;
class BeforeInit implements \Magento\Framework\Event\ObserverInterface
{
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$observer->getEvent()->getConfig()->setBeforeSend(function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
$exceptionMessageToFilter = 'Illegal string offset \'attribute\'';
if ($hint !== null && $hint->exception !== null && stripos($hint->exception->getMessage(), $exceptionMessageToFilter) !== false) {
return null;
}
return $event;
});
}
} |
We can apply a similar strategy for frontend events. We now override <script>
Sentry.init({
dsn: '<?= $block->escapeUrl(trim($block->getDSN())) ?>',
release: '<?= $block->escapeHtml(trim($block->getVersion())) ?>',
environment: '<?= $block->escapeHtml(trim($block->getEnvironment())) ?>',
beforeSend: function(event) {
// do some filter on the event here
return event;
}
});
</script> But there should be a cleaner way to approach this. Since this is client-side code, we could also create a field for this in the HTML, similar to the "Miscellaneous HTML" field in Magento. |
@peterjaap Does
|
@convenient yes, but that's only for the Javascript part. |
Hi there. I saw that the PR to fire the event Here's what I did:
<?php declare(strict_types=1);
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'MyVendor_SentryFilters', __DIR__);
<?xml version="1.0"?>
<config
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"
>
<module name="MyVendor_SentryFilters" setup_version="1.0.0">
<sequence>
<module name="JustBetter_Sentry"/>
</sequence>
</module>
</config>
<?xml version="1.0" ?>
<config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sentry_before_init">
<observer
name="MyVendor_SentryFilters_Observer_Sentry_BeforeInit_sentry_before_init"
instance="MyVendor\SentryFilters\Observer\Sentry\BeforeInit"
/>
</event>
</config>
<?php declare(strict_types=1);
namespace MyVendor\SentryFilters\Observer\Sentry;
class BeforeInit implements \Magento\Framework\Event\ObserverInterface
{
private const FILTERED_EXCEPTION_MESSAGES = [
'Environment emulation nesting is not allowed.',
'Could not find a cart with ID',
'ReCaptcha validation failed, please try again',
'No such entity.',
'The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.',
];
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$observer->getEvent()->getConfig()->setBeforeSend(function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
foreach (self::FILTERED_EXCEPTION_MESSAGES as $exceptionMessageToFilter) {
if ($hint !== null && $hint->exception !== null && stripos($hint->exception->getMessage(), $exceptionMessageToFilter) !== false) {
return null;
}
}
return $event;
});
}
} |
@maxacarvalho at first glance that looks fine to me. Did you xdebug to see the event actually gets fired and check what's in its payload? Maybe you can try removing the |
Here's the code that works for us;
|
It looks like it should work indeed, is the observer being executed? It'll allow multiple observers in different modules to be able to filter and change events before being sent. |
I've merged it in, it is in release 3.4.0 right now. |
@indykoning nice! |
Sometimes we encounter a bug but it takes a while for us to fix it (due to whatever reason).
We'd like to temporarily filter this event from turning up in Sentry. We could do this by ignoring the event in Sentry, but incoming events will still count towards our quota. Some of these events actually eat up almost all of our quota for a given month, so we want to filter them on the Magento end instead of in Sentry.
Sentry offers a basic way to filter incoming events but that isn't very granular, thereby sometimes accidentally filtering other events too.
We could pretty easily filter this inside PHP, see Filtering for PHP.
The way we do this now is creating a Composer patch that adds a bit of logic to
vendor/justbetter/magento2-sentry/Plugin/GlobalExceptionCatcher.php
. Like this on line 55 (right before$this->sentryInteraction->initialize($config);
);I'd like to introduce a user-friendlier way to achieve this. My suggestion would be to add an event like
sentry_config
that we could hook into to add something to the$config
array (which then should be encapsulated in an object).The text was updated successfully, but these errors were encountered: