【问题标题】:New Relic + Wordpress - ignore wp-cron.phpNew Relic + Wordpress - 忽略 wp-cron.php
【发布时间】:2015-04-06 23:39:56
【问题描述】:

我们在我们的服务器上安装了 New Relic,以监控我们网站的性能。但最近我们想从 New Relic 监控中删除 wp-cron.php。

我将以下代码放在 wp-cron.php 中:

[...]

if ( !defined('ABSPATH') ) {
/** Set up WordPress environment */
require_once('./wp-load.php');
}

if (extension_loaded('newrelic')) {
    newrelic_ignore_transaction();
    newrelic_ignore_apdex();
}

[...]

不幸的是,此代码不起作用,New Relic 仍然在报告中显示 wordpress cronjobs 时间过长。我们不需要知道 cron 作业在我们的 wordpress 应用程序中消耗了多少时间。

有人知道如何将它从 New Relic 中移除吗?

【问题讨论】:

    标签: php wordpress newrelic


    【解决方案1】:

    我发现为每个 PHP 应用程序或应用程序中的脚本设置变量的最佳方法是通过 在 PHP.ini 中设置 auto_prepend_file 以指向 PHP 文件为您设置所述变量。

    我使用的代码是published on git,请随意借用/改进/建议。为了简单起见,我也在这里列出它:

    ###
    # NewRelic PHP API central file
    # Description: Allows PHP installs using mod-fgcid to set newrelic_set_appname
    # Usage: Inside PHP.ini for each vhost in your server,
    # point to this script using: auto_prepend_file = "newrelic.php"
    # Where you place the script depends on your include_path setting.
    # See http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
    # Version: 0.2
    # Author http://MATTERmedia.com/
    #
    # This script is released under the GNU General Public License, version 2 (GPL).
    # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    ###
    
    if (extension_loaded('newrelic')) {
        if (!isset($_SERVER['HTTP_HOST'])) {
            newrelic_set_appname ("Unknown");
            } else {
                # If UseCanonicalName is set to Off, Apache will use (user inputted) HTTP_HOST for SERVER_NAME
                # Best is to rely on HTTP_HOST and validate it against a list of allowed hosts.
                # See http://shiflett.org/blog/2006/mar/server-name-versus-http-host
                $host = strtolower($_SERVER['HTTP_HOST']);
                # Easily disable any vhost from sending data to newrelic.
                $disabled_hosts = array('foo.example.com');
                $valid_hosts = array('bar.example.com');
                # Add a secondary AppName
                $secondary_appname = ';All Virtual Hosts';          
                if ((!in_array($host, $disabled_hosts)) && (in_array($host, $valid_hosts))) {
                        newrelic_set_appname($host.$secondary_appname);
                    } else {                    
                        newrelic_ignore_transaction();
                        # technically you wouldn't need to disable_autorum when you ignore_transaction, but it's good practice.
                        newrelic_disable_autorum();
                }           
            }
    }
    

    如果您的服务器配置允许每个目录 php.ini,删除一个新的 php.ini 并让 auto_prepend_file 指向一个调用 newrelic_ignore_transaction() 的 php 文件。 p>

    如果您的服务器配置不允许 per dir php.ini(例如使用 mod_fcgid),请修改建议的代码以检测正在运行的脚本并发出 newrelic_ignore_transaction() em> 满足条件时。

    【讨论】:

      【解决方案2】:

      我对@9​​87654322@ 一无所知,感谢您的提问,我发现这似乎是一个非常有用的工具。

      环顾四周,我发现Drupal 的这个 sn-p 与 cron 作业有关。请参阅第 68 行中的 TRUE 参数。也许您需要那个...

      Drupal module implementing New Relic

      57 /**
      58 * Implementation of hook_cron().
      59 *
      60 * This is used to set cron tasks to be not tracked by RPM if so desired.
      61 */
      62 function new_relic_rpm_cron() {
      63     $cron_tracking = variable_get('new_relic_rpm_track_cron', 'norm');
      64     if ($cron_tracking == 'bg') {
      65         newrelic_background_job(TRUE);
      66     }
      67     elseif ($cron_tracking == 'ignore') {
      68         newrelic_ignore_transaction(TRUE); // pass TRUE to ignore
      69     }
      70 }
      

      【讨论】:

        猜你喜欢
        • 2012-11-09
        • 2021-05-04
        • 1970-01-01
        • 2019-06-03
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 2018-06-15
        • 1970-01-01
        相关资源
        最近更新 更多