Skip to content
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

Initial commit module - basic working module #1

Merged
merged 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/Messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace JustBetter\SentryFilterEvents\Block\Adminhtml\System\Config\Form\Field;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
* Class Messages
*/
class Messages extends AbstractFieldArray
{

rbnmulder marked this conversation as resolved.
Show resolved Hide resolved
/**
* Initialise form fields
*
* @return void
*/
protected function _construct(): void
{
$this->addColumn('message', ['label' => __('Event Message')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');

parent::_construct();
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog
## [1.0.0] - 2023-06-15
### Added
* Initial commit module - basic working module
46 changes: 46 additions & 0 deletions Observer/BeforeSending.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace JustBetter\SentryFilterEvents\Observer;

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;

class BeforeSending implements ObserverInterface
{
/**
* @param ScopeConfigInterface $scopeConfig
* @param Json $json
*/
public function __construct(
protected ScopeConfigInterface $scopeConfig,
protected Json $json
) { }

/**
* Filter event before dispatching it to sentry
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer): void
{
$event = $observer->getEvent()->getSentryEvent()->getEvent();
$hint = $observer->getEvent()->getSentryEvent()->getHint();

$hintMessage = $hint->exception->getMessage();
if ($hint->exception instanceof LocalizedException) {
$hintMessage = $hint->exception->getRawMessage();
$event->setMessage($hintMessage);
}

$messages = $this->json->unserialize($this->scopeConfig->getValue('sentry/event_filtering/messages'));
foreach ($messages as $message) {
if (str_contains($hintMessage, $message['message'])) {
$observer->getEvent()->getSentryEvent()->unsEvent();
}
}
}
}
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# magento2-sentry-filter-events
# Magento 2 Sentry Filter Events

This Magento 2 module makes it possible to filter Sentry events by exception message.

## Installation
- `composer require justbetter/magento2-sentry-filter-events`
- `bin/magento module:enable JustBetter_SentryFilterEvents`
- `bin/magento setup:upgrade`
- `bin/magento setup:di:compile`
- `bin/magento setup:static-content:deploy`

## Configuration
This module adds the following configuration field to the already existing JustBetter Sentry configuration:
`Stores > Configuration > JustBetter > Sentry configuration > Sentry event filtering`.

### Configuration values
* `messages`: Here you can specify the exception messages you want to exclude from being dispatched to Sentry

## Compatibility
The module is tested on Magento version 2.4.x with sentry sdk version 3.x.

## Ideas, bugs or suggestions?
Please create a [issue](https://github.com/justbetter/magento2-sentry-filter-events/issues) or a [pull request](https://github.com/justbetter/magento2-sentry-filter-events/pulls).

## About us
We’re a innovative development agency from The Netherlands building awesome websites, webshops and web applications with Laravel and Magento. Check out our website [justbetter.nl](https://justbetter.nl) and our [open source projects](https://github.com/justbetter).

## License
[MIT](LICENSE)

---

<a href="https://justbetter.nl" title="JustBetter"><img src="https://raw.githubusercontent.com/justbetter/art/master/justbetter-logo.png" width="200px" alt="JustBetter logo"></a>
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "justbetter/magento2-sentry-filter-events",
"description": "Magento 2 Filter for Sentry Events",
"type": "magento2-module",
"version": "1.0.0",
rbnmulder marked this conversation as resolved.
Show resolved Hide resolved
"license": "MIT",
"require": {
"php": ">=8.0",
"justbetter/magento2-sentry": "^3.4"
},
"authors": [
{
"name": "Robin Mulder",
"email": "robin@justbetter.nl",
"homepage": "https://justbetter.nl",
"role": "Developer"
},
{
"name": "Indy Koning",
"email": "indy@justbetter.nl",
"homepage": "https://justbetter.nl",
"role": "Developer"
}
],
"autoload": {
"psr-4": { "JustBetter\\SentryFilterEvents\\": "" },
"files": [ "registration.php" ]
}
}
18 changes: 18 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="sentry">
<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"
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>
</field>
</group>
</section>
</system>
</config>
10 changes: 10 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<sentry>
<event_filtering>
<messages>{"_1686819911779_779":{"message":"The session has expired, please login again."},"_1686820213917_917":{"message":"The requested qty is not available"},"_1686820227732_732":{"message":"The path is not allowed:"},"_1686820300850_850":{"message":"Some of the products are out of stock."},"_1686820331567_567":{"message":"The product that was requested doesn't exist. Verify the product and try again."}}</messages>
</event_filtering>
</sentry>
</default>
</config>
6 changes: 6 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sentry_before_send">
<observer name="filter_sentry_events_before_sending" instance="JustBetter\SentryFilterEvents\Observer\BeforeSending"/>
</event>
</config>
8 changes: 8 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="JustBetter_SentryFilterEvents" setup_version="1.0.0">
<sequence>
<module name="JustBetter_Sentry" />
</sequence>
</module>
</config>
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'JustBetter_SentryFilterEvents',
__DIR__
);