Magento 2 New Relic Reporting

Magento 2 New Relic Reporting

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.
nr_2

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.
nr_1
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="Magento\NewRelicReporting\Model\Observer\ReportOrderPlaced"/>
       <observer name="newrelicreporting_newrelic_report_sales_order_place" instance="Magento\NewRelicReporting\Model\Observer\ReportOrderPlacedToNewRelic />
   </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 \Magento\Sales\Model\Order $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 \Magento\NewRelicReporting\Model\Orders $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 \Magento\Sales\Model\Order $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!

You made it all the way down here so you must have enjoyed this post! You may also like:

3 best open-source eCommerce platforms in 2021 Zrinka Antolovic
Zrinka Antolovic, | 8

3 best open-source eCommerce platforms in 2021

Introducing BrexitCart by Inchoo – a new cart abandonment solution Aron Stanic
Aron Stanic, | 1

Introducing BrexitCart by Inchoo – a new cart abandonment solution

Ready for 2nd Meet Magento Croatia with agenda focused on PWA? Book the dates! Maja Kardum
Maja Kardum, | 0

Ready for 2nd Meet Magento Croatia with agenda focused on PWA? Book the dates!

4 comments

  1. I’ve reviewed the full module and made some notes to its behavior:

    CLI:
    – bin/magento newrelic:create:deploy-marker –message=”” –change_log=”” [–user=””]
    – sends deployment message/change-log to deployment API endpoint

    Cron:
    – scheduled every 2 min
    – sends counts to Insights API endpoint: all/active product, config, category, website, storeview, customer
    – updates module status changes in DB (reporting_module_status, reporting_system_updates)
    – updates counts changes in DB (reporting_counts): all/active product, config, category

    Observer:
    – ReportOrderPlaced: updates order in DB (reporting_orders)
    – ReportOrderPlacedToNewRelic: adds order details to current web transaction (monitored by NewRelic agent)
    – ReportConcurrentUsers: updates customer action (logged-in only) in DB (reporting_users)
    – ReportConcurrentUsersToNewRelic: adds customer (logged-in only) details to current web transaction (monitored by NewRelic agent)
    – ReportSystemCacheFlush: updates cache flush action in DB (reporting_system_updates)
    – ReportSystemCacheFlushToNewRelic: sends cache flush action to deployment API endpoint
    – ReportConcurrentAdmins: updates admin action in DB (reporting_users)
    – ReportConcurrentAdminsToNewRelic: adds admin user details to current web transaction (monitored by NewRelic agent)
    – ReportProductSaved: updates backend product save action in DB (reporting_system_updates)
    – ReportProductSavedToNewRelic: adds product change flag to current web transaction (monitored by NewRelic agent)
    – ReportProductDeleted: updates backend product delete action in DB (reporting_system_updates)
    – ReportProductDeletedToNewRelic: adds product delete flag to current web transaction (monitored by NewRelic agent)

    Plugin:
    – CommandPlugin: adds command name to current web transaction (monitored by NewRelic agent)
    – HttpPlugin: adds exception message to current web transaction (monitored by NewRelic agent)
    – StatePlugin: adds app name (incl. area code) to current web transaction (monitored by NewRelic agent)
    – StatPlugin: adds cronjob profiling to current web transaction (monitored by NewRelic agent)

    So it looks like that the collected entries in the Magento DB are not used for any further purpose – it might be that they collect data for later features but for now it’s “dead data” as far as I can see.

  2. Hi. I’m a New Relic Engineer – and I think I can help clear some of this up.

    The Admin page configuration creates a new Event Type in Insights called Cron. The Cron event type will have your catalog size reported over time, so you can see if you are having slowdowns due to large data loads, etc.

    Separately from this, the Add Custom Parameter call will add additional Attributes to your Transaction events in Insights, such as Shopping Cart value, userID, etc.

    Finally, the wrapper is a best practice, so that you can remove New Relic and still have a functional application. It just checks for the presence of the agent before calling the PHP Agent API.

    Happy to chat further offline, @josephlocascio on Twitter.

  3. Is it me or does new relic just seem a bit expensive for what it is? or would it be better to use it for a few months after initial setup only to ensure your website is running optimally and then unsubscribe after that?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.