From c5add394b761a65328f124a39b4cbe650ff093b8 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sun, 11 Jan 2015 15:47:44 +0000 Subject: [PATCH 01/32] cake3 compat --- composer.json | 24 +++++++++++++++---- .../Console}/Command/Task/NewRelicTask.php | 0 .../Component/NewRelicComponent.php | 12 ++++++---- {Lib => src/Lib}/NewRelic.php | 1 + {Trait => src/Traits}/NewRelicTrait.php | 3 ++- 5 files changed, 30 insertions(+), 10 deletions(-) rename {Console => src/Console}/Command/Task/NewRelicTask.php (100%) rename {Controller => src/Controller}/Component/NewRelicComponent.php (75%) rename {Lib => src/Lib}/NewRelic.php (99%) rename {Trait => src/Traits}/NewRelicTrait.php (98%) diff --git a/composer.json b/composer.json index 7f3a210..dd39ae3 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,24 @@ "issues":"https://github.com/jippi/cakephp-newrelic/issues", "irc":"irc://irc.freenode.org/friendsofcake" }, - "require":{ - "composer/installers":"*" + "require": { + "cakephp/cakephp": "3.0.*-dev", + "cakephp/plugin-installer": "*" + }, + "require-dev": { + "cakephp/cakephp-codesniffer": "dev-master", + "phpunit/phpunit": "4.1.*" + }, + "autoload": { + "psr-4": { + "NewRelic\\": "src", + "NewRelic\\Test\\Fixture\\": "tests\\Fixture" + } + }, + "autoload-dev": { + "psr-4": { + "Cake\\Test\\": "vendor/cakephp/cakephp/tests", + "DebugKit\\Test\\": "tests" + } }, - "extra": { - "installer-name": "NewRelic" - } } diff --git a/Console/Command/Task/NewRelicTask.php b/src/Console/Command/Task/NewRelicTask.php similarity index 100% rename from Console/Command/Task/NewRelicTask.php rename to src/Console/Command/Task/NewRelicTask.php diff --git a/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php similarity index 75% rename from Controller/Component/NewRelicComponent.php rename to src/Controller/Component/NewRelicComponent.php index 46644f8..34dff2b 100644 --- a/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -1,7 +1,9 @@ setName($controller->request); + public function beforeFilter(Event $event) { + $controller = $event->data['controller']; + + $this->setName($event->data['request']); $this->start(); if ($controller->Auth) { diff --git a/Lib/NewRelic.php b/src/Lib/NewRelic.php similarity index 99% rename from Lib/NewRelic.php rename to src/Lib/NewRelic.php index 1e11401..630265f 100644 --- a/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -1,4 +1,5 @@ Date: Sun, 11 Jan 2015 15:48:24 +0000 Subject: [PATCH 02/32] fix composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dd39ae3..42daee1 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,5 @@ "Cake\\Test\\": "vendor/cakephp/cakephp/tests", "DebugKit\\Test\\": "tests" } - }, + } } From caff67fdd7ece7e1e1d0ac5cf169ec80294948d8 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sun, 11 Jan 2015 16:01:00 +0000 Subject: [PATCH 03/32] fix auth thingie --- src/Controller/Component/NewRelicComponent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index 34dff2b..d576017 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -23,12 +23,12 @@ class NewRelicComponent extends Component { * @return void */ public function beforeFilter(Event $event) { - $controller = $event->data['controller']; $this->setName($event->data['request']); $this->start(); - if ($controller->Auth) { + $controller = $event->data['controller']; + if (isset($controller->Auth)) { $this->user($controller->Auth->user('id'), $controller->Auth->user('email'), ''); } From 09a77de7cecdf0dca7870527aedf7a21f8ca2a06 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sun, 11 Jan 2015 19:14:01 +0000 Subject: [PATCH 04/32] Add installer name --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 42daee1..c5cffd0 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,8 @@ "Cake\\Test\\": "vendor/cakephp/cakephp/tests", "DebugKit\\Test\\": "tests" } + }, + "extra": { + "installer-name": "NewRelic" } } From f10e7f8d8d78a2474bcf889c671c7e1e1ba8542f Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Tue, 13 Jan 2015 17:16:07 +0000 Subject: [PATCH 05/32] refactor for cake3 --- src/Console/Command/Task/NewRelicTask.php | 10 ---------- src/Lib/NewRelic.php | 18 ++++++++++++++++++ src/Shell/Task/NewRelicTask.php | 11 +++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) delete mode 100644 src/Console/Command/Task/NewRelicTask.php create mode 100644 src/Shell/Task/NewRelicTask.php diff --git a/src/Console/Command/Task/NewRelicTask.php b/src/Console/Command/Task/NewRelicTask.php deleted file mode 100644 index 386f143..0000000 --- a/src/Console/Command/Task/NewRelicTask.php +++ /dev/null @@ -1,10 +0,0 @@ -hasNewRelic()) { + return; + } + + newrelic_notice_error($code, $description, $file, $line, $context); + } + /** * Set user attributes * diff --git a/src/Shell/Task/NewRelicTask.php b/src/Shell/Task/NewRelicTask.php new file mode 100644 index 0000000..63d2a54 --- /dev/null +++ b/src/Shell/Task/NewRelicTask.php @@ -0,0 +1,11 @@ + Date: Tue, 20 Jan 2015 11:12:53 +0000 Subject: [PATCH 06/32] bump --- src/Lib/NewRelic.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 9668c24..88bcc7f 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -1,6 +1,8 @@ Date: Tue, 20 Jan 2015 15:34:56 +0000 Subject: [PATCH 07/32] oops --- src/Traits/NewRelicTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index 5052aaa..4d86eb2 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -2,6 +2,7 @@ namespace NewRelic\Traits; use NewRelic\Lib\NewRelic; +use Exception; trait NewRelicTrait { From 6653123868d1c7a18f1f848f756e426b09c8b3c9 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Tue, 20 Jan 2015 16:01:28 +0000 Subject: [PATCH 08/32] Import missing classes and fix detection --- src/Traits/NewRelicTrait.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index 4d86eb2..b7d7ae7 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -2,6 +2,8 @@ namespace NewRelic\Traits; use NewRelic\Lib\NewRelic; +use Cake\Console\Shell; +use Cake\Network\Request; use Exception; trait NewRelicTrait { @@ -26,7 +28,7 @@ public function setName($name) { $name = $this->_deriveNameFromShell($name); } - if ($name instanceof CakeRequest) { + if ($name instanceof Request) { $name = $this->_deriveNameFromRequest($name); } @@ -191,7 +193,7 @@ protected function _deriveNameFromShell(Shell $shell) { * @param CakeRequest $request * @return string */ - protected function _deriveNameFromRequest(CakeRequest $request) { + protected function _deriveNameFromRequest(Request $request) { $name = []; if ($request->prefix) { From d70bb957c8365c85eb67aef5afc5c243eabf920c Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Tue, 20 Jan 2015 16:58:16 +0000 Subject: [PATCH 09/32] fix NewRelic auto-naming for controller requests --- src/Controller/Component/NewRelicComponent.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index d576017..34e36d9 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -23,11 +23,10 @@ class NewRelicComponent extends Component { * @return void */ public function beforeFilter(Event $event) { - - $this->setName($event->data['request']); + $this->setName($event->subject->request); $this->start(); - $controller = $event->data['controller']; + $controller = $event->subject; if (isset($controller->Auth)) { $this->user($controller->Auth->user('id'), $controller->Auth->user('email'), ''); } From 6894c1d10ef3f643023d92d0b5ec34c16e571dbd Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 23 Jan 2015 09:55:46 +0000 Subject: [PATCH 10/32] Switch things up so the NewRelic class is static and allow ignoring exceptions --- src/Lib/NewRelic.php | 92 +++++++++++++++++------------------- src/Traits/NewRelicTrait.php | 22 ++++----- 2 files changed, 55 insertions(+), 59 deletions(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 88bcc7f..6715cb8 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -11,25 +11,7 @@ */ 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; - } + protected static $ignoredExceptions = []; /** * Change the application name @@ -37,8 +19,8 @@ public static function getInstance() { * @param string $name * @return void */ - public function applicationName($name) { - if (!$this->hasNewRelic()) { + public static function applicationName($name) { + if (!static::hasNewRelic()) { return; } @@ -51,8 +33,8 @@ public function applicationName($name) { * @param string $name * @return void */ - public function start($name = null) { - if (!$this->hasNewRelic()) { + public static function start($name = null) { + if (!static::hasNewRelic()) { return; } @@ -66,8 +48,8 @@ public function start($name = null) { * @param boolean $ignore Should the statistics NewRelic gathered be discarded? * @return void */ - public function stop($ignore = false) { - if (!$this->hasNewRelic()) { + public static function stop($ignore = false) { + if (!static::hasNewRelic()) { return; } @@ -79,8 +61,8 @@ public function stop($ignore = false) { * * @return */ - public function ignoreTransaction() { - if (!$this->hasNewRelic()) { + public static function ignoreTransaction() { + if (!static::hasNewRelic()) { return; } @@ -92,8 +74,8 @@ public function ignoreTransaction() { * * @return */ - public function ignoreApdex() { - if (!$this->hasNewRelic()) { + public static function ignoreApdex() { + if (!static::hasNewRelic()) { return; } @@ -106,8 +88,8 @@ public function ignoreApdex() { * @param boolean $boolean * @return void */ - public function captureParams($boolean) { - if (!$this->hasNewRelic()) { + public static function captureParams($boolean) { + if (!static::hasNewRelic()) { return; } @@ -119,8 +101,8 @@ public function captureParams($boolean) { * * @param string $method */ - public function addTracer($method) { - if (!$this->hasNewRelic()) { + public static function addTracer($method) { + if (!static::hasNewRelic()) { return; } @@ -133,8 +115,8 @@ public function addTracer($method) { * @param string $key * @param mixed $value */ - public function parameter($key, $value) { - if (!$this->hasNewRelic()) { + public static function parameter($key, $value) { + if (!static::hasNewRelic()) { return false; } @@ -152,8 +134,8 @@ public function parameter($key, $value) { * @param integer|float $value * @return */ - public function metric($key, $value) { - if (!$this->hasNewRelic()) { + public static function metric($key, $value) { + if (!static::hasNewRelic()) { return; } @@ -170,26 +152,40 @@ public function metric($key, $value) { * @param string $method * @return void */ - public function tracer($method) { - if (!$this->hasNewRelic()) { + public static function tracer($method) { + if (!static::hasNewRelic()) { return; } newrelic_add_custom_tracer($method); } +/** + * Ignore an exception class + * + * @param string $exception + * @return void + */ + public static function ignoreException($exception) { + static::$ignoredExceptions = array_merge(static::$ignoredExceptions, (array)$exception); + } + /** * Send an exception to New Relic * - * @param Exception $e + * @param Exception $exception * @return void */ - public function sendException(Exception $e) { - if (!$this->hasNewRelic()) { + public static function sendException(Exception $exception) { + if (!static::hasNewRelic()) { + return; + } + + if (false !== array_search(get_class($exception), static::$ignoredExceptions)) { return; } - newrelic_notice_error(null, $e); + newrelic_notice_error(null, $exception); } /** @@ -202,8 +198,8 @@ public function sendException(Exception $e) { * @param [type] $context [description] * @return [type] [description] */ - public function sendError($code, $description, $file, $line, $context = null) { - if (!$this->hasNewRelic()) { + public static function sendError($code, $description, $file, $line, $context = null) { + if (!static::hasNewRelic()) { return; } @@ -218,8 +214,8 @@ public function sendError($code, $description, $file, $line, $context = null) { * @param string $product * @return void */ - public function user($user, $account, $product) { - if (!$this->hasNewRelic()) { + public static function user($user, $account, $product) { + if (!static::hasNewRelic()) { return; } @@ -231,7 +227,7 @@ public function user($user, $account, $product) { * * @return boolean */ - public function hasNewRelic() { + public static function hasNewRelic() { return extension_loaded('newrelic'); } diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index b7d7ae7..7b6fc7e 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -51,7 +51,7 @@ public function getName() { * @return void */ public function applicationName($name) { - NewRelic::getInstance()->applicationName($name); + NewRelic::applicationName($name); } /** @@ -61,7 +61,7 @@ public function applicationName($name) { * @return void */ public function start($name = null) { - NewRelic::getInstance()->start($this->_getTransactionName($name)); + NewRelic::start($this->_getTransactionName($name)); } /** @@ -70,7 +70,7 @@ public function start($name = null) { * @return void */ public function stop($ignore = false) { - NewRelic::getInstance()->stop($ignore); + NewRelic::stop($ignore); } /** @@ -79,7 +79,7 @@ public function stop($ignore = false) { * @return void */ public function ignoreTransaction() { - NewRelic::getInstance()->ignoreTransaction(); + NewRelic::ignoreTransaction(); } /** @@ -88,7 +88,7 @@ public function ignoreTransaction() { * @return void */ public function ignoreApdex() { - NewRelic::getInstance()->ignoreApdex(); + NewRelic::ignoreApdex(); } /** @@ -99,7 +99,7 @@ public function ignoreApdex() { * @return void */ public function parameter($key, $value) { - NewRelic::getInstance()->parameter($key, $value); + NewRelic::parameter($key, $value); } /** @@ -110,7 +110,7 @@ public function parameter($key, $value) { * @return void */ public function metric($key, $value) { - NewRelic::getInstance()->metric($key, $value); + NewRelic::metric($key, $value); } /** @@ -120,7 +120,7 @@ public function metric($key, $value) { * @return void */ public function captureParams($capture) { - NewRelic::getInstance()->captureParams($capture); + NewRelic::captureParams($capture); } /** @@ -129,7 +129,7 @@ public function captureParams($capture) { * @param string $method */ public function addTracer($method) { - NewRelic::getInstance()->addTracer($method); + NewRelic::addTracer($method); } /** @@ -141,7 +141,7 @@ public function addTracer($method) { * @return void */ public function user($user, $account, $product) { - NewRelic::getInstance()->user($user, $account, $product); + NewRelic::user($user, $account, $product); } /** @@ -151,7 +151,7 @@ public function user($user, $account, $product) { * @return void */ public function sendException(Exception $e) { - NewRelic::getInstance()->sendException($e); + NewRelic::sendException($e); } /** From c4b6e92938b125b5484bc5069c0c973bc881987d Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 23 Jan 2015 11:31:41 +0000 Subject: [PATCH 11/32] Add more nifty methods to NewRelic --- src/Lib/NewRelic.php | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 6715cb8..44516e1 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -13,6 +13,12 @@ class NewRelic { protected static $ignoredExceptions = []; + protected static $ignoredErrors = []; + + protected static $serverVariables = []; + + protected static $cookieVariables = []; + /** * Change the application name * @@ -170,6 +176,36 @@ public static function ignoreException($exception) { static::$ignoredExceptions = array_merge(static::$ignoredExceptions, (array)$exception); } +/** + * Ignore error strings + * + * @param string $exception + * @return void + */ + public static function ignoreError($error) { + static::$ignoredErrors = array_merge(static::$ignoredErrors, (array)$error); + } + +/** + * Server variables to collect + * + * @param array $variables + * @return void + */ + public static function collectServerVariables(array $variables) { + static::$serverVariables = array_merge(static::$serverVariables, (array)$variables); + } + +/** + * Cookie variables to collect + * + * @param array $variables + * @return void + */ + public static function collectCookieVariables(array $variables) { + static::$cookieVariables = array_merge(static::$cookieVariables, (array)$variables); + } + /** * Send an exception to New Relic * @@ -203,6 +239,12 @@ public static function sendError($code, $description, $file, $line, $context = n return; } + foreach (static::$ignoreErrors as $errorMessage) { + if (false !== strpos($description, $errorMessage)) { + return; + } + } + newrelic_notice_error($code, $description, $file, $line, $context); } @@ -231,4 +273,29 @@ public static function hasNewRelic() { return extension_loaded('newrelic'); } +/** + * Collect environmental data for the transaction + * + * @return void + */ + public static function collect() { + static::parameter('_get', $_GET); + static::parameter('_post', $_POST); + static::parameter('_files', $_FILES); + + foreach ($_SERVER as $key => $value) { + if (!in_array($key, static::$serverVariables)) { + continue; + } + static::parameter('server_' . strtolower($key), $value); + } + + foreach ($_COOKIE as $key => $value) { + if (!in_array($key, static::$cookieVariables)) { + continue; + } + static::parameter('cookie_' . strtolower($key), $value); + } + } + } From 5e567666dbf758009d5170535bc5c3cbe024a90a Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 23 Jan 2015 11:49:02 +0000 Subject: [PATCH 12/32] fix variable name for ignoredExceptions --- src/Lib/NewRelic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 44516e1..199d9b4 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -239,7 +239,7 @@ public static function sendError($code, $description, $file, $line, $context = n return; } - foreach (static::$ignoreErrors as $errorMessage) { + foreach (static::$ignoredErrors as $errorMessage) { if (false !== strpos($description, $errorMessage)) { return; } From 00c7080e061bde2205131b41f05bd2f5e990555c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Tue, 24 Mar 2015 15:01:01 +0100 Subject: [PATCH 13/32] using cakephp 3 stable --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c5cffd0..a6c007b 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "irc":"irc://irc.freenode.org/friendsofcake" }, "require": { - "cakephp/cakephp": "3.0.*-dev", + "cakephp/cakephp": "~3.0", "cakephp/plugin-installer": "*" }, "require-dev": { From 67ac3baec624629b967521f89eb434a91b1a7ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Tue, 24 Mar 2015 15:03:16 +0100 Subject: [PATCH 14/32] Removing cake 2 specific code --- src/Controller/Component/NewRelicComponent.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index 34e36d9..e5a3c94 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -32,15 +32,6 @@ public function beforeFilter(Event $event) { } $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'); } } From 10ca7359a52eb46c35359c9b2d6130960cfb9fd7 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Mon, 18 Jul 2016 15:31:02 +0000 Subject: [PATCH 15/32] fix ignoring exceptions --- src/Lib/NewRelic.php | 263 ++++++++++++++++++++++--------------------- 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 199d9b4..1c8469f 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -11,13 +11,13 @@ */ class NewRelic { - protected static $ignoredExceptions = []; + protected static $ignoredExceptions = []; - protected static $ignoredErrors = []; + protected static $ignoredErrors = []; - protected static $serverVariables = []; + protected static $serverVariables = []; - protected static $cookieVariables = []; + protected static $cookieVariables = []; /** * Change the application name @@ -25,13 +25,13 @@ class NewRelic { * @param string $name * @return void */ - public static function applicationName($name) { - if (!static::hasNewRelic()) { - return; - } + public static function applicationName($name) { + if (!static::hasNewRelic()) { + return; + } - newrelic_set_appname($name); - } + newrelic_set_appname($name); + } /** * Start a New Relic transaction @@ -39,14 +39,14 @@ public static function applicationName($name) { * @param string $name * @return void */ - public static function start($name = null) { - if (!static::hasNewRelic()) { - return; - } + public static function start($name = null) { + if (!static::hasNewRelic()) { + return; + } - newrelic_start_transaction(NEW_RELIC_APP_NAME); - newrelic_name_transaction($name); - } + newrelic_start_transaction(NEW_RELIC_APP_NAME); + newrelic_name_transaction($name); + } /** * End a New Relic transaction @@ -54,39 +54,39 @@ public static function start($name = null) { * @param boolean $ignore Should the statistics NewRelic gathered be discarded? * @return void */ - public static function stop($ignore = false) { - if (!static::hasNewRelic()) { - return; - } + public static function stop($ignore = false) { + if (!static::hasNewRelic()) { + return; + } - newrelic_end_transaction($ignore); - } + newrelic_end_transaction($ignore); + } /** * Ignore the current transaction * * @return */ - public static function ignoreTransaction() { - if (!static::hasNewRelic()) { - return; - } + public static function ignoreTransaction() { + if (!static::hasNewRelic()) { + return; + } - newrelic_ignore_transaction(); - } + newrelic_ignore_transaction(); + } /** * Ignore the current apdex * * @return */ - public static function ignoreApdex() { - if (!static::hasNewRelic()) { - return; - } + public static function ignoreApdex() { + if (!static::hasNewRelic()) { + return; + } - newrelic_ignore_apdex(); - } + newrelic_ignore_apdex(); + } /** * Should NewRelic capture params ? @@ -94,26 +94,26 @@ public static function ignoreApdex() { * @param boolean $boolean * @return void */ - public static function captureParams($boolean) { - if (!static::hasNewRelic()) { - return; - } + public static function captureParams($boolean) { + if (!static::hasNewRelic()) { + return; + } - newrelic_capture_params($boolean); - } + newrelic_capture_params($boolean); + } /** * Add custom tracer method * * @param string $method */ - public static function addTracer($method) { - if (!static::hasNewRelic()) { - return; - } + public static function addTracer($method) { + if (!static::hasNewRelic()) { + return; + } - newrelic_add_custom_tracer($method); - } + newrelic_add_custom_tracer($method); + } /** * Add a custom parameter to the New Relic transaction @@ -121,17 +121,17 @@ public static function addTracer($method) { * @param string $key * @param mixed $value */ - public static function parameter($key, $value) { - if (!static::hasNewRelic()) { - return false; - } + public static function parameter($key, $value) { + if (!static::hasNewRelic()) { + return false; + } - if (!is_scalar($value)) { - $value = json_encode($value); - } + if (!is_scalar($value)) { + $value = json_encode($value); + } - newrelic_add_custom_parameter($key, $value); - } + newrelic_add_custom_parameter($key, $value); + } /** * Track a custom metric @@ -140,17 +140,17 @@ public static function parameter($key, $value) { * @param integer|float $value * @return */ - public static function metric($key, $value) { - if (!static::hasNewRelic()) { - return; - } + public static function metric($key, $value) { + if (!static::hasNewRelic()) { + return; + } - if (!is_numeric($value)) { - throw new CakeException('Value must be numeric'); - } + if (!is_numeric($value)) { + throw new CakeException('Value must be numeric'); + } - newrelic_custom_metric($key, $value); - } + newrelic_custom_metric($key, $value); + } /** * Add a custom method to have traced by NewRelic @@ -158,13 +158,13 @@ public static function metric($key, $value) { * @param string $method * @return void */ - public static function tracer($method) { - if (!static::hasNewRelic()) { - return; - } + public static function tracer($method) { + if (!static::hasNewRelic()) { + return; + } - newrelic_add_custom_tracer($method); - } + newrelic_add_custom_tracer($method); + } /** * Ignore an exception class @@ -172,9 +172,9 @@ public static function tracer($method) { * @param string $exception * @return void */ - public static function ignoreException($exception) { - static::$ignoredExceptions = array_merge(static::$ignoredExceptions, (array)$exception); - } + public static function ignoreException($exception) { + static::$ignoredExceptions = array_merge(static::$ignoredExceptions, (array)$exception); + } /** * Ignore error strings @@ -182,9 +182,9 @@ public static function ignoreException($exception) { * @param string $exception * @return void */ - public static function ignoreError($error) { - static::$ignoredErrors = array_merge(static::$ignoredErrors, (array)$error); - } + public static function ignoreError($error) { + static::$ignoredErrors = array_merge(static::$ignoredErrors, (array)$error); + } /** * Server variables to collect @@ -192,9 +192,9 @@ public static function ignoreError($error) { * @param array $variables * @return void */ - public static function collectServerVariables(array $variables) { - static::$serverVariables = array_merge(static::$serverVariables, (array)$variables); - } + public static function collectServerVariables(array $variables) { + static::$serverVariables = array_merge(static::$serverVariables, (array)$variables); + } /** * Cookie variables to collect @@ -202,9 +202,9 @@ public static function collectServerVariables(array $variables) { * @param array $variables * @return void */ - public static function collectCookieVariables(array $variables) { - static::$cookieVariables = array_merge(static::$cookieVariables, (array)$variables); - } + public static function collectCookieVariables(array $variables) { + static::$cookieVariables = array_merge(static::$cookieVariables, (array)$variables); + } /** * Send an exception to New Relic @@ -212,17 +212,18 @@ public static function collectCookieVariables(array $variables) { * @param Exception $exception * @return void */ - public static function sendException(Exception $exception) { - if (!static::hasNewRelic()) { - return; - } + public static function sendException(Exception $exception) { + if (!static::hasNewRelic()) { + return; + } - if (false !== array_search(get_class($exception), static::$ignoredExceptions)) { - return; - } + $exceptionClass = get_class($exception); + if (in_array($exceptionClass, static::$ignoredExceptions)) { + return; + } - newrelic_notice_error(null, $exception); - } + newrelic_notice_error(null, $exception); + } /** * Send an error to New Relic @@ -234,19 +235,19 @@ public static function sendException(Exception $exception) { * @param [type] $context [description] * @return [type] [description] */ - public static function sendError($code, $description, $file, $line, $context = null) { - if (!static::hasNewRelic()) { - return; - } + public static function sendError($code, $description, $file, $line, $context = null) { + if (!static::hasNewRelic()) { + return; + } - foreach (static::$ignoredErrors as $errorMessage) { - if (false !== strpos($description, $errorMessage)) { - return; - } - } + foreach (static::$ignoredErrors as $errorMessage) { + if (false !== strpos($description, $errorMessage)) { + return; + } + } - newrelic_notice_error($code, $description, $file, $line, $context); - } + newrelic_notice_error($code, $description, $file, $line, $context); + } /** * Set user attributes @@ -256,46 +257,46 @@ public static function sendError($code, $description, $file, $line, $context = n * @param string $product * @return void */ - public static function user($user, $account, $product) { - if (!static::hasNewRelic()) { - return; - } + public static function user($user, $account, $product) { + if (!static::hasNewRelic()) { + return; + } - newrelic_set_user_attributes($user, $account, $product); - } + newrelic_set_user_attributes($user, $account, $product); + } /** * Check if the NewRelic PHP extension is loaded * * @return boolean */ - public static function hasNewRelic() { - return extension_loaded('newrelic'); - } + public static function hasNewRelic() { + return extension_loaded('newrelic'); + } /** * Collect environmental data for the transaction * * @return void */ - public static function collect() { - static::parameter('_get', $_GET); - static::parameter('_post', $_POST); - static::parameter('_files', $_FILES); - - foreach ($_SERVER as $key => $value) { - if (!in_array($key, static::$serverVariables)) { - continue; - } - static::parameter('server_' . strtolower($key), $value); - } - - foreach ($_COOKIE as $key => $value) { - if (!in_array($key, static::$cookieVariables)) { - continue; - } - static::parameter('cookie_' . strtolower($key), $value); - } - } + public static function collect() { + static::parameter('_get', $_GET); + static::parameter('_post', $_POST); + static::parameter('_files', $_FILES); + + foreach ($_SERVER as $key => $value) { + if (!in_array($key, static::$serverVariables)) { + continue; + } + static::parameter('server_' . strtolower($key), $value); + } + + foreach ($_COOKIE as $key => $value) { + if (!in_array($key, static::$cookieVariables)) { + continue; + } + static::parameter('cookie_' . strtolower($key), $value); + } + } } From e2ddf2f2ba2857370f5e1255a80df87ba78bda25 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Thu, 20 Oct 2016 09:53:17 +0000 Subject: [PATCH 16/32] don't name a transaction unless it got a non-empty name --- src/Lib/NewRelic.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 1c8469f..1a13556 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -45,7 +45,10 @@ public static function start($name = null) { } newrelic_start_transaction(NEW_RELIC_APP_NAME); - newrelic_name_transaction($name); + + if ($name) { + newrelic_name_transaction($name); + } } /** From 2c3dd2b44c61e60ed9988e4385a6ad0c2ad1953d Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Thu, 20 Oct 2016 11:04:20 +0000 Subject: [PATCH 17/32] a bit of refactoring --- src/Lib/NewRelic.php | 6 +++++- src/Traits/NewRelicTrait.php | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 1a13556..13d93cf 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -19,6 +19,8 @@ class NewRelic { protected static $cookieVariables = []; + protected static $currentTransactionName; + /** * Change the application name * @@ -47,8 +49,10 @@ public static function start($name = null) { newrelic_start_transaction(NEW_RELIC_APP_NAME); if ($name) { - newrelic_name_transaction($name); + static::$currentTransactionName = $name; } + + newrelic_name_transaction(static::$currentTransactionName); } /** diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index 7b6fc7e..f1045dc 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -13,7 +13,7 @@ trait NewRelicTrait { * * @var string */ - protected $_name; + protected $_newrelicTransactionName; /** * Set the transaction name @@ -25,14 +25,14 @@ trait NewRelicTrait { */ public function setName($name) { if ($name instanceof Shell) { - $name = $this->_deriveNameFromShell($name); + $name = $this->_deriveNameFromShell($name); } if ($name instanceof Request) { $name = $this->_deriveNameFromRequest($name); } - $this->_name = $name; + $this->_newrelicTransactionName = $name; } /** @@ -41,7 +41,7 @@ public function setName($name) { * @return string */ public function getName() { - return $this->_name; + return $this->_newrelicTransactionName; } /** @@ -165,7 +165,7 @@ protected function _getTransactionName($name) { return $name; } - return $this->_name; + return $this->_newrelicTransactionName; } /** From 772487ca7e4e4d6674f51c73dbc13bb83b264f81 Mon Sep 17 00:00:00 2001 From: Mikkel Bonde Date: Thu, 23 Feb 2017 17:45:23 +0100 Subject: [PATCH 18/32] RFC: Support for exceptions with cake > 3.3 middleware --- .../NewRelicErrorHandlerMiddleware.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Middleware/NewRelicErrorHandlerMiddleware.php diff --git a/src/Middleware/NewRelicErrorHandlerMiddleware.php b/src/Middleware/NewRelicErrorHandlerMiddleware.php new file mode 100644 index 0000000..d5542d1 --- /dev/null +++ b/src/Middleware/NewRelicErrorHandlerMiddleware.php @@ -0,0 +1,22 @@ + Date: Thu, 1 Mar 2018 09:35:17 +0200 Subject: [PATCH 19/32] Allow for PHP7+ errors PHP7 errors/throwables cause a fatal error when calling `NewRelic::sendException`. Fix that. --- src/Lib/NewRelic.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index 13d93cf..c4c8b0c 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -216,10 +216,10 @@ public static function collectCookieVariables(array $variables) { /** * Send an exception to New Relic * - * @param Exception $exception + * @param Exception|Throwable $exception * @return void */ - public static function sendException(Exception $exception) { + public static function sendException($exception) { if (!static::hasNewRelic()) { return; } From 78a3a1a001ef7450e2a2400fc4878c4b57e90f60 Mon Sep 17 00:00:00 2001 From: Mark Sch Date: Fri, 29 Jun 2018 11:48:35 +0200 Subject: [PATCH 20/32] Fix up readme for 3.x --- README.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e498b4d..08f52bd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CakePHP <3 NewRelic -You can modify your files like this +You can modify your files like this to have full NewRelic support. ## Things included @@ -9,9 +9,18 @@ You can modify your files like this - NewRelicTrait trait - NewRelic.NewRelic -### Console +## Installation -Include this snippet in `app/Console/AppShell.php` +Note: This branch is for CakePHP 3. + +``` +composer require jippi/cakephp-newrelic: +``` + + +### Shell + +Include this snippet in `src/Shell/AppShell.php` ```php public function startup() { @@ -29,12 +38,11 @@ Include this snippet in `app/Console/AppShell.php` Simply add `NewRelic.NewRelic` to your `$components` list -## app/webroot/index.php +## webroot/index.php Add this in top of your file before `define('DS', 'DIRECTORY_SEPARATOR')` ```php - Date: Fri, 29 Jun 2018 11:52:20 +0200 Subject: [PATCH 21/32] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08f52bd..1111f68 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You can modify your files like this to have full NewRelic support. Note: This branch is for CakePHP 3. ``` -composer require jippi/cakephp-newrelic: +composer require jippi/cakephp-newrelic:dev-cake3 ``` From 9fb765e9a4ee2092ef9adeccaf8626b95946b490 Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Mon, 6 Aug 2018 09:58:58 +0200 Subject: [PATCH 22/32] Use non-deprecated request methods --- src/Traits/NewRelicTrait.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index f1045dc..a5601ac 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -196,21 +196,21 @@ protected function _deriveNameFromShell(Shell $shell) { protected function _deriveNameFromRequest(Request $request) { $name = []; - if ($request->prefix) { - $name[] = $request->prefix; + if ($request->getParam('prefix')) { + $name[] = $request->getParam('prefix'); } - if ($request->plugin) { - $name[] = $request->plugin; + if ($request->getParam('plugin')) { + $name[] = $request->getParam('plugin'); } - $name[] = $request->controller; - $name[] = $request->action; + $name[] = $request->getParam('controller'); + $name[] = $request->getParam('action'); $name = join('/', $name); - if ($request->ext) { - $name .= '.' . $request->ext; + if ($request->getParam('ext')) { + $name .= '.' . $request->getParam('ext'); } return $name; From aada7805517a11461cf9852a25fdf49695f49a9e Mon Sep 17 00:00:00 2001 From: Daniel Penkov Date: Tue, 20 Nov 2018 13:12:18 +0200 Subject: [PATCH 23/32] Support Cake 3.6 in Component --- src/Controller/Component/NewRelicComponent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index e5a3c94..266cfab 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -23,10 +23,10 @@ class NewRelicComponent extends Component { * @return void */ public function beforeFilter(Event $event) { - $this->setName($event->subject->request); + $this->setName($event->getSubject()->request); $this->start(); - $controller = $event->subject; + $controller = $event->getSubject(); if (isset($controller->Auth)) { $this->user($controller->Auth->user('id'), $controller->Auth->user('email'), ''); } From ab392397351bf8e4ad43ea21d598955d991c4341 Mon Sep 17 00:00:00 2001 From: Mikkel Bonde Date: Tue, 20 Nov 2018 13:57:58 +0100 Subject: [PATCH 24/32] Specify lowest version of CakePHP 3.x compatible in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1111f68..225d3a9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ You can modify your files like this to have full NewRelic support. ## Installation -Note: This branch is for CakePHP 3. +Note: This branch is for CakePHP 3.4 and newer ``` composer require jippi/cakephp-newrelic:dev-cake3 From c6b36e20447e6dd829d9370bb9120150b31f9095 Mon Sep 17 00:00:00 2001 From: Mikkel Bonde Date: Fri, 15 Feb 2019 14:47:03 +0100 Subject: [PATCH 25/32] There is no such type in phpdoc as "scalar" - but according to https://secure.php.net/manual/en/function.is-scalar.php these are the scalar types in PHP --- src/Traits/NewRelicTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index a5601ac..95b5864 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -95,7 +95,7 @@ public function ignoreApdex() { * Add custom parameter to transaction * * @param string $key - * @param scalar $value + * @param integer|float|string|boolean $value * @return void */ public function parameter($key, $value) { From 6245bd897faaa0ddbcd81de749f6fff998b02f5d Mon Sep 17 00:00:00 2001 From: Mikkel Bonde Date: Fri, 15 Feb 2019 14:49:27 +0100 Subject: [PATCH 26/32] Non-scalar values are handled nicely in method. Setting type to the same as in https://github.com/jippi/cakephp-newrelic/blob/cake3/src/Lib/NewRelic.php#L129 Non-scalar will be `json_encode`d (https://github.com/jippi/cakephp-newrelic/blob/cake3/src/Lib/NewRelic.php#L136) --- src/Traits/NewRelicTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/NewRelicTrait.php b/src/Traits/NewRelicTrait.php index 95b5864..1e8c10e 100644 --- a/src/Traits/NewRelicTrait.php +++ b/src/Traits/NewRelicTrait.php @@ -95,7 +95,7 @@ public function ignoreApdex() { * Add custom parameter to transaction * * @param string $key - * @param integer|float|string|boolean $value + * @param mixed * @return void */ public function parameter($key, $value) { From 01c54c446c883bf94d3c5d680df689dc293ab3b6 Mon Sep 17 00:00:00 2001 From: Mikkel Bonde Date: Wed, 2 Oct 2019 15:06:21 +0200 Subject: [PATCH 27/32] Clearly state that the newrelic php module is required --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 225d3a9..a7aef08 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ You can modify your files like this to have full NewRelic support. - NewRelicTrait trait - NewRelic.NewRelic +## Requirements +- [New Relic PHP agent](https://docs.newrelic.com/docs/agents/php-agent/getting-started/introduction-new-relic-php) installed as a PHP module + ## Installation Note: This branch is for CakePHP 3.4 and newer From 181ba23025e96fc3b6a7fccb1820a61aa58aa70f Mon Sep 17 00:00:00 2001 From: Rafael Queiroz Date: Wed, 6 May 2020 13:45:04 -0300 Subject: [PATCH 28/32] Upgraded to CakePHP 4 --- composer.json | 11 +- phpstan-baseline.neon | 0 phpstan.neon | 12 + psalm-baseline.xml | 4 + psalm.xml | 20 ++ .../Component/NewRelicComponent.php | 27 +- src/Lib/NewRelic.php | 306 ++++++++++-------- .../NewRelicErrorHandlerMiddleware.php | 9 +- src/Traits/NewRelicTrait.php | 272 ++++++++-------- tests/bootstrap.php | 32 ++ 10 files changed, 405 insertions(+), 288 deletions(-) create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon create mode 100644 psalm-baseline.xml create mode 100644 psalm.xml create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json index a6c007b..ad03f6f 100644 --- a/composer.json +++ b/composer.json @@ -24,13 +24,14 @@ "irc":"irc://irc.freenode.org/friendsofcake" }, "require": { - "cakephp/cakephp": "~3.0", - "cakephp/plugin-installer": "*" + "cakephp/cakephp": "^4.0" }, "require-dev": { - "cakephp/cakephp-codesniffer": "dev-master", - "phpunit/phpunit": "4.1.*" - }, + "phpunit/phpunit": "*", + "cakephp/cakephp-codesniffer": "^4.0", + "phpstan/phpstan": "^0.12.18", + "psalm/phar": "~3.11.2" +}, "autoload": { "psr-4": { "NewRelic\\": "src", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..0f5f66f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,12 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + autoload_files: + - tests/bootstrap.php + ignoreErrors: + +services: \ No newline at end of file diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..d3a0c84 --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..0d865a6 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index 266cfab..82e677f 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -1,4 +1,6 @@ setName($event->getSubject()->request); + /** + * Called before the Controller::beforeFilter(). + * + * Start NewRelic and configure transaction name + * + * @param Event $event + * @return void + */ + public function beforeFilter(Event $event) + { + $this->setName($event->getSubject()->getRequest()); $this->start(); $controller = $event->getSubject(); @@ -33,5 +37,4 @@ public function beforeFilter(Event $event) { $this->captureParams(true); } - } diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index c4c8b0c..ced80e3 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -1,7 +1,10 @@ _deriveNameFromShell($name); + /** + * Set the transaction name + * + * If `$name` is a Shell instance, the name will + * automatically be derived based on best practices + * + * @param ServerRequestInterface $argument + */ + public function setName($argument) + { + $name = ""; + if ($argument instanceof Shell) { + $name = $this->_deriveNameFromShell($argument); } - - if ($name instanceof Request) { - $name = $this->_deriveNameFromRequest($name); + if ($argument instanceof ServerRequest) { + $name = $this->_deriveNameFromRequest($argument); } $this->_newrelicTransactionName = $name; } -/** - * Get the name - * - * @return string - */ - public function getName() { + /** + * Get the name + * + * @return string + */ + public function getName() + { return $this->_newrelicTransactionName; } -/** - * Change the application name - * - * @param string $name - * @return void - */ - public function applicationName($name) { + /** + * Change the application name + * + * @param string $name + * @return void + */ + public function applicationName($name) + { NewRelic::applicationName($name); } -/** - * Start a NewRelic transaction - * - * @param null|string $name - * @return void - */ - public function start($name = null) { + /** + * Start a NewRelic transaction + * + * @param null|string $name + * @return void + */ + public function start($name = null) + { NewRelic::start($this->_getTransactionName($name)); } -/** - * Stop a transaction - * - * @return void - */ - public function stop($ignore = false) { + /** + * Stop a transaction + * + * @return void + */ + public function stop($ignore = false) + { NewRelic::stop($ignore); } -/** - * Ignore current transaction - * - * @return void - */ - public function ignoreTransaction() { + /** + * Ignore current transaction + * + * @return void + */ + public function ignoreTransaction() + { NewRelic::ignoreTransaction(); } -/** - * Ignore current apdex - * - * @return void - */ - public function ignoreApdex() { + /** + * Ignore current apdex + * + * @return void + */ + public function ignoreApdex() + { NewRelic::ignoreApdex(); } -/** - * Add custom parameter to transaction - * - * @param string $key - * @param mixed - * @return void - */ - public function parameter($key, $value) { + /** + * Add custom parameter to transaction + * + * @param string $key + * @param mixed $value + * @return void + */ + public function parameter($key, $value) + { NewRelic::parameter($key, $value); } -/** - * Add custom metric - * - * @param string $key - * @param float $value - * @return void - */ - public function metric($key, $value) { + /** + * Add custom metric + * + * @param string $key + * @param float $value + * @return void + */ + public function metric($key, $value) + { NewRelic::metric($key, $value); } -/** - * capture params - * - * @param boolean $capture - * @return void - */ - public function captureParams($capture) { + /** + * capture params + * + * @param boolean $capture + * @return void + */ + public function captureParams($capture) + { NewRelic::captureParams($capture); } -/** - * Add custom tracer method - * - * @param string $method - */ - public function addTracer($method) { + /** + * Add custom tracer method + * + * @param string $method + */ + public function addTracer($method) + { NewRelic::addTracer($method); } -/** - * Set user attributes - * - * @param string $user - * @param string $account - * @param string $product - * @return void - */ - public function user($user, $account, $product) { + /** + * Set user attributes + * + * @param string $user + * @param string $account + * @param string $product + * @return void + */ + public function user($user, $account, $product) + { NewRelic::user($user, $account, $product); } -/** - * Send an exception to New Relic - * - * @param Exception $e - * @return void - */ - public function sendException(Exception $e) { + /** + * Send an exception to New Relic + * + * @param Exception $e + * @return void + */ + public function sendException(Exception $e) + { NewRelic::sendException($e); } -/** - * Get transaction name - * - * @param string $name - * @return string - */ - protected function _getTransactionName($name) { - if (is_string($name)) { + /** + * Get transaction name + * + * @param string $name + * @return string + */ + protected function _getTransactionName($name) + { + if ($name) { return $name; } return $this->_newrelicTransactionName; } -/** - * Derive the transaction name - * - * @param Shell $name - * @return string - */ - protected function _deriveNameFromShell(Shell $shell) { + /** + * Derive the transaction name + * + * @param Shell $shell + * @return string + */ + protected function _deriveNameFromShell(Shell $shell) + { $name = []; if ($shell->plugin) { @@ -188,14 +207,14 @@ protected function _deriveNameFromShell(Shell $shell) { } /** - * Compute name based on request information - * - * @param CakeRequest $request - * @return string - */ - protected function _deriveNameFromRequest(Request $request) { + * Compute name based on request information + * + * @param ServerRequest $request + * @return string + */ + protected function _deriveNameFromRequest(ServerRequest $request) + { $name = []; - if ($request->getParam('prefix')) { $name[] = $request->getParam('prefix'); } @@ -215,5 +234,4 @@ protected function _deriveNameFromRequest(Request $request) { return $name; } - } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..28fc48e --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,32 @@ + Date: Wed, 6 May 2020 13:47:11 -0300 Subject: [PATCH 29/32] Upgraded to CakePHP 4 --- src/Shell/Task/NewRelicTask.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Shell/Task/NewRelicTask.php b/src/Shell/Task/NewRelicTask.php index 63d2a54..ce02afc 100644 --- a/src/Shell/Task/NewRelicTask.php +++ b/src/Shell/Task/NewRelicTask.php @@ -1,10 +1,12 @@ Date: Wed, 6 May 2020 13:52:02 -0300 Subject: [PATCH 30/32] Upgraded to CakePHP 4 --- composer.json | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index ad03f6f..0103770 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,27 @@ { - "name":"jippi/cakephp-newrelic", + "name": "jippi/cakephp-newrelic", "version": "1.0.0", - "description":"CakePHP <3 NewRelic", - "type":"cakephp-plugin", - "keywords":[ + "description": "CakePHP <3 NewRelic", + "type": "cakephp-plugin", + "keywords": [ "cakephp", "newrelic", "apm", "plugin" ], - "homepage":"https://github.com/jippi/cakephp-newrelic", - "license":"MIT", - "authors":[ + "homepage": "https://github.com/jippi/cakephp-newrelic", + "license": "MIT", + "authors": [ { - "name":"Christian Winther", - "role":"Author", - "homepage":"http://cakephp.nu/" + "name": "Christian Winther", + "role": "Author", + "homepage": "http://cakephp.nu/" } ], - "support":{ - "source":"https://github.com/jippi/cakephp-newrelic", - "issues":"https://github.com/jippi/cakephp-newrelic/issues", - "irc":"irc://irc.freenode.org/friendsofcake" + "support": { + "source": "https://github.com/jippi/cakephp-newrelic", + "issues": "https://github.com/jippi/cakephp-newrelic/issues", + "irc": "irc://irc.freenode.org/friendsofcake" }, "require": { "cakephp/cakephp": "^4.0" @@ -31,7 +31,7 @@ "cakephp/cakephp-codesniffer": "^4.0", "phpstan/phpstan": "^0.12.18", "psalm/phar": "~3.11.2" -}, + }, "autoload": { "psr-4": { "NewRelic\\": "src", @@ -47,4 +47,4 @@ "extra": { "installer-name": "NewRelic" } -} +} \ No newline at end of file From 94dea31ce1bfdc723db2dda2f2fdaa60dec62c47 Mon Sep 17 00:00:00 2001 From: Rafael Queiroz Date: Wed, 6 May 2020 13:53:15 -0300 Subject: [PATCH 31/32] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a7aef08..c3bca12 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ You can modify your files like this to have full NewRelic support. ## Installation -Note: This branch is for CakePHP 3.4 and newer +Note: This branch is for CakePHP 4.x ``` -composer require jippi/cakephp-newrelic:dev-cake3 +composer require jippi/cakephp-newrelic:dev-cake4 ``` From 4b9ed99707617dfeee8ab437e74df8bbeb6accab Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Sat, 5 Aug 2023 13:51:05 +0200 Subject: [PATCH 32/32] initial upgrade to CakePHP5, updated types, updated phpunit settings, cc --- .editorconfig | 21 +++ .gitignore | 32 ++++ README.md | 2 +- VERSION.txt | 1 + composer.json | 20 ++- config/bootstrap.php | 6 + phpunit.xml.dist | 23 +++ .../Component/NewRelicComponent.php | 9 +- src/Lib/NewRelic.php | 98 ++++++----- src/Shell/Task/NewRelicTask.php | 13 -- src/Traits/NewRelicTrait.php | 157 +++++++++--------- tests/bootstrap.php | 17 +- tests/schema.php | 4 + 13 files changed, 248 insertions(+), 155 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 VERSION.txt create mode 100644 config/bootstrap.php create mode 100644 phpunit.xml.dist delete mode 100644 src/Shell/Task/NewRelicTask.php create mode 100644 tests/schema.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f7e2a69 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.neon] +indent_style = tab + +[phars.xml] +indent_size = 2 + +[*.js] +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2378dde --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +/composer.lock +*.diff +*.err +*.log +*.orig +*.rej +*.swo +*.swp +*.vi +*~ +.idea/* +nbproject/* +.vscode +.DS_Store +.cache +.phpunit.cache +.project +.settings +.svn +errors.err +tags +node_modules +package-lock.json +/.phpunit.result.cache +/nbproject/ +/tools +/vendor +/phpunit.xml +/webroot/css/style.css.map +/webroot/mix.js.map +/webroot/mix-manifest.json +.ddev/* \ No newline at end of file diff --git a/README.md b/README.md index c3bca12..6fc3f99 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You can modify your files like this to have full NewRelic support. ## Installation -Note: This branch is for CakePHP 4.x +Note: This branch is for CakePHP 5.x ``` composer require jippi/cakephp-newrelic:dev-cake4 diff --git a/VERSION.txt b/VERSION.txt new file mode 100644 index 0000000..2468aa9 --- /dev/null +++ b/VERSION.txt @@ -0,0 +1 @@ +3.0.0-dev diff --git a/composer.json b/composer.json index 0103770..49fa85a 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "jippi/cakephp-newrelic", - "version": "1.0.0", + "version": "3.0.0", "description": "CakePHP <3 NewRelic", "type": "cakephp-plugin", "keywords": [ @@ -24,14 +24,15 @@ "irc": "irc://irc.freenode.org/friendsofcake" }, "require": { - "cakephp/cakephp": "^4.0" + "php": ">=8.1", + "cakephp/cakephp": "5.x-dev" }, "require-dev": { - "phpunit/phpunit": "*", - "cakephp/cakephp-codesniffer": "^4.0", - "phpstan/phpstan": "^0.12.18", - "psalm/phar": "~3.11.2" + "phpunit/phpunit": "^10.1.0", + "cakephp/cakephp-codesniffer": "^5.0" }, + "minimum-stability": "dev", + "prefer-stable": true, "autoload": { "psr-4": { "NewRelic\\": "src", @@ -46,5 +47,10 @@ }, "extra": { "installer-name": "NewRelic" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } -} \ No newline at end of file +} diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000..f651d7d --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,6 @@ + + + + + + + + + + + + tests/TestCase/ + + + + + + + + src/ + + + diff --git a/src/Controller/Component/NewRelicComponent.php b/src/Controller/Component/NewRelicComponent.php index 82e677f..4c5e714 100644 --- a/src/Controller/Component/NewRelicComponent.php +++ b/src/Controller/Component/NewRelicComponent.php @@ -4,7 +4,7 @@ namespace NewRelic\Controller\Component; use Cake\Controller\Component; -use Cake\Event\Event; +use Cake\Event\EventInterface; use NewRelic\Traits\NewRelicTrait; /** @@ -14,7 +14,6 @@ */ class NewRelicComponent extends Component { - use NewRelicTrait; /** @@ -22,11 +21,11 @@ class NewRelicComponent extends Component * * Start NewRelic and configure transaction name * - * @param Event $event + * @param \Cake\Event\EventInterface $event * @return void */ - public function beforeFilter(Event $event) - { + public function beforeFilter(EventInterface $event): void + { $this->setName($event->getSubject()->getRequest()); $this->start(); diff --git a/src/Lib/NewRelic.php b/src/Lib/NewRelic.php index ced80e3..6147a24 100644 --- a/src/Lib/NewRelic.php +++ b/src/Lib/NewRelic.php @@ -14,24 +14,23 @@ */ class NewRelic { + protected static array $ignoredExceptions = []; - protected static $ignoredExceptions = []; + protected static array $ignoredErrors = []; - protected static $ignoredErrors = []; + protected static array $serverVariables = []; - protected static $serverVariables = []; + protected static array $cookieVariables = []; - protected static $cookieVariables = []; - - protected static $currentTransactionName; + protected static string $currentTransactionName; /** * Change the application name * - * @param string $name + * @param string $name * @return void */ - public static function applicationName($name) + public static function applicationName(string $name): void { if (!static::hasNewRelic()) { return; @@ -43,10 +42,10 @@ public static function applicationName($name) /** * Start a New Relic transaction * - * @param string $name + * @param string|null $name * @return void */ - public static function start($name = null) + public static function start(?string $name = null): void { if (!static::hasNewRelic()) { return; @@ -64,10 +63,10 @@ public static function start($name = null) /** * End a New Relic transaction * - * @param boolean $ignore Should the statistics NewRelic gathered be discarded? + * @param boolean $ignore Should the statistics NewRelic gathered be discarded? * @return void */ - public static function stop($ignore = false) + public static function stop(bool $ignore = false): void { if (!static::hasNewRelic()) { return; @@ -81,7 +80,7 @@ public static function stop($ignore = false) * * @return void */ - public static function ignoreTransaction() + public static function ignoreTransaction(): void { if (!static::hasNewRelic()) { return; @@ -95,7 +94,7 @@ public static function ignoreTransaction() * * @return void */ - public static function ignoreApdex() + public static function ignoreApdex(): void { if (!static::hasNewRelic()) { return; @@ -107,10 +106,10 @@ public static function ignoreApdex() /** * Should NewRelic capture params ? * - * @param boolean $boolean + * @param boolean $boolean * @return void */ - public static function captureParams($boolean) + public static function captureParams(bool $boolean): void { if (!static::hasNewRelic()) { return; @@ -124,7 +123,7 @@ public static function captureParams($boolean) * * @param string $method */ - public static function addTracer($method) + public static function addTracer(string $method): void { if (!static::hasNewRelic()) { return; @@ -138,8 +137,9 @@ public static function addTracer($method) * * @param string $key * @param mixed $value + * @return false|void */ - public static function parameter($key, $value) + public static function parameter(string $key, mixed $value) { if (!static::hasNewRelic()) { return false; @@ -155,18 +155,19 @@ public static function parameter($key, $value) /** * Track a custom metric * - * @param string $key - * @param integer|float $value - * @return void|\Exception + * @param string $key + * @param float|integer $value + * @return void + * @throws \Exception */ - public static function metric($key, $value) + public static function metric(string $key, float|int $value): void { if (!static::hasNewRelic()) { return; } if (!is_numeric($value)) { - throw new \Exception('Value must be numeric'); + throw new Exception('Value must be numeric'); } newrelic_custom_metric($key, $value); @@ -175,10 +176,10 @@ public static function metric($key, $value) /** * Add a custom method to have traced by NewRelic * - * @param string $method + * @param string $method * @return void */ - public static function tracer($method) + public static function tracer(string $method): void { if (!static::hasNewRelic()) { return; @@ -190,10 +191,10 @@ public static function tracer($method) /** * Ignore an exception class * - * @param string $exception + * @param string $exception * @return void */ - public static function ignoreException($exception) + public static function ignoreException(string $exception): void { static::$ignoredExceptions = array_merge(static::$ignoredExceptions, (array) $exception); } @@ -201,10 +202,10 @@ public static function ignoreException($exception) /** * Ignore error strings * - * @param string $error + * @param string $error * @return void */ - public static function ignoreError($error) + public static function ignoreError(string $error): void { static::$ignoredErrors = array_merge(static::$ignoredErrors, (array) $error); } @@ -215,9 +216,9 @@ public static function ignoreError($error) * @param array $variables * @return void */ - public static function collectServerVariables(array $variables) + public static function collectServerVariables(array $variables): void { - static::$serverVariables = array_merge(static::$serverVariables, (array) $variables); + static::$serverVariables = array_merge(static::$serverVariables, $variables); } /** @@ -226,18 +227,18 @@ public static function collectServerVariables(array $variables) * @param array $variables * @return void */ - public static function collectCookieVariables(array $variables) + public static function collectCookieVariables(array $variables): void { - static::$cookieVariables = array_merge(static::$cookieVariables, (array) $variables); + static::$cookieVariables = array_merge(static::$cookieVariables, $variables); } /** * Send an exception to New Relic * - * @param Exception|Throwable $exception + * @param \Throwable|\Exception $exception * @return void */ - public static function sendException($exception) + public static function sendException(Throwable|Exception $exception): void { if (!static::hasNewRelic()) { return; @@ -248,7 +249,7 @@ public static function sendException($exception) return; } - newrelic_notice_error(null, $exception); + newrelic_notice_error(0, $exception); } /** @@ -258,17 +259,22 @@ public static function sendException($exception) * @param mixed $description * @param mixed $file * @param mixed $line - * @param mixed $context + * @param mixed|null $context * @return void */ - public static function sendError($code, $description, $file, $line, $context = null) - { + public static function sendError( + mixed $code, + mixed $description, + mixed $file, + mixed $line, + mixed $context = null + ): void { if (!static::hasNewRelic()) { return; } foreach (static::$ignoredErrors as $errorMessage) { - if (false !== strpos($description, $errorMessage)) { + if (str_contains($description, $errorMessage)) { return; } } @@ -279,12 +285,12 @@ public static function sendError($code, $description, $file, $line, $context = n /** * Set user attributes * - * @param string $user - * @param string $account - * @param string $product + * @param string $user + * @param string $account + * @param string $product * @return void */ - public static function user($user, $account, $product) + public static function user(string $user, string $account, string $product): void { if (!static::hasNewRelic()) { return; @@ -298,7 +304,7 @@ public static function user($user, $account, $product) * * @return boolean */ - public static function hasNewRelic() + public static function hasNewRelic(): bool { return extension_loaded('newrelic'); } @@ -308,7 +314,7 @@ public static function hasNewRelic() * * @return void */ - public static function collect() + public static function collect(): void { static::parameter('_get', $_GET); static::parameter('_post', $_POST); diff --git a/src/Shell/Task/NewRelicTask.php b/src/Shell/Task/NewRelicTask.php deleted file mode 100644 index ce02afc..0000000 --- a/src/Shell/Task/NewRelicTask.php +++ /dev/null @@ -1,13 +0,0 @@ -_deriveNameFromShell($argument); + if ($argument instanceof Command) { + $name = $this->_deriveNameFromCommand($argument); } if ($argument instanceof ServerRequest) { $name = $this->_deriveNameFromRequest($argument); @@ -45,40 +47,41 @@ public function setName($argument) * * @return string */ - public function getName() - { + public function getName(): string + { return $this->_newrelicTransactionName; } /** * Change the application name * - * @param string $name + * @param string $name * @return void */ - public function applicationName($name) - { + public function applicationName(string $name): void + { NewRelic::applicationName($name); } /** * Start a NewRelic transaction * - * @param null|string $name + * @param string|null $name * @return void */ - public function start($name = null) - { + public function start(?string $name = null): void + { NewRelic::start($this->_getTransactionName($name)); } - /** - * Stop a transaction - * - * @return void - */ - public function stop($ignore = false) - { + /** + * Stop a transaction + * + * @param bool $ignore + * @return void + */ + public function stop(bool $ignore = false): void + { NewRelic::stop($ignore); } @@ -87,8 +90,8 @@ public function stop($ignore = false) * * @return void */ - public function ignoreTransaction() - { + public function ignoreTransaction(): void + { NewRelic::ignoreTransaction(); } @@ -97,43 +100,44 @@ public function ignoreTransaction() * * @return void */ - public function ignoreApdex() - { + public function ignoreApdex(): void + { NewRelic::ignoreApdex(); } /** * Add custom parameter to transaction * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value * @return void */ - public function parameter($key, $value) - { + public function parameter(string $key, mixed $value): void + { NewRelic::parameter($key, $value); } - /** - * Add custom metric - * - * @param string $key - * @param float $value - * @return void - */ - public function metric($key, $value) - { + /** + * Add custom metric + * + * @param string $key + * @param float $value + * @return void + * @throws \Exception + */ + public function metric(string $key, float $value): void + { NewRelic::metric($key, $value); } /** * capture params * - * @param boolean $capture + * @param boolean $capture * @return void */ - public function captureParams($capture) - { + public function captureParams(bool $capture): void + { NewRelic::captureParams($capture); } @@ -142,43 +146,43 @@ public function captureParams($capture) * * @param string $method */ - public function addTracer($method) - { + public function addTracer(string $method): void + { NewRelic::addTracer($method); } /** * Set user attributes * - * @param string $user - * @param string $account - * @param string $product + * @param string $user + * @param string $account + * @param string $product * @return void */ - public function user($user, $account, $product) - { + public function user(string $user, string $account, string $product): void + { NewRelic::user($user, $account, $product); } /** * Send an exception to New Relic * - * @param Exception $e + * @param \Exception $e * @return void */ - public function sendException(Exception $e) - { + public function sendException(Exception $e): void + { NewRelic::sendException($e); } /** * Get transaction name * - * @param string $name + * @param string $name * @return string */ - protected function _getTransactionName($name) - { + protected function _getTransactionName(string $name): string + { if ($name) { return $name; } @@ -189,31 +193,22 @@ protected function _getTransactionName($name) /** * Derive the transaction name * - * @param Shell $shell + * @param \Cake\Command\Command $command * @return string */ - protected function _deriveNameFromShell(Shell $shell) - { - $name = []; - - if ($shell->plugin) { - $name[] = $shell->plugin; - } - - $name[] = $shell->name; - $name[] = $shell->command; - - return join('/', $name); + protected function _deriveNameFromCommand(Command $command): string + { + return $command->getName(); } /** * Compute name based on request information * - * @param ServerRequest $request + * @param \Cake\Http\ServerRequest $request * @return string */ - protected function _deriveNameFromRequest(ServerRequest $request) - { + protected function _deriveNameFromRequest(ServerRequest $request): string + { $name = []; if ($request->getParam('prefix')) { $name[] = $request->getParam('prefix'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 28fc48e..c9f40d2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,12 +1,19 @@ loadInternalFile(env('FIXTURE_SCHEMA_METADATA')); +} diff --git a/tests/schema.php b/tests/schema.php new file mode 100644 index 0000000..353ace1 --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,4 @@ +