Created
May 20, 2014 11:38
-
-
Save jippi/7f25ee111f761dc1995a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php -q | |
| <?php | |
| // app/Console/cake | |
| require_once dirname(dirname(__DIR__)) . '/vendors/autoload.php'; | |
| if (extension_loaded('newrelic')) { | |
| require_once dirname(dirname(__FILE__)) . '/Lib/Environment.php'; | |
| define('NEW_RELIC_APP_NAME', sprintf('%s - app - cli', Environment::get())); | |
| newrelic_set_appname(NEW_RELIC_APP_NAME); | |
| newrelic_background_job(true); | |
| newrelic_capture_params(true); | |
| } | |
| $ds = DIRECTORY_SEPARATOR; | |
| $dispatcher = 'Cake' . $ds . 'Console' . $ds . 'ShellDispatcher.php'; | |
| if (function_exists('ini_set')) { | |
| // $root = dirname(dirname(dirname(__FILE__))); | |
| $root = dirname(dirname(dirname(__FILE__))).$ds.'vendors'.$ds.'cakephp'.$ds.'cakephp'; | |
| // the following line differs from its sibling | |
| // /lib/Cake/Console/Templates/skel/Console/cake.php | |
| ini_set('include_path', $root . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path')); | |
| } | |
| if (!include($dispatcher)) { | |
| trigger_error('Could not locate CakePHP core files.', E_USER_ERROR); | |
| } | |
| unset($paths, $path, $dispatcher, $root, $ds); | |
| include __DIR__ . '/BowntyShellDispatcher.php'; | |
| return BowntyShellDispatcher::run($argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // app/webroot/index.php | |
| require_once dirname(dirname(__DIR__)) . '/vendors/autoload.php'; | |
| require_once dirname(dirname(__FILE__)) . '/Lib/Environment.php'; | |
| if (extension_loaded('newrelic')) { | |
| $appType = 'app'; | |
| $appName = 'web'; | |
| if (strpos($_SERVER['REQUEST_URI'], '/admin/') !== false) { | |
| $appName = 'admin'; | |
| } | |
| define('NEW_RELIC_APP_NAME', sprintf('%1$s - %2$s - %3$s', Environment::get(), $appType, $appName)); | |
| newrelic_set_appname(NEW_RELIC_APP_NAME); | |
| newrelic_background_job(false); | |
| newrelic_capture_params(true); | |
| } | |
| define('DS', DIRECTORY_SEPARATOR); | |
| define('ROOT', dirname(dirname(dirname(__FILE__)))); | |
| define('APP_DIR', basename(dirname(dirname(__FILE__)))); | |
| define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendors'.DS.'cakephp'.DS.'cakephp'.DS.'lib'); | |
| define('WEBROOT_DIR', basename(dirname(__FILE__))); | |
| define('WWW_ROOT', dirname(__FILE__) . DS); | |
| require CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php'; | |
| $statsd = Environment::statsd(); | |
| $statsd->increment('app.web.all.hits'); | |
| $statsd->startMemoryProfile('app.web.all.memory_profile'); | |
| $statsd->startTiming('app.web.all.load_time'); | |
| App::uses('Dispatcher', 'Routing'); | |
| $Dispatcher = new Dispatcher(); | |
| $Dispatcher->dispatch(new CakeRequest(), new CakeResponse()); | |
| $statsd->endTiming('app.web.all.load_time'); | |
| $statsd->endMemoryProfile('app.web.all.memory_profile'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Class to help work with NewRelic in PHP | |
| * | |
| * @author Christian Winther | |
| * @see https://docs.newrelic.com/docs/php/the-php-api | |
| */ | |
| class NewRelic { | |
| /** | |
| * Static instance of NewRelic | |
| * | |
| * @var NewRelic | |
| */ | |
| protected static $_instance; | |
| /** | |
| * Get the singleton instance of NewRelic | |
| * | |
| * @return NewRelic | |
| */ | |
| public static function getInstance() { | |
| if (static::$_instance === null) { | |
| static::$_instance = new NewRelic(); | |
| } | |
| return static::$_instance; | |
| } | |
| /** | |
| * Change the application name | |
| * | |
| * @param string $name | |
| * @return void | |
| */ | |
| public function applicationName($name) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_set_appname($name); | |
| } | |
| /** | |
| * Start a New Relic transaction | |
| * | |
| * @param string $name | |
| * @return void | |
| */ | |
| public function start($name) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_start_transaction(NEW_RELIC_APP_NAME); | |
| newrelic_name_transaction($name); | |
| } | |
| /** | |
| * End a New Relic transaction | |
| * | |
| * @param boolean $ignore Should the statistics NewRelic gathered be discarded? | |
| * @return void | |
| */ | |
| public function stop($ignore = false) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_end_transaction($ignore); | |
| } | |
| /** | |
| * Ignore the current transaction | |
| * | |
| * @return | |
| */ | |
| public function ignoreTransaction() { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_ignore_transaction(); | |
| } | |
| /** | |
| * Ignore the current apdex | |
| * | |
| * @return | |
| */ | |
| public function ignoreApdex() { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_ignore_apdex(); | |
| } | |
| /** | |
| * Should NewRelic capture params ? | |
| * | |
| * @param boolean $boolean | |
| * @return void | |
| */ | |
| public function captureParams($boolean) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_capture_params($boolean); | |
| } | |
| /** | |
| * Add custom tracer method | |
| * | |
| * @param string $method | |
| */ | |
| public function addTracer($method) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_add_custom_tracer($method); | |
| } | |
| /** | |
| * Add a custom parameter to the New Relic transaction | |
| * | |
| * @param string $key | |
| * @param mixed $value | |
| */ | |
| public function parameter($key, $value) { | |
| if (!$this->hasNewRelic()) { | |
| return false; | |
| } | |
| newrelic_add_custom_parameter($key, $value); | |
| } | |
| /** | |
| * Track a custom metric | |
| * | |
| * @param string $key | |
| * @param integer|float $value | |
| * @return | |
| */ | |
| public function metric($key, $value) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| if (!is_numeric($value)) { | |
| throw new CakeException('Value must be numeric'); | |
| } | |
| newrelic_custom_metric($key, $value); | |
| } | |
| /** | |
| * Add a custom method to have traced by NewRelic | |
| * | |
| * @param string $method | |
| * @return void | |
| */ | |
| public function tracer($method) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_add_custom_tracer($method); | |
| } | |
| /** | |
| * Send an exception to New Relic | |
| * | |
| * @param Exception $e | |
| * @return void | |
| */ | |
| public function sendException(Exception $e) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_notice_error(null, $e); | |
| } | |
| /** | |
| * Set user attributes | |
| * | |
| * @param string $user | |
| * @param string $account | |
| * @param string $product | |
| * @return void | |
| */ | |
| public function user($user, $account, $product) { | |
| if (!$this->hasNewRelic()) { | |
| return; | |
| } | |
| newrelic_set_user_attributes($user, $account, $product); | |
| } | |
| /** | |
| * Check if the NewRelic PHP extension is loaded | |
| * | |
| * @return boolean | |
| */ | |
| public function hasNewRelic() { | |
| return extension_loaded('newrelic'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('NewRelic', 'NewRelic.Lib'); | |
| App::uses('NewRelicTrait', 'NewRelic.Trait'); | |
| /** | |
| * New Relic Component | |
| * | |
| * @author Christian Winther | |
| */ | |
| class NewRelicComponent extends Component { | |
| use NewRelicTrait; | |
| /** | |
| * Called before the Controller::beforeFilter(). | |
| * | |
| * Start NewRelic and configure transaction name | |
| * | |
| * @param Controller $controller | |
| * @return void | |
| */ | |
| public function initialize(Controller $controller) { | |
| $this->setName($controller->request); | |
| $this->start(); | |
| if ($controller->Auth) { | |
| $this->user($controller->Auth->user('id'), $controller->Auth->user('email'), ''); | |
| } | |
| $this->captureParams(true); | |
| $this->addTracer('CakeRoute::match'); | |
| $this->addTracer('CrudComponent::executeAction'); | |
| $this->addTracer('Controller::render'); | |
| $this->addTracer('View::render'); | |
| $this->addTracer('View::element'); | |
| $this->addTracer('View::renderLayout'); | |
| $this->addTracer('DboSource::_execute'); | |
| $this->addTracer('AttemptChecker::attempt'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('AppShell', 'Console/Command'); | |
| App::uses('NewRelicTrait', 'NewRelic.Trait'); | |
| class NewRelicTask extends AppShell { | |
| use NewRelicTrait; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('NewRelic', 'NewRelic.Lib'); | |
| trait NewRelicTrait { | |
| /** | |
| * The transaction name to use | |
| * | |
| * @var string | |
| */ | |
| protected $_name; | |
| /** | |
| * Set the transaction name | |
| * | |
| * If `$name` is a Shell instance, the name will | |
| * automatically be derived based on best practices | |
| * | |
| * @param string|Shell $name | |
| */ | |
| public function setName($name) { | |
| if ($name instanceof Shell) { | |
| $name = $this->_deriveNameFromShell($name); | |
| } | |
| if ($name instanceof CakeRequest) { | |
| $name = $this->_deriveNameFromRequest($name); | |
| } | |
| $this->_name = $name; | |
| } | |
| /** | |
| * Get the name | |
| * | |
| * @return string | |
| */ | |
| public function getName() { | |
| return $this->_name; | |
| } | |
| /** | |
| * Change the application name | |
| * | |
| * @param string $name | |
| * @return void | |
| */ | |
| public function applicationName($name) { | |
| NewRelic::getInstance()->applicationName($name); | |
| } | |
| /** | |
| * Start a NewRelic transaction | |
| * | |
| * @param null|string $name | |
| * @return void | |
| */ | |
| public function start($name = null) { | |
| NewRelic::getInstance()->start($this->_getTransactionName($name)); | |
| } | |
| /** | |
| * Stop a transaction | |
| * | |
| * @return void | |
| */ | |
| public function stop() { | |
| NewRelic::getInstance()->stop(); | |
| } | |
| /** | |
| * Ignore current transaction | |
| * | |
| * @return void | |
| */ | |
| public function ignoreTransaction() { | |
| NewRelic::getInstance()->ignoreTransaction(); | |
| } | |
| /** | |
| * Ignore current apdex | |
| * | |
| * @return void | |
| */ | |
| public function ignoreApdex() { | |
| NewRelic::getInstance()->ignoreApdex(); | |
| } | |
| /** | |
| * Add custom parameter to transaction | |
| * | |
| * @param string $key | |
| * @param scalar $value | |
| * @return void | |
| */ | |
| public function parameter($key, $value) { | |
| NewRelic::getInstance()->parameter($key, $value); | |
| } | |
| /** | |
| * Add custom metric | |
| * | |
| * @param string $key | |
| * @param float $value | |
| * @return void | |
| */ | |
| public function metric($key, $value) { | |
| NewRelic::getInstance()->metric($key, $value); | |
| } | |
| /** | |
| * capture params | |
| * | |
| * @param boolean $capture | |
| * @return void | |
| */ | |
| public function captureParams($capture) { | |
| NewRelic::getInstance()->captureParams($capture); | |
| } | |
| /** | |
| * Add custom tracer method | |
| * | |
| * @param string $method | |
| */ | |
| public function addTracer($method) { | |
| NewRelic::getInstance()->addTracer($method); | |
| } | |
| /** | |
| * Set user attributes | |
| * | |
| * @param string $user | |
| * @param string $account | |
| * @param string $product | |
| * @return void | |
| */ | |
| public function user($user, $account, $product) { | |
| NewRelic::getInstance()->user($user, $account, $product); | |
| } | |
| /** | |
| * Send an exception to New Relic | |
| * | |
| * @param Exception $e | |
| * @return void | |
| */ | |
| public function sendException(Exception $e) { | |
| NewRelic::getInstance()->sendException($e); | |
| } | |
| /** | |
| * Get transaction name | |
| * | |
| * @param string $name | |
| * @return string | |
| */ | |
| protected function _getTransactionName($name) { | |
| if (is_string($name)) { | |
| return $name; | |
| } | |
| return $this->_name; | |
| } | |
| /** | |
| * Derive the transaction name | |
| * | |
| * @param Shell $name | |
| * @return string | |
| */ | |
| protected function _deriveNameFromShell(Shell $shell) { | |
| $name = []; | |
| if ($shell->plugin) { | |
| $name[] = $shell->plugin; | |
| } | |
| $name[] = $shell->name; | |
| $name[] = $shell->command; | |
| return join('/', $name); | |
| } | |
| /** | |
| * Compute name based on request information | |
| * | |
| * @param CakeRequest $request | |
| * @return string | |
| */ | |
| protected function _deriveNameFromRequest(CakeRequest $request) { | |
| $name = []; | |
| if ($request->prefix) { | |
| $name[] = $request->prefix; | |
| } | |
| if ($request->plugin) { | |
| $name[] = $request->plugin; | |
| } | |
| $name[] = $request->controller; | |
| $name[] = $request->action; | |
| $name = join('/', $name); | |
| if ($request->ext) { | |
| $name .= '.' . $request->ext; | |
| } | |
| return $name; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment