Skip to content

wp_sentry_before_send: $event->getLevel() result can be null #241

Description

@astratagem

I am trying to set up event filtering using wp_sentry_before_send but am encountering some rather confusing behavior where the error event has no level for comparing against a Sentry\Severity object. This behavior was what prompted me to open #240, but after diving deeper, I'm not so sure.

Following the example in the readme, I added the following near the top of plugins/hello.php:

$this->functionFailsForSure();

And here is my filter, which lives in mu-plugins/000-monitoring.php:

add_filter( 'wp_sentry_before_send', function ( \Sentry\Event $event, ?\Sentry\EventHint $hint = null ) {
    if (($hint === null) || ($hint->exception === null)) {
        return $event;
    }

    var_dump($hint->exception);
    var_dump($event->getLevel());

    $level = $event->getLevel();
    $isWarning = $level === \Sentry\Severity::warning();
    $isPluginHelloDolly = strpos($hint->exception->getFile(), 'plugins/hello.php') !== false;

    if ( $isWarning && $isPluginHelloDolly ) {
        return null;
    }

    return $event;
}, 10, 2 );

On the site homepage we have the output from var_dump().

Here is $hint->exception:

/var/www/html/wp-content/mu-plugins/000-monitoring.php:44:
object(Error)[1008]
  protected 'message' => string 'Using $this when not in object context' (length=38)
  private string 'string' => string '' (length=0)
  protected 'code' => int 0
  protected string 'file' => string '/var/www/html/wp-content/plugins/hello.php' (length=42)
  protected int 'line' => int 20
  private array 'trace' => 
    array (size=5)
      0 => 
        array (size=3)
          'file' => string '/var/www/html/wp-settings.php' (length=29)
          'line' => int 560
          'function' => string 'include_once' (length=12)
      1 => 
        array (size=4)
          'file' => string '/var/www/html/wp-config.php' (length=27)
          'line' => int 227
          'args' => 
            array (size=1)
              ...
          'function' => string 'require_once' (length=12)
      2 => 
        array (size=4)
          'file' => string '/var/www/html/wp-load.php' (length=25)
          'line' => int 50
          'args' => 
            array (size=1)
              ...
          'function' => string 'require_once' (length=12)
      3 => 
        array (size=4)
          'file' => string '/var/www/html/wp-blog-header.php' (length=32)
          'line' => int 13
          'args' => 
            array (size=1)
              ...
          'function' => string 'require_once' (length=12)
      4 => 
        array (size=4)
          'file' => string '/var/www/html/index.php' (length=23)
          'line' => int 17
          'args' => 
            array (size=1)
              ...
          'function' => string 'require' (length=7)
  private ?Throwable 'previous' => null

And then $event->getLevel():

/var/www/html/wp-content/mu-plugins/000-monitoring.php:45:null

If $event->getLevel() can sometimes result in a null value even when $hint?->exception !== null, how can I reliably filter events by log level?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions