Loading...

Profile Drush Commands in New Relic

To profile the performance of Drush commands used in scheduled jobs through New Relic’s transactions:

  1. Set up the New Relic PHP Agent
  2. Configure Drupal to define the Transaction Name
  3. Create a script to run Drush commands
  4. Adding script to scheduled jobs in Cloud Platform
  5. Verify Drush commands

Setting up the New Relic PHP Agent

To configure the New Relic PHP agent, use the newrelic.ini file. For more information, visit New Relic PHP agent configuration. In addition, you can use the drush.ini file associated with Drush. You can use the following New Relic configuration file variables:

Variable name DescriptionExample
newrelic.license Indicates the New Relic license key installed in Cloud Platform."a47b28d4db93f9476e3f9576352061a20452NRAL"
newrelic.appname Indicates the New Relic application name that data is reported under."myapplication.dev"
newrelic.enabled Indicates if the New Relic agent is enabled or disabled.true
  1. In your Cloud Platform infrastructure, create the file drush.ini file in your home directory,/home/clouduser/.drush. If this file already exists, add the content at the end of the file. Ensure that you include the quote character (") in the following value.
    Content of the drush.ini file:

    extension=newrelic.so
    newrelic.license = "[INSERT LICENSE KEY HERE]"
    newrelic.appname = "[site].[env]"
    newrelic.enabled = true

Configuring Drupal to define the Transaction Name

For New Relic to display the specific Drush command, you must update the Drupal site’s settings.php file. For more information, visit New Relic PHP agent API and newrelic_name_transaction.

  1. In your codebase, add the following `code snippet to the Drupal site’s settings.php file.

    // 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
      }
    }

Creating a script to run Drush commands

You can create a script that runs Drush commands and logs them to /shared/logs/drush-cron.log. You can add this script to the application’s scheduled jobs or cron jobs. You can name the script based on your application.

For example, let us assume that the script name is newrelic_drush.sh. The following steps serves as an example. You must modify the steps based on your application needs.

  1. In your codebase, create the Shell script at <code_base_root>/scripts/. For example, <code_base_root>/scripts/newrelic_drush.sh.

    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"
  2. Ensure that the permissions of script are set to be executable.

    -r-xr-xr-x. 1 myapplication.dev myapplication 1485 Jul 26 02:15 newrelic_drush.sh
  3. Test the script on the Cloud Platform non-production environment. For example, 

    /var/www/html/myapplication.dev/scripts/newrelic_drush.sh myapplicationdev <https://myapplicationdev.prod.acquia-sites.com>
  4. Confirm that the/shared/logs/drush-cron.log file 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 *****

Adding script to scheduled jobs in Cloud Platform

After the script is ready, you can add a scheduled job to the Cloud Platform environment to run the script at regular intervals.

  1. Run the following command by replacing myapplication with your application details. For more information, visit Creating scheduled jobs.

    /var/www/html/scripts/newrelic_drush.sh myapplicationdev <https://myapplicationdev.prod.acquia-sites.com>

Verifying Drush commands

Verify if can see the Drush commands listed on the Transaction overview in New Relic.

 

 

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation