PHP code example of stayallive / wp-sentry

1. Go to this page and download the library: Download stayallive/wp-sentry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stayallive / wp-sentry example snippets


define( 'WP_SENTRY_PHP_DSN', 'PHP_DSN' );

define( 'WP_SENTRY_BROWSER_DSN', 'JS_DSN' );

// You can _optionally_ enable or disable the JavaScript tracker in certain parts of your site with these constants:
define('WP_SENTRY_BROWSER_ADMIN_ENABLED', true);    // Add the JavaScript tracker to the admin area. Default: true
define('WP_SENTRY_BROWSER_LOGIN_ENABLED', true);    // Add the JavaScript tracker to the login page. Default: true
define('WP_SENTRY_BROWSER_FRONTEND_ENABLED', true); // Add the JavaScript tracker to the front end. Default: true

define( 'WP_SENTRY_SEND_DEFAULT_PII', true );

define( 'WP_SENTRY_VERSION', 'v8.2.0' );

define( 'WP_SENTRY_ENV', 'production' );

define( 'WP_SENTRY_ERROR_TYPES', E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_USER_DEPRECATED & ~E_USER_NOTICE );

// https://docs.sentry.io/platforms/php/performance/#configure
define( 'WP_SENTRY_TRACES_SAMPLE_RATE', 0.3 ); // traces_sample_rate

define( 'WP_SENTRY_TRACING_FEATURES', [
	'db' => [
		'spans' => true,
		'breadcrumbs' => defined( 'SAVEQUERIES' ) && SAVEQUERIES,
	],
	'http' => [
		'spans' => true,
		'breadcrumbs' => true,
	],
	'transients' => [
		'spans' => true,
		'breadcrumbs' => true,
	],
] );

// https://docs.sentry.io/platforms/javascript/performance/#configure-the-sample-rate
define( 'WP_SENTRY_BROWSER_TRACES_SAMPLE_RATE', 0.3 ); // tracesSampleRate

// These options are passed directly to `new BrowserTracing({})`
// define( 'WP_SENTRY_BROWSER_TRACING_OPTIONS', [] );

// These options are injected into the `Sentry.init()` call
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
define( 'WP_SENTRY_BROWSER_REPLAYS_SESSION_SAMPLE_RATE', 0.1 ); // replaysSessionSampleRate
define( 'WP_SENTRY_BROWSER_REPLAYS_ON_ERROR_SAMPLE_RATE', 1.0 ); // replaysOnErrorSampleRate

// These options are passed directly to `new Replay({})`
// - https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
// - https://docs.sentry.io/platforms/javascript/session-replay/privacy/#privacy-configuration
// define( 'WP_SENTRY_BROWSER_SESSION_REPLAY_OPTIONS', [ 'maskAllText' => true ] );

define( 'WP_SENTRY_TRACES_SAMPLE_RATE', 0.3 ); // traces_sample_rate
// https://docs.sentry.io/platforms/php/profiling/#configure
define( 'WP_SENTRY_PROFILES_SAMPLE_RATE', 0.3 ); // profiles_sample_rate

define( 'WP_SENTRY_BROWSER_FEEDBACK_OPTIONS', [ 'enabled' => true ] );

add_filter( 'wp_sentry_user_context', function ( array $user ) {
	return array_merge( $user, array(
		'a-custom-user-meta-key' => 'custom value',
	));
} );

add_filter( 'wp_sentry_dsn', function ( $dsn ) {
	return 'https://<key>:<secret>@sentry.io/<project>';
} );

add_filter( 'wp_sentry_scope', function ( \Sentry\State\Scope $scope ) {
	$scope->setTag('my-custom-tag', 'tag-value');

	return $scope;
} );

add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
	// Only sample 90% of the events
	$options->setSampleRate(0.9);

	return $options;
} );

add_filter( 'wp_sentry_before_send', function ( \Sentry\Event $event, ?\Sentry\EventHint $hint = null ) {
    // Don't send error event with level `warning` for the Hello Dolly example plugin
    if ( $hint->exception !== null && $event->getLevel() === \Sentry\Severity::warning() && strpos( $hint->exception->getFile(), 'plugins/hello.php' ) !== false ) {
        return null;
    }
    
    return $event;
}, 2 );

add_filter( 'wp_sentry_public_dsn', function ( $dsn ) {
	return 'https://<key>@sentry.io/<project>';
} );

add_filter( 'wp_sentry_public_options', function ( array $options ) {
	return array_merge( $options, array(
		'sampleRate' => '0.5',
		'denyUrls' => array(
			'https://github.com/',
			'regex:\\w+\\.example\\.com',
		),
	));
} );

add_filter( 'wp_sentry_public_context', function ( array $context ) {
	$context['tags']['my-custom-tag'] = 'tag-value';

	return $context;
} );

define( 'WP_SENTRY_ERROR_TYPES', E_ALL & ~E_NOTICE & ~E_USER_NOTICE );

try {
	myMethodThatCanThrowAnException();
} catch ( \Throwable $e ) {
    // Option #1: Use the `captureException` or `captureMessage` action
    // It's safe to call these actions even if the plugin is disabled (it will simply do nothing)
    do_action( 'sentry/captureException', $e );
    do_action( 'sentry/captureMessage', $e->getMessage() ); // it is recommended to use `captureException`

    // Option #2: Use the `wp_sentry_safe` function to interact with the Sentry SDK directly
	// It's advised to wrap this in a function_exists check to prevent errors when the plugin is disabled
	if ( function_exists( 'wp_sentry_safe' ) ) {
		wp_sentry_safe( function ( \Sentry\State\HubInterface $client ) use ( $e ) {
			$client->captureException( $e );
		} );
	}

	wp_die( 'There was an error doing this thing you were doing, we have been notified!' );
}

$e = new Exception('Some exception I want to capture with extra data.');

if (function_exists('wp_sentry_safe')) {
	wp_sentry_safe(function (\Sentry\State\HubInterface $client) use ($e) {
		$client->withScope(function (\Sentry\State\Scope $scope) use ($client, $e) {
			$scope->setContext('user_data', $e->getData());
			$client->captureException($e);
		});
	});
}

// It's possible your WordPress installation is different, check to make sure this path is correct for your installation



/**
 * Plugin Name: WordPress Sentry
 * Plugin URI: https://github.com/stayallive/wp-sentry
 * Description: A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.
 * Version: must-use-proxy
 * Author: Alex Bouma
 * Author URI: https://alex.bouma.dev
 * License: MIT
 */

$wp_sentry = WP_CONTENT_DIR . '/plugins/wp-sentry-integration/wp-sentry.php';

// Do not crash in case the plugin is not installed
if ( ! file_exists( $wp_sentry ) ) {
	return;
}


add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
	$options->setBeforeSendCallback( function ( \Sentry\Event $event ) {
		$exceptions = $event->getExceptions();

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

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

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

		// 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;
		};

		foreach ( $stacktrace->getFrames() as $frame ) {
			// Check the the frame happened inside our theme or plugin
			// Change THEME_NAME and PLUGIN_NAME to whatever is 

add_action( 'wp_enqueue_scripts', function () {
	wp_add_inline_script( 'wp-sentry-browser', 'function wp_sentry_hook(options) { return someCheckInYourCode() ? true : false; }', 'before' );
} );

function wp_sentry_clientbuilder_callback( \Sentry\ClientBuilder $builder ): void {
    // For example, disabling the default integrations
	$builder->getOptions()->setDefaultIntegrations( false );
}

define( 'WP_SENTRY_CLIENTBUILDER_CALLBACK', 'wp_sentry_clientbuilder_callback' );