Improve clarity of $unformatted_message variable in log ignore filtering

Created on 29 April 2025, 10 days ago

Problem/Motivation

I have been facing multiple events being created on Sentry and using all my quota. To improve this, I tried to ignore some messages using ignored_messages from the config form. But I noticed the same messages I tried to ignore were still creating events.
I initially assumed that the value to be ignored should match the @message seen in the Sentry dashboard. Uppon debugging the log method, I found out that currently the code checks if $unformatted_message is on the $config->get('ignored_messages') array, but the $unformatted_message is too generic (%type: @message in %function (line %line of %file).) and does not match the actual error message.

Steps to reproduce

1. Add "The theme must be passed as a query argument" to the Ignored_messages config form.
2. Access https://example.site/sites/default/files/css/test4.css
3. The event will be created on Sentry

Proposed resolution

Filter ignored messages by $context['@message'] rather than unformated message

πŸ› Bug report
Status

Active

Version

6.0

Component

Code

Created by

πŸ‡§πŸ‡·Brazil PabloNicolas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @PabloNicolas
  • Pipeline finished with Failed
    10 days ago
    Total: 148s
    #484841
  • Pipeline finished with Success
    10 days ago
    Total: 154s
    #484860
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    We intentionally match the generic unformatted/uninterpolated message because that is often necessary. For example, we don't want to have to ignore a message for each user logging in, but rather for "[user] logged in". So, we can't make this change.

    To filter events the way you want you'll need to write a little code to set the before_send callback option, which allows you to provide arbitrary custom logic for filtering events; the ignore_exceptions option is also available.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Thinking thru some pros and cons of various solutions for this:

    If we check both the unformatted and formatted message in ignored_messages, then it would be easy to ignore specific errors. However, whenever the line number or absolute path of the file changes, the error would no longer be ignored. Therefore, this would be a pretty brittle solution - it doesn't seem particularly great to me

    Another idea would be to check both the unformatted message and, if it exists, $context['@message']. This would be a more general rule, ignoring new cases of the error that appear, regardless of where it appears. So, it seems more useful as a filter, although certainly could cause people to unintentionally miss new errors.

  • πŸ‡§πŸ‡·Brazil PabloNicolas

    I managed to resolve my specific issue by creating an event subscriber to OptionsAlter:

    <?php
    
    namespace Drupal\example\EventSubscriber;
    
    use Drupal\raven\Event\OptionsAlter;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Sentry\Event;
    use Sentry\EventHint;
    
    class SentryOptionsAlterSubscriber implements EventSubscriberInterface {
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents(): array {
        return [
          OptionsAlter::class => 'onOptionsAlter',
        ];
      }
    
      /**
       * Alter Sentry options before client initialization.
       */
      public function onOptionsAlter(OptionsAlter $event): void {
        $options = &$event->options;
    
        $options['before_send'] = function (Event $event, ?EventHint $hint): ?Event {
          $ignored = \Drupal::config('raven.settings')->get('ignored_messages') ?? [];
          if (
            $hint !== null &&
            isset($hint->exception) &&
            method_exists($hint->exception, 'getMessage') &&
            in_array($hint->exception->getMessage(), $ignored, true)
          ) {
            return null;
          }
    
          return $event;
        };
      }
    }
    

    But I think we can replicate it on the module itself, and with some tweaks here and there would give us more flexibility on how we ignore these specific messages.

  • Pipeline finished with Failed
    1 day ago
    Total: 171s
    #492252
  • Pipeline finished with Success
    1 day ago
    Total: 168s
    #492256
  • Pipeline finished with Success
    1 day ago
    Total: 153s
    #492304
  • Pipeline finished with Success
    about 23 hours ago
    Total: 153s
    #492416
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Sorry for the miscommunication: Subscribing to the event and setting the before_send callback is something you should do in your custom code to provide the functionality you're looking for.

    It should not be done in this module.

Production build 0.71.5 2024