Acquia Cloud has two instances of PHP running on webservers: one instance is reserved for Apache and handles all incoming HTTP requests, while the other instance is reserved for CLI processing (which includes Drush commands). As the New Relic PHP extensions are only added to the php.ini file associated with Apache, whenever Drush commands are executed, the New Relic daemon is not triggered.
We are unable to modify the php.ini file on Acquia Cloud for CLI processing, but we can leverage the drush.ini file to include New Relic on CLI processes. To do this:
- Navigate to the
.drush
directory inside of your home directory - Inside
.drush
, create a file calleddrush.ini
with the following contents:
# Enable New Relic profiling for Drush runs.
# See https://support.acquia.com/hc/en-us/articles/360009061733-Profile-Drush-Commands-in-New-Relic
extension=newrelic.so
newrelic.license = "[INSERT YOUR LICENSE KEY HERE]"
newrelic.daemon.port=/run/newrelic.sock
Once that file is saved with the New Relic information, cron jobs will now be available as “background processes” in New Relic.
After you do that, the background processes that are now going into New Relic need to be associated with the correct Application Name, otherwise the data will not be visible in the New Relic UI. Be sure to use the same application names that have been configured in Acquia Cloud, that way all of the data is correctly aggregated. There are two ways to do this:
- Add another variable to the drush.ini file created above.
- Dynamically set the application name in
settings.php
Option 1: Set the application name in drush.ini
Add the the newrelic.appname variable to the above drush.ini file:
# .. {add this below the other newrelic.* directives} ..
#
# The next line sets the application name in New Relic.
newrelic.appname = "[SITENAME].[ENV]"
Option 2: Dynamically set the application name in settings.php
if (PHP_SAPI === 'cli' && isset($_ENV['AH_SITE_ENVIRONMENT']) && extension_loaded('newrelic')) {
// Associate with the proper New Relic application. This should match the
// New Relic application name set in the environment settings in Acquia Cloud.
// If the app name has been configured then the default is '[SITENAME].[ENV]'.
newrelic_set_appname($_ENV['AH_SITE_GROUP'] . '.' . $_ENV['AH_SITE_ENVIRONMENT'], '', 'true');
// See https://support.acquia.com/hc/en-us/articles/360009061733-Profile-Drush-Commands-in-New-Relic
}
At this point, New Relic will be able to trace the cron tasks that are executing on the site; but the transactions are named after the file that executed them, such as drush.php:
As this is not very helpful, these transactions should be named.
To name a New Relic transaction, add the following snippet to settings.php (or combine it if you used "option 2" above):
// If this is a CLI call, set some parameters so that
// the request is logged properly in drupal-request.log.
if (PHP_SAPI === 'cli') {
$_SERVER['REQUEST_METHOD'] = 'CLI';
// If we are in a drush environment, set the REQUEST_URI to the command.
if (function_exists('drush_get_arguments')) {
// Get the arguments. This should include the command called.
$cli_args = drush_get_arguments();
// Ensure each argument is wrapped in quotes.
$cli_args = array_map(function ($value) {
$escaped = escapeshellarg($value);
// If nothing was changed inside the string, return the value as is so that
// the argument won't necessarily be wrapped in quotes.
return ("'$value'" === $escaped) ? $value : $escaped;
}, $cli_args);
// Prepend the arguments with 'drush'.
array_unshift($cli_args, 'drush');
// Now set the arguments to REQUEST_URI, imploding with a single space.
$_SERVER['REQUEST_URI'] = implode(' ', $cli_args);
}
if (extension_loaded('newrelic')) {
// Name the transaction after the drush command and arguments.
if (function_exists('newrelic_name_transaction') && !empty($cli_args)) {
newrelic_name_transaction(implode(' ', $cli_args));
}
}
// Profile on-demand drush calls when AH_XHPROF environment variable is present.
if (getenv('AH_XHPROF')) {
$_GET['AH_XHPROF'] = 1;
}
}
Now the transactions will have a more useful name in New Relic: