Closed jonathancaffeine closed 6 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.
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;
} );
}
setBeforeSendCallback is not working correctly. I tried to return null but the sentry still sending the events