Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-34362: Fix CLI transaction reporting #2772

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docroot/sites/default/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,42 @@
if (extension_loaded('newrelic')) { // Ensure PHP agent is available
newrelic_disable_autorum();
}

// 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
}
}
41 changes: 41 additions & 0 deletions scripts/drush-transaction.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/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 "***** Running Drush entity usage tracking"
PHP_INI_SCAN_DIR=:$HOME/.drush drush --root="/var/www/html/${AH_SITE_NAME}/docroot/" --uri="${uri}" --time-limit=60 queue:run entity_usage_tracker

echo

echo -e "***** Script Completed: $(date --rfc-3339=seconds) *****\\n"