Monitoring

This page is for version 1.1. Read the current version.

For arlanix/notification 1.1.0

Monitoring

arlanix/module-notification-monitoring adds eleven events about the store itself. They cover failed and successful admin sign-ins, failed cron jobs, a stopped cron, invalid indexers, errors in the log, imports and exports that finished or failed, and a store view whose orders stopped. The store's rules decide where each event goes. See Monitoring in the manual.

How each event is detected

Each event comes from one of these sources:

Event Code Detected by
Admin login failed admin_login_failed Magento's backend_auth_user_login_failed event.
Admin logged in admin_login Magento's backend_auth_user_login_success event. admin.new_ip and admin.new_device compare the sign-in with the addresses and browsers the admin used in the past year.
Cron job failed cron_job_failed The check. It reads the jobs in cron_schedule with status error since the previous check.
Cron stopped cron_stopped The check, an admin page and the health URL. They compare the newest executed_at in cron_schedule with cron_stale_minutes.
Indexer needs reindex indexer_invalid The check. It reads each indexer's state.
Orders stopped orders_stalled The check. It compares each store view's orders and carts with the same minutes in earlier weeks.
Application error app_error A processor on Magento's main logger, for records at or above app_error_level.
Import finished, Import failed import_finished, import_failed A plugin on Magento\ImportExport\Model\Import::importSource().
Export finished, Export failed export_finished, export_failed Plugins on the export queue consumer and on ExportManagementInterface::export().

The check is the cron job arlanix_notification_monitoring_check. It runs every five minutes in the arlanix_notification cron group. You can also run it now with this command:

bin/magento arlanix:notification:monitoring:check

The checks for cron, indexers and orders report a change of state, not every check they make. They send the problem when it starts. When it ends, they send the recovery with severity Info. The last state is stored in arlanix_notification_monitoring_state. The module updates it with a conditional write, so when two checks run at the same time, the change is still reported once.

Cron job failed works differently. It is sent for each job that failed since the previous check, up to 50 per check.

Orders that stop

The check looks at each active store view where Watch the orders is Yes. It counts two things in the last Minutes looked at:

  • Orders, by sales_order.created_at.
  • Carts that shoppers changed, by quote.updated_at.

It also counts the same minutes on the same weekday in each earlier week. The expected number is the median of those weeks. So one unusual week, such as a sale or an outage, does not change it.

The check then decides what to send:

Result What is sent
The expected number of orders is below Orders expected at least. Nothing. Too few orders arrive at this time of the week to judge.
Orders are at or above Orders below (%) of the expected number. Nothing.
Orders are below it, and carts are at or above Carts as usual from (%) of the expected number. Orders stopped, with reason checkout_broken. Shoppers are on the site, but their orders fail.
Orders are below it, and carts are below Shoppers gone below (%). Orders stopped, with reason traffic_gone. Shoppers cannot reach the storefront.
Orders are below it, and carts are between the two thresholds. Nothing. There are too few carts to say that shoppers are there, and too many to say that they are gone.
Orders are below it, but the store has too few weeks of carts to compare. Nothing. Magento deletes carts after Quote Lifetime (days). Weeks before the oldest remaining cart are not counted, and the check needs at least half of the weeks in Weeks of carts compared.

The check sends a change only after Looks in a row before it is sent checks in a row have seen it (2 by default). This applies both to a stop and to its recovery, so one quiet window does not send a message. The pending_count column of arlanix_notification_monitoring_state holds the count so far.

The check treats a week as exactly 604,800 seconds. So after a daylight saving change, the weeks from before the change are compared one hour off. This lasts until those weeks drop out of the comparison.

Every setting except Looks in a row before it is sent can differ per store view. Set a value in the store view's scope, or switch the check off for a store view with too few orders.

The counts use the sales and checkout database connections, which a split database keeps separate. The queries use Magento's indexes on the store view and the time columns.

A stopped cron

Cron runs the check, so the check cannot report that cron itself has stopped. Two things outside cron check it instead:

  • An admin page, at most once every five minutes.
  • The health URL, each time it is requested.

Cron stopped is sent at once, without the queue, because cron also starts the queue consumer.

The health check

You can point an uptime monitor, such as UptimeRobot, Pingdom or a load balancer, at the health URL. To set it up:

  1. In Stores > Configuration > Arlanix > Notifications > Monitoring, set Health check key. The key is stored encrypted.

  2. Point the monitor at this URL:

    GET https://store.example.com/arlanix_monitoring/health/cron?key=…
    

    You can also send the key in the X-Health-Key header instead.

The URL gives these answers:

Answer Meaning
200 {"cron":"running","minutes_since_last_run":1,…} A cron job ran within cron_stale_minutes.
503 {"cron":"stopped",…} No cron job ran within that time. The request also sends Cron stopped, once.
503 {"cron":"unknown","minutes_since_last_run":null,…} cron_schedule shows that no job has ever run. This happens on a new installation whose cron is not set up yet, or after the table was emptied. Nothing is sent.
403 The key is wrong.
404 No key is set, so the URL is off.

The URL is a storefront route, and it answers with Cache-Control: no-store. If Varnish or the full-page cache caches by path, exclude this URL.

