如何配置wordpress以使用哨兵性能监控



我正在使用自助哨兵来监控我的wordpress网站。我在哨兵主机面板中收到错误和异常日志并监视问题,但我没有收到任何性能报告
我在index.php的末尾添加了这个片段代码,但它不起作用。

Sentryinit([
'dsn' => 'SENTRY_DSN',
'traces_sampler' => static function (SentryTracingSamplingContext $context): float {
if (false !== strpos($context->getTransactionContext()->getName(), 'health')) {
// Discard transactions that have 'health' in their name
return 0.0;
}
// Sample rate for all other transactions
return 1.0;
},
]);

有人能帮我配置wordpress在哨兵中使用性能监控吗?

由于Wordpress的启动顺序,您应该将其添加为mu插件:https://codex.wordpress.org/Plugin_API/Action_Reference

我已经通过与wp-sesent插件和自定义的mu插件集成,实现了一些功能。在这里查看我的答案,随着时间的推移可能会变得更好:https://github.com/stayallive/wp-sentry/issues/68

安装wp-sesent插件,并将其添加到/wp-content/mu插件内的文件中。之前一定要删除wp-setinet的mu插件。

<?php
/**
* 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: 
* Author URI: 
* License: MIT
*/
$wp_sentry = __DIR__ . '/../plugins/wp-sentry-integration/wp-sentry.php';
$ok = true;
if (!file_exists($wp_sentry)) {
trigger_error('Sentry is not being mu-loaded because the plugin files are not there');
$ok = false;
return;
}
require $wp_sentry;
if(!function_exists('wp_sentry_safe') || !$ok){
trigger_error('Sentry is not being mu-loaded because the require statement failed');
return;
}
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) {
# 1 - set up tracing in our hub
wp_sentry_safe(function (SentryStateHubInterface $sentryHub) {
$rate = WP_SENTRY_TRACES_SAMPLE_RATE ?? 0;
$sentryHub->getClient()->getOptions()->setTracesSampleRate($rate);
SentrySentrySdk::setCurrentHub($sentryHub);
});
# 2 set up transaction context
$transactionContext = new SentryTracingTransactionContext();
$url = null;
if ($_SERVER['REQUEST_URI'] ?? null) {
$url = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($_SERVER['REQUEST_URI'],'wp-admin/admin-ajax.php') !== false) {
$actionName = $_GET['action'] ?? $_POST['action'] ?? 'SENTRY_PARSE_ERROR_NO_ACTION';
/**
* If its admin ajax, the transaction name includes the name of the action instad of the URL.
*/
$name = "admin-ajax: ".$actionName;
$transactionContext->setTags(['admin-ajax']);
} else {
$name = $_SERVER['HTTP_HOST'].parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$transactionContext->setTags(['normal-request']);
}
} else {
if (defined('DOING_CRON')) {
$name = 'cron';
$transactionContext->setTags(['cron']);
} else {
$commands = $_SERVER['argv'] ?? ['','SENTRY_PARSE_ERROR_NO_COMMAND'];
array_shift($commands); // the first one is the wp binary.
$command = implode(' ',$commands);
$name = 'wp '.$command;
$transactionContext->setTags(['wp-cli']);
}
}
# 3 set up transaction context
$transactionContext->setName($name);
$transactionContext->setOp('http.server');
$transactionContext->setData([
'url' => $url,
'method' => strtoupper($_SERVER['REQUEST_METHOD'] ?? '')
]);
$transactionContext->setStartTimestamp($_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true) );
# 4 start transaction
$transaction = SentrySentrySdk::getCurrentHub()->startTransaction($transactionContext);
SentrySentrySdk::getCurrentHub()->setSpan($transaction);
# 5 set up span context
$spanContext = new SentryTracingSpanContext();
$spanContext->setOp('wordpress_span');
$spanContext->setStartTimestamp(microtime(true));
# 6 start span
$appSpan = $transaction->startChild($spanContext);
//$appSpan->getTraceContext() this?
SentrySentrySdk::getCurrentHub()->setSpan($appSpan);
add_action('shutdown', function () {
# 7 finish span
$sentrySpan = SentrySentrySdk::getCurrentHub()->getSpan();
if($sentrySpan){
$sentrySpan->finish();
}
$sentryTransaction = SentrySentrySdk::getCurrentHub()->getTransaction();
# 8 finish transaction
if($sentryTransaction){
SentrySentrySdk::getCurrentHub()->setSpan($sentryTransaction);
$transaction = SentrySentrySdk::getCurrentHub()->getSpan();
$transaction->finish();
}
});
}

添加后,请确保在WP-config.php中定义WP_SENTRY_TRACES_SAMPLE_RATE,其值介于0和1之间。0.3用于生产是个好主意。

最新更新