• Resolved bryanwk

    (@bryanwilsonkheng)


    I would like to block a few events from going to Sentry using the before_send callback, however the events kept on showing on Sentry. I have followed the approach on this topic, but seems it still doesn’t work. Here are the code snippet on my wp-sentry.php to block the issues, could someone point out the mistake?

    add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
    	$options->setBeforeSendCallback( function ( \Sentry\Event $event ) {
    		$exceptions = $event->getExceptions();
    		
    		// Little helper and fallback for PHP versions without the str_contains function
    		$strContainsHelper = function ( $haystack, $needle ) {
    			if ( function_exists( 'str_contains' ) ) {
    				return str_contains( $haystack, $needle );
    			}
    
    			return $needle !== '' && mb_strpos( $haystack, $needle ) !== false;
    		};
    		
    		// No exceptions in the event? Send the event to Sentry, it's most likely a log message
    		if ( empty( $exceptions ) ) {
    			return $event;
    		}
    
    		if ( $strContainsHelper( $exceptions[0]->getType(), 'UnhandledRejection') && $strContainsHelper( $exceptions[0]->getValue(), 'Non-Error promise rejection captured with value: ')) {
    			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 $event;
    		}
    		
    		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 ( $strContainsHelper( $frame->getAbsoluteFilePath(), 'utils/build/esm/supports' ) ) {
    				return null;
                }
                
                if ( $strContainsHelper( $frame->getAbsoluteFilePath(), 'addthis_widget' ) ) {
    				return null;
    			}
    
    			if ( $strContainsHelper( $frame->getAbsoluteFilePath(), 'plugins/addthis') ) {
    				return null;
    			}
    		}
    
    		// Stacktrace contained no frames in our theme and/or plugin? We send nothing to Sentry
    		return null;
    	} );
    
    	return $options;
    } );

    The stacktrace shows that system>exception>stack-trace>frame>module contains 'utils/build/esm/supports' and 'addthis_widget'for the two issues I’m trying to filter out.

    • This topic was modified 2 years ago by bryanwk.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author stayallive

    (@stayallive)

    You do not mention what exceptions you want to block, but from your code snippet… are the events you are getting possibly JavaScript exceptions? The before send callback you are setting here only works on PHP exceptions.

    Thread Starter bryanwk

    (@bryanwilsonkheng)

    Hi Alex, thanks for the reply.

    After ur question, I am pretty sure the exceptions I’d like to block are Javascript exceptions. If so, how would I be able to block out these exceptions instead?

    Plugin Author stayallive

    (@stayallive)

    There is a (very lightly documented) ignoreErrors option in the JavaScript SDK, you can configure that like this:

    add_filter( 'wp_sentry_public_options', function ( array $options ) {
    	return array_merge( $options, array(
    		'ignoreErrors' => array(
    			'Non-Error promise rejection captured with value',
    		),
    	));
    } );

    I have to admit I did not had the time to test this, however it “should” work ??

    Documentation links:
    https://github.com/stayallive/wp-sentry#wp_sentry_public_options-array
    https://docs.sentry.io/platforms/javascript/configuration/filtering/#decluttering-sentry

    Thread Starter bryanwk

    (@bryanwilsonkheng)

    Thanks for the reply. I have modified the code to adjust the Javascript exception, however, the events are still going through to Sentry for now. I have clearly stated the ignoreErrors for errors and ignoreUrls for url of the exceptions. Am I missing anything?

    add_filter( 'wp_sentry_public_options', function ( array $options ) {
    	return array_merge( $options, array(
    		'ignoreErrors' => array(
    			'Non-Error promise rejection captured with',
    			"TypeError: undefined is not an object",
    			'Cannot read properties of null (reading',
    		),
    		'denyUrls' => array(
    			'https://browser.sentry-cdn.com/utils/build/esm/supports.js',
    			'https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-520b1c5c65b546e6',
    			'regex:extensions\//i',
    			'regex:^chrome:\/\//i'
    		),
    	));
    } );
    • This reply was modified 2 years ago by bryanwk.
    Plugin Author stayallive

    (@stayallive)

    Hmm, that does look correct to me seeing the example app Sentry provides.

    It also looks like the code you added is good, I have for testing placed your snippet in my theme’s functions.php it needs to run before the header is rendered in the theme, if you search for wp-sentry-browser-js-extra in the HTML of your pages do you see your ignoreErrors and/or denyUrls listed?

    Otherwise it might be that the strings must match exact error messages instead of partials, we should probably add the option to make sure we allow regex’s in the ignoreErrors option.

    Thread Starter bryanwk

    (@bryanwilsonkheng)

    I have similarly placed the snippet in functions.php, yet the events are still not being filtered out. When I tried searching for wp-sentry-browser-js-extra in the HTML, there is no results, however I do see the Sentry.init() code containing dsn and environment variable which is declared in wp-config.php. If these values are successfully inserted, it would perhaps be possible to inject the ignoreErrors within those just like how JS did, correct?

    I have also tried to match the exact the error message but there seems no resolve.

    In addition, I am curious whether there is a before_send-equivalent to dealing with these JS Exceptions through php, instead of an add_filter.

    Plugin Author stayallive

    (@stayallive)

    I am not too sure anymore what you want to filter. So let’s try and get it straight because I feel like you are mixing 2 things or we are not understanding each other.

    There are 2 Sentry trackers in this plugin, the PHP and JavaScript/Browser tracker.

    The PHP tracker can be partially configured using constants and the full PHP SDK options can be configured with a filter.

    The JavaScript/Browser tracker can also be partially configured using contants and custom simple JavaScript/Browser SDK options can be configured with a filter. The filter and constants can only do simple options but not callbacks like beforeSend since PHP is not executed in the Browser SDK, so if you want to configure options like that you can inject some JavaScript to take full control over the JavaScript SDK options and do whatever you need like setting a beforeSend callback.

    Hope that helps!

    Thread Starter bryanwk

    (@bryanwilsonkheng)

    Thanks for the response.

    To be clear, I would like to filter the Javascript tracker, filtering errors I have declared in the code above, following this guide. After you mentioned that the errors might not be recognized if it does not match the exact error message, I have further added some regex strings following the errors after the v6.10.0 update, however it still could not filter out the errors I want.

    I have also tried placing the code snippet in multiple locations. The theme code (functions.php), plugin code, and also wp-config.php (right after the Sentry configurations. Might it been an issue related to the code being ran after WordPress loads? Or would there be some other configurations I missed while setting up the Sentry? Thanks

    Plugin Author stayallive

    (@stayallive)

    For future readers, Bryan and I exchanged a few e-mails and we figured out another developer left a Sentry.init() call in the theme code causing the setting from the WordPress plugin to be discarded resulting in the filtering not working correctly. The fix was removing the extra Sentry.init() call and everything worked as expected. So if you have a similar issue make sure you don’t have a Sentry.init() call in your own theme or plugin code.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Surpressing issues from going to Sentry’ is closed to new replies.