Magento and New Relic Error Rate
Published: November 13, 2017
New Relic’s Error rate monitoring and alerting feature is a great way to catch unforeseen issues in production. However, properly using the feature requires an understanding of what is actually being measured.
In this post, we’ll take a look at what what New Relic’s “Error Rate” means for Magento applications.
PHP Errors
There are a few ways that “errors” can manifest themselves in Magento systems. One such way is as a PHP error. For example, let’s say there was syntax error in index.php
as follows…
$ head -n 5 index.php
<?php
Whoops <-- This will fatal error
/**
* Application entry point
*
One the frontend the user will see a screen like this…
The error will also be logged to PHP’s error_log
like this…
PHP Parse error: syntax error, unexpected 'will' (T_STRING), expecting '[' in /Applications/MAMP/htdocs/magento2.2-develop/index.php on line 2
The New Relic PHP agent will detect these errors by default and report them to New Relic, and they will be tracked when measuring New Relic’s Error Rate.
Handled Exceptions
Another possible “error” scenario in Magento is a handled exception. An example of this would be if app/etc/env.php
specified invalid database credentials. In this case Magento would not able to establish a connection to the database. The user would be presented a screen like this…
Details on the “error” could then be found in var/report/1089155067867
…
$ cat var/report/1089155067867
...SQLSTATE[HY000] [1045] Access denied for user...
By default, this “Error” would not be reported to New Relic, however. This is because it isn’t truly a PHP Error, but rather an Exception
that Magento handled. As such, by default, these types of “Errors” would not be accounted for in New Relic’s Error Rate.
Upcoming Changes In Magento 2.2
In pull request #11944, I introduced a changeset that uses newrelic_notice_error
to report handled exceptions to New Relic. When these changes become available, New Relic Error Rate will begin to account for handled exceptions.
As I’m not a Magento employee I can’t personally promise which release these changes will go into, but I’m hopeful that they’ll be available in v2.2.2.
What About Magento 1
In Magento 1, exceptions are caught in Mage::run
try {
// Run application
} catch (Exception $e) {
if (self::isInstalled() || self::$_isDownloader) {
self::printException($e);
exit();
}
// If Magento isn't installed
}
There isn’t a great way to integrate the changes to report handled exceptions to New Relic without modifying core, but core can be changed to do this as follows…
try {
// Run application
} catch (Exception $e) {
if (self::isInstalled() || self::$_isDownloader) {
if (extension_loaded('newrelic')) {
newrelic_notice_error($e->getMessage(), $e);
}
self::printException($e);
exit();
}
// If Magento isn't installed
}
Hope you found these details helpful!