For Scheduled Jobs on the Acquia Cloud platform, that use Drush commands, there may be a need to profile the performance of these commands using New Relic’s Transactions.
In order to do this there are a number of steps involved:
1: Set up the New Relic PHP Agent
2: Configure Drupal to define the Transaction Name for New Relic
3: Create a script to run Drush commands
4: Add script to Acquia Cloud Scheduled Jobs
5: Verify Drush commands are shown in New Relic Transactions
The steps outlined here are for current Drupal and current Drush.
The steps can be used for earlier versions of Drupal that use Drush 9 as a minimum.
However, we strongly recommend using the latest Drupal and Drush versions, where possible.
The New Relic PHP agent configuration documentation talks about using a file named newrelic.ini
to configure the agent. We can leverage the use of the drush.ini
file associated with Drush to include New Relic agent configuration.
This configuration uses three New Relic Configuration file variables:
newrelic.license
- The Acquia Cloud Installed New Relic License Key."a47b28d4db93f9476e3f9576352061a20452NRAL"
newrelic.appname
- The New Relic application name that data is reported under."myapplication.dev"
newrelic.enabled
- A boolean setting to indicate to enable or disable New Relic agent.true
drush.ini
in your home directory under the .drush
directory, /home/clouduser/.drush
. If this file all ready exists, then add the content at the end of the file.It is important to include the quote character, "
, in the values below.
Contents for drush.ini
:
extension=newrelic.so
newrelic.license = "[INSERT LICENSE KEY HERE]"
newrelic.appname = "[site].[env]"
newrelic.enabled = true
In order that New Relic can show the specific Drush command, the Drupal site’s settings.php
file needs to be modified. The modification will use the New Relic PHP agent API function, newrelic_name_transaction, which sets a custom name for the New Relic transaction.
settings.php
to capture PHP CLI name for New Relic, above the Acquia require line.// If this is a CLI call, set some parameters so that
// it can be logged to New Relic
if (PHP_SAPI === 'cli') {
$cli_arg = NULL;
$_SERVER['REQUEST_METHOD'] = 'CLI';
// If we are in a drush environment, set the REQUEST_URI to the command.
try {
// Check if we have the \Drush\Drush::input() method - that is we expect to use a later Drush version
$method = new ReflectionMethod('\Drush\Drush::input');
if ( $method->isStatic() )
{
// Method exists!
// Retrieve the drush command and set New Relic transaction name
$cli_arg = \Drush\Drush::input()->getFirstArgument();
$_SERVER['REQUEST_URI'] = $cli_arg;
if (extension_loaded('newrelic')) {
// Using the function newrelic_name_transaction()
// <https://docs.newrelic.com/docs/apm/agents/php-agent/php-agent-api/newrelic_name_transaction/>
if (function_exists('newrelic_name_transaction') && !empty($cli_arg)) {
$cli_arg = 'drush ' . $cli_arg;
// Define the Transaction Name for New Relic
newrelic_name_transaction($cli_arg);
}
}
}
}
catch ( ReflectionException $e )
{
// The method \Drush\Drush::input() method does not exist
// We will do nothing, so as to allow rest of script to continue
}
}
We can create a script that runs the Drush commands and logs those to /shared/logs/drush-cron.log
. This script will be added to the application’s Scheduled Jobs or cron
jobs.
The name of the script is really dependent on what is appropriate for your application.
We are using the generic name of newrelic_drush.sh
.
Put the script in the script
directory at the root of your codebase, that is <code_base_root>/scripts/newrelic_drush.sh
.
The example script here, serves as an example only. You will need to modify it to best serve your needs.
In your codebase, create the shell script at <code_base_root>/scripts/
.
Content of script:
#!/usr/bin/env bash
if [[ $# -ne 2 ]]; then
echo "Script requires two parameters" >&2
echo "${0} AH_SITE_NAME SITE_DOMAIN" >&2
exit 2
fi
# if there is a $HOME/.ssh/environment file,
# then read each line and export each variable from the file
SSH_ENVIRONMENT=$HOME/.ssh/environment
if [[ -f "${SSH_ENVIRONMENT}" ]]; then
#export each of the variables from the file
while read line; do export $line; done < ${SSH_ENVIRONMENT}
fi
logfile="/shared/logs/drush-cron.log"
exec > >(/usr/bin/tee -a "$logfile") 2>&1
if [ -n "${2}" ]; then
uri="${2}"
else
uri="${AH_SITE_NAME}.${AH_REALM}.acquia-sites.com"
fi
#echo "URI: ${uri}"
echo "***** Script ${0} Started: $(date --rfc-3339=seconds) *****"
echo "***** Running Drush status"
PHP_INI_SCAN_DIR=:$HOME/.drush drush --root="/var/www/html/${AH_SITE_NAME}/docroot/" --uri="${uri}" status
echo
echo "***** Running Drush cron"
PHP_INI_SCAN_DIR=:$HOME/.drush drush --root="/var/www/html/${AH_SITE_NAME}/docroot/" --uri="${uri}" cron
echo
echo -e "***** Script Completed: $(date --rfc-3339=seconds) *****\\n"
-r-xr-xr-x. 1 myapplication.dev myapplication 1485 Jul 26 02:15 newrelic_drush.sh
/var/www/html/myapplication.dev/scripts/newrelic_drush.sh myapplicationdev <https://myapplicationdev.prod.acquia-sites.com>
/shared/logs/drush-cron.log
, has an entry that starts with a line similar to:***** Script /var/www/html/myapplication.dev/scripts/newrelic_drush.sh Started: 2024-08-08 09:55:44+00:00 *****
Once the script is ready, we can add a scheduled job to the Acquia environment to run the script at regular intervals.
myapplication
with your application details./var/www/html/scripts/newrelic_drush.sh myapplicationdev <https://myapplicationdev.prod.acquia-sites.com>
With all the configurations in place, you should now see the Drush commands listed in the New Relic Transactions, in the Transaction overview.
Example of Drush commands in Transaction overview:
You now have New Relic monitoring set up to show Drush commands as Transactions!
If this content did not answer your questions, try searching or contacting our support team for further assistance.