stayallive / wp-sentry

A (unofficial) WordPress plugin reporting PHP and JavaScript errors to Sentry.
https://wordpress.org/plugins/wp-sentry-integration/
MIT License
300 stars 48 forks source link

setBeforeSendCallback is not working correctly #184

Closed jonathancaffeine closed 3 months ago

jonathancaffeine commented 4 months ago

setBeforeSendCallback is not working correctly. I tried to return null but the sentry still sending the events

stayallive commented 4 months ago

Show us the code you have create to set the before send hook. And also share as much of the event that is still coming through.

jonathancaffeine commented 4 months ago

Here's my code:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

 // Little helper and fallback for PHP versions without the str_contains function
 function mmio_str_contains ( $haystack, $needle ) {
    if ( function_exists( 'str_contains' ) ) {
        return str_contains( $haystack, $needle );
    }

    return $needle !== '' && mb_strpos( $haystack, $needle ) !== false;
};

add_filter( 'wp_sentry_options', 'mmio_sentry_options' );
function mmio_sentry_options ( \Sentry\Options $options ) {

    $options->setBeforeSendCallback( function ( \Sentry\Event $event, ?\Sentry\EventHint $hint = null) {
        write_log([
            "setBeforeSendCallback"
        ]);
        $exceptions = $event->getExceptions();

        // No exceptions in the event? Send the event to Sentry, it's most likely a log message
        if ( empty( $exceptions ) ) {
            return null;
        }

        $stacktrace = $exceptions[0]->getStacktrace();

        if ( $stacktrace === null ) {
            return null;
        }

        if(
            $event->getLevel() !== \Sentry\Severity::error() &&
            $event->getLevel() !== \Sentry\Severity::fatal()
        ){
            return null;
        }

        $toSend = true;

        foreach ( $stacktrace->getFrames() as $frame ) {
            // Check the the frame happened inside our theme or plugin
            // Change THEME_NAME and PLUGIN_NAME to whatever is required
            // And / or modify this `if` statement to detect other variables
            if ( mmio_str_contains( $frame->getFile(), 'plugins/memberium2') ||
                mmio_str_contains( $frame->getFile(), 'plugins/ithemes-sync' )
            ) {
                // Send the event to Sentry
                $toSend = false;
                break;
            }
        }

        foreach ( $exceptions as $exception ) {
            if ( mmio_str_contains( $exception->getValue(), 'Undefined index')
            ) {
                // Send the event to Sentry
                $toSend = false;
                break;
            }
        }

        // Stacktrace contained no frames in our theme and/or plugin? We send nothing to Sentry
        return $toSend ? $event : null;
    } );

    return $options;
}

function mmio_sentry_before_send( \Sentry\Event $event, ?\Sentry\EventHint $hint = null ) {
    if ( $hint && $hint->exception !== null &&
        ( mmio_str_contains( $hint->exception->getFile(), 'plugins/memberium2') ||
            mmio_str_contains( $hint->exception->getFile(), 'plugins/ithemes-sync' ) )
    ){
        return null;
    }

    if(
        $event->getLevel() !== \Sentry\Severity::error() &&
        $event->getLevel() !== \Sentry\Severity::fatal()
    ){
        return null;
    }

    return $event;
}
add_filter( 'wp_sentry_before_send', 'mmio_sentry_before_send', 2 );

function mmio_sentry_client_builder( \Sentry\ClientBuilder $builder ): void {
    $builder->getOptions()->setBeforeSendCallback( function ( \Sentry\Event $event, ?\Sentry\EventHint $hint = null) {
        write_log([
            "CLIENT_BUILDER"
        ]);
        $exceptions = $event->getExceptions();

        // No exceptions in the event? Send the event to Sentry, it's most likely a log message
        if ( empty( $exceptions ) ) {
            return null;
        }       

        $stacktrace = $exceptions[0]->getStacktrace();

        // No stacktrace in the first exception? Send it to Sentry just to be safe then
        if ( $stacktrace === null ) {
            return null;
        }

        if(
            $event->getLevel() !== \Sentry\Severity::error() &&
            $event->getLevel() !== \Sentry\Severity::fatal()
        ){
            return null;
        }

        $toSend = true;

        foreach ( $stacktrace->getFrames() as $frame ) {
            // Check the the frame happened inside our theme or plugin
            // Change THEME_NAME and PLUGIN_NAME to whatever is required
            // And / or modify this `if` statement to detect other variables
            if ( mmio_str_contains( $frame->getFile(), 'plugins/memberium2') ||
                mmio_str_contains( $frame->getFile(), 'plugins/ithemes-sync' )
            ) {
                // Send the event to Sentry
                $toSend = false;
                break;
            }
        }

        foreach ( $exceptions as $exception ) {
            if ( mmio_str_contains( $exception->getValue(), 'Undefined index')
            ) {
                // Send the event to Sentry
                $toSend = false;
                break;
            }
        }

        // Stacktrace contained no frames in our theme and/or plugin? We send nothing to Sentry
        return $toSend ? $event : null;
    } );
}
Screenshot 2024-04-29 at 4 26 44 PM