Search code examples
phpwordpressnewrelic

New Relic + Wordpress - ignore wp-cron.php


We have installed New Relic on our server, to monitor the performance of our websites. But recently we want to remove wp-cron.php from New Relic monitoring.

I put the following code in 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();
}

[...]

Unfortunatelly, this code does not work and New Relic still showing in reports the wordpress cronjobs with excessive time. We don't need to know how many time cron jobs consumed in our wordpress application.

Anyone know how remove it from New Relic?


Solution

  • The best way I've found to setting variables per individual PHP application or scripts within an application is by setting auto_prepend_file in PHP.ini to point to a PHP file that sets said variables for you.

    The code I use is published on git, feel free to borrow/improve/suggest. I'm also listing it here for sake of simplicity:

    ###
    # 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();
                }           
            }
    }
    

    If your server config allows per dir php.ini, drop a new php.ini and have auto_prepend_file point to a php file that calls newrelic_ignore_transaction().

    If your server config does NOT allow per dir php.ini (such as with mod_fcgid), modify the code suggested to detect the script being run and issue a newrelic_ignore_transaction() when that condition is satisfied.