Before you point a monitor at the URL, check these cases:

  • If Add Store Code to Urls is on, include a store code in the URL, such as https://store.example.com/default/arlanix_monitoring/health/cron?key=….
  • In maintenance mode, Magento answers every storefront request with its own 503. The monitor then reports a stop during a deploy. Pause the monitor during deploys, or allow its address with bin/magento maintenance:allow-ips.
  • If the site has basic auth or an IP allowlist, as staging sites often do, give the monitor the credentials or allow its addresses. You can send the key in X-Health-Key next to an Authorization header.
  • A new installation answers 503 until cron has run once.

Application errors

A Monolog processor on Magento\Framework\Logger\Monolog turns log records into the Application error event. It works with Monolog 2 and 3. It reports records at or above app_error_level, which is 500 (Critical) by default. The event includes the message, the level, the exception class, and the file and line where the exception was thrown.

The processor follows these rules:

  • The same message is sent at most once every ten minutes.
  • When a record is written inside a database transaction, the event is sent at the end of the request, because the transaction may be rolled back.
  • Records about the notification modules themselves are not sent. So a failing delivery cannot cause notifications about itself.

Imports and exports

Every import that uses Magento's import model goes through Import::importSource(). This includes the Import page, the CSV API, Adobe Commerce's scheduled imports, and integrations built on the model. A plugin on that method reports the result:

  • If importSource() returns true, the plugin sends Import finished. The event has the created, updated and deleted counts, and the number of errors from the error aggregator. Rows with errors do not make the import fail.
  • If it returns false or throws an exception, the plugin sends Import failed, with the first error or the exception message. The plugin then throws the exception again.

The file name and the admin user come from the Import History row that Magento loads while the import runs. An import from the Import page has such a row. For an import that keeps no history, import.file and import.user are empty.

An export is a message on the export queue. The consumer exportProcessor creates the file. It catches its own errors and only logs them. So the module treats an export as finished when the consumer has written var/export/<file>.

To keep the reason for a failure, a plugin on ExportManagementInterface::export() records it before the consumer catches it. If the export fails after that step, for example because the directory is not writable, Export failed points to the log instead. If no consumer runs, no export is made and no event is sent.

Reporting an import or an export never changes its result. If gathering the event's data fails, the module logs the error, and the import or export continues.

Data it stores

The module stores data in two tables:

Table Contents
arlanix_notification_monitoring_state The last state of each check: cron, each indexer, and each store view's orders. It also counts the checks that have seen a change that is not sent yet.
arlanix_notification_monitoring_admin_seen The addresses and browsers each admin signed in from in the last year, stored as HMAC-SHA-256 with the store's encryption key. The rows are deleted when the admin user is deleted.

The admin's IP address and browser are personal fields. A rule sends them only when the rule's own message text names them.

arlanix_notification_monitoring_admin_seen is empty after installation. So each admin's first sign-in after the installation counts as a new address and a new device.

Behind a proxy

The module gets the address from Magento's RemoteAddress, which reads REMOTE_ADDR. Behind a load balancer, a CDN or Varnish that does not pass on the client's address, REMOTE_ADDR is the proxy's address. Then three things go wrong:

  • Every sign-in seems to come from the proxy.
  • From a new address is never Yes after the first sign-in.
  • A failed sign-in shows the proxy's address.

To fix it, make the web server set the client's address (nginx real_ip, Apache mod_remoteip). Or tell RemoteAddress which header to read, with your proxies as trusted, in a module's di.xml:

<type name="Magento\Framework\HTTP\PhpEnvironment\RemoteAddress">
    <arguments>
        <argument name="alternativeHeaders" xsi:type="array">
            <item name="x-forwarded-for" xsi:type="string">HTTP_X_FORWARDED_FOR</item>
        </argument>
        <argument name="trustedProxies" xsi:type="array">
            <item name="load-balancer" xsi:type="string">10.0.0.10</item>
        </argument>
    </arguments>
</type>

With trustedProxies, Magento takes the last address in the header that is not a trusted proxy. Without it, Magento takes the first address, which the client can set itself.

The module identifies a browser by the cookie arlanix_admin_device on the admin path. The cookie holds a random token and is HttpOnly. It is set again at each sign-in, so it lasts one year from the last sign-in. A private window, another browser or cleared cookies count as a new device. A new encryption key makes every stored address and browser count as new once.

Settings

The module has these settings:

Path Admin field Default Notes
arlanix_notification/monitoring/cron_stale_minutes Cron stopped after (minutes) 15
arlanix_notification/monitoring/health_key Health check key Stored encrypted. When empty, the URL is off.
arlanix_notification/monitoring/app_error_level Application errors from 500 400 Error, 500 Critical, 550 Alert, 600 Emergency.
arlanix_notification/order_flow/enabled Watch the orders 1 Per store view.
arlanix_notification/order_flow/window_minutes Minutes looked at 10 Per store view.
arlanix_notification/order_flow/order_weeks Weeks of orders compared 4 Per store view.
arlanix_notification/order_flow/cart_weeks Weeks of carts compared 3 Per store view.
arlanix_notification/order_flow/min_expected_orders Orders expected at least 3 Per store view.
arlanix_notification/order_flow/orders_below_percent Orders below (%) 25 Per store view.
arlanix_notification/order_flow/carts_normal_percent Carts as usual from (%) 60 Per store view.
arlanix_notification/order_flow/carts_collapsed_percent Shoppers gone below (%) 20 Per store view.
arlanix_notification/order_flow/confirmations Looks in a row before it is sent 2 Default scope only.