One of the new features that you get out of the box with Magento 2 is New Relic reporting.
New Relic has been around for about the same amount of time as Magento. I always liked projects that were using New Relic for performance analysis because it was much easier to identify preformance bottlenecks – especially on production systems where you have little bit less debugging possibilities. That’s why I’m really excited to have it in the new Magento version.
Unfortunately, I still didn’t have a chance to use it in production environment and it still confuses me a bit 🙂
From the feature’s standpoint, it integrates New Relic APM (Application Preformance Monitoring) and New Relic Insights with Magento 2 in a similar way New Relic Reporting extension developed by Blue Acorn did it for Magento 1. It actually looks like a port of that extension for Magento 2.
So, what about this Insights thing?!
Well, some time back we mostly utilized APM functionality. Setup was pretty simple. You needed New Relic agent installed in the form of a php extension and things would start to happen themselves.
You can check how to install New Relic agent here.
At that point you could have code-level visibility performance tracking that allowed you to analyse your code, database queries, errors and similar.
Insights just brought it all up one level higher so you could also monitor user behaviour or business transactions and see how your data is actually related to APM findings.
Being able to add custom attributes and events there, you can have many dashboards with any kind of information. For example, according to their own documentation, you could attach user names to failing requests to see if particular users have an unusually poor experience.
And you could do that using the New Relic Query Language (NRQL) which is is an SQL-flavored query language for making calls against the Insights Events database.
Since it would be little bit too much to write about all features of New Relic itself, let’s keep things little bit closer to Magento 2.
All starts with installing New Relic user agent. Like always.
After that you can find all settings in Magento 2 administration interface, under:
Stores->Configuration [General]->New Relic reporting.
It should be quite straight forward to configure it since all you need to do is to provide data it asks for. It does require you to have New Relic account though.
At that point you have successfuly connected Magento and New Relic. Or did you?
You will be able to monitor many things from flushing your cache or deleting the product in admin to placing the order on your frontend. At least you should be able to do that.
I mentioned before that this module is confusing me. It seems that even though Magento does reporting, sometimes there is a delay measured in hours or I just can’t find submitted data.
As I understood, Insights should work in the real time…
I won’t just jump to conclusions because I might have done something wrong in my own setup and it will take me more time to investigate, but I’m really interested if there is anybody who tried this too. Please share your experience in comments.
So, how it is done anyway!?
Here is how it works for orders placed on frontend:
<config xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="newrelicreporting_observer_report_sales_order_place" instance="MagentoNewRelicReportingModelObserverReportOrderPlaced"/>
<observer name="newrelicreporting_newrelic_report_sales_order_place" instance="MagentoNewRelicReportingModelObserverReportOrderPlacedToNewRelic />
</event>
</config>
First it hooks up to sales_order_place_after event:
It continues by executing method ReportOrderPlaced::execute()
/**
* Reports orders placed to the database reporting_orders table
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
if ($this->config->isNewRelicEnabled()) {
/** @var MagentoSalesModelOrder $order */
$order = $observer->getEvent()->getOrder();
$itemCount = $order->getTotalItemCount();
if (!is_numeric($itemCount) && empty($itemCount)) {
$itemCount = $order->getTotalQtyOrdered();
}
$modelData = [
'customer_id' => $order->getCustomerId(),
'total' => $order->getGrandTotal(),
'total_base' => $order->getBaseGrandTotal(),
'item_count' => $itemCount,
'updated_at' => $this->dateTime->formatDate(true)
];
/** @var MagentoNewRelicReportingModelOrders $orderModel */
$orderModel = $this->ordersFactory->create();
$orderModel->setData($modelData);
$orderModel->save();
}
}
and ReportOrderPlacedToNewRelic::execute() method:
/**
* Reports orders placed to New Relic
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
if ($this->config->isNewRelicEnabled()) {
/** @var MagentoSalesModelOrder $order */
$order = $observer->getEvent()->getOrder();
$itemCount = $order->getTotalItemCount();
if (!is_numeric($itemCount) && empty($itemCount)) {
$itemCount = $order->getTotalQtyOrdered();
}
$this->newRelicWrapper->addCustomParameter(Config::ORDER_PLACED, 1);
$this->newRelicWrapper->addCustomParameter(Config::ORDER_ITEMS, $itemCount);
$this->newRelicWrapper->addCustomParameter(Config::ORDER_VALUE, $order->getBaseGrandTotal());
}
}
There is also one confusing thing…
I just can’t find what Magento actually does with this report that is saved in database.
Anyone?!
It reports to New Relic directly since addCustomParameter only wraps agent’s function newrelic_add_custom_parameter like this:
/**
* Wrapper for 'newrelic_add_custom_parameter' function
*
* @param string $param
* @param string|int $value
* @return bool
*/
public function addCustomParameter($param, $value)
{
if (extension_loaded('newrelic')) {
newrelic_add_custom_parameter($param, $value);
return true;
}
return false;
}
Additionaly, Magento will use cron to periodically report what is happening with your modules (enabling, disabling, installing, uninstalling) or general counts about your system, for example:
$this->addCustomParameters([
Config::PRODUCT_COUNT => $this->counter->getAllProductsCount(),
Config::CONFIGURABLE_COUNT => $this->counter->getConfigurableCount(),
Config::ACTIVE_COUNT => $this->counter->getActiveCatalogSize(),
Config::CATEGORY_COUNT => $this->counter->getCategoryCount(),
Config::WEBSITE_COUNT => $this->counter->getWebsiteCount(),
Config::STORE_VIEW_COUNT => $this->counter->getStoreViewsCount(),
Config::CUSTOMER_COUNT => $this->counter->getCustomerCount(),
]);
Even though I tried to write about New Relic Reporting in Magento, it is still new to me and it would take me much more than one blog post to go through everything. But, it is there and I’m looking forward to getting to know this module a little bit better. However, so far this wasn’t really much of an experience for me when it comes to Insights.
For now, idea is great and I will definitely get to it little bit better, especially because many additional customizations are coming to my mind for the future, but I sure need more investigating.
In case you feel you need some extra help, we can offer you a detailed custom report based on our technical audit – feel free to get in touch and see what we can do for you!
Please feel free to share your experience!