Created
March 26, 2021 08:20
-
-
Save Trisky/b4f5cbf20b4446c5b3bf77d9d332bcb7 to your computer and use it in GitHub Desktop.
Wordpress Sentry performance integration mu-plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * This is a modified mu-plugin for wp-sentry that enables Sentry Performance albeit it doesn't show the stacktrace | |
| * 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 | |
| * License: MIT | |
| */ | |
| $wp_sentry = __DIR__ . '/../plugins/wp-sentry-integration/wp-sentry.php'; | |
| if (!file_exists($wp_sentry)) { | |
| trigger_error('Sentry is not being mu-loaded because the plugin files are not there'); | |
| return; | |
| } | |
| require $wp_sentry; | |
| define( 'WP_SENTRY_MU_LOADED', true ); | |
| /* | |
| * Sentry performance activation for wordpress | |
| */ | |
| $sentryDsn = defined('WP_SENTRY_DSN') && (WP_SENTRY_DSN !== false); | |
| $sentryTracingRate = defined('WP_SENTRY_TRACES_SAMPLE_RATE') ? WP_SENTRY_TRACES_SAMPLE_RATE : false; | |
| if ($sentryDsn && is_numeric($sentryTracingRate) && $sentryTracingRate != 0) { | |
| /* | |
| * First we modify the current hub so the new transaction is attached to it. | |
| */ | |
| wp_sentry_safe(function (\Sentry\State\HubInterface $sentryHub) { | |
| $rate = WP_SENTRY_TRACES_SAMPLE_RATE ?? 0; | |
| $sentryHub->getClient()->getOptions()->setTracesSampleRate($rate); | |
| \Sentry\SentrySdk::setCurrentHub($sentryHub); | |
| }); | |
| $transactionContext = new \Sentry\Tracing\TransactionContext(); | |
| $transactionContext->setName($_SERVER['REQUEST_URI']); | |
| $transactionContext->setOp('http.server'); | |
| $transactionContext->setData([ | |
| 'url' => $_SERVER['REQUEST_URI'] ?? 'no_url', | |
| 'method' => strtoupper($_SERVER['REQUEST_METHOD'] ?? '') | |
| ]); | |
| $transactionContext->setStartTimestamp($_SERVER['REQUEST_TIME_FLOAT'] ?? null); | |
| $transaction = \Sentry\startTransaction($transactionContext); | |
| /* | |
| * We create a span for our transaction (is this ok?) | |
| */ | |
| $spanContext = new \Sentry\Tracing\SpanContext(); | |
| $spanContext->setOp('wordpress'); | |
| $GLOBALS['SENTRY_PERFORMANCE_TRANSACTION'] = $transaction; | |
| $GLOBALS['SENTRY_PERFORMANCE_SPAN'] = $transaction->startChild($spanContext); | |
| /* | |
| * On wp shutdown, we finalize the transaction. | |
| */ | |
| add_action('shutdown', function () { | |
| $sentryTransaction = $GLOBALS['SENTRY_PERFORMANCE_TRANSACTION'] ?? null; | |
| $sentrySpan = $GLOBALS['SENTRY_PERFORMANCE_SPAN'] ?? null; | |
| if ($sentryTransaction instanceof \Sentry\Tracing\Transaction && $sentrySpan instanceof \Sentry\Tracing\Span) { | |
| $sentryTransaction->finish(); | |
| $sentrySpan->finish(); | |
| } else { | |
| trigger_error('SENTRY global variables have been modified'); | |
| } | |
| }); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment