Home

Awesome

Elastic APM Integration for Magento 2

Stability : Experimental, work (very much) in early stages.

Screenshot of Magento Elastic APM trace Screenshot of Magento Elastic APM trace showing MySQL query

Installation:

  1. Install the module with composer
composer config repositories.cmtickle/module-elastic-apm vcs https://github.com/cmtickle/elastic-apm-magento.git
composer require --dev cmtickle/module-elastic-apm:dev-develop@dev
  1. Create a file at app/etc/apm.php based on the below, containing values appropriate to your environment.
 <?php
return [
    'serverUrl'             => 'http://apm-server:8200',
    'enabled'               => true,
    'transactionSampleRate' => 1, // Decimal, 0 to 1 .. e.g. 0.5 = 50% of transactions traced, 1 = 100%.
    'serviceName'           => 'magento', // Overridden by $_SERVER['HTTP_HOST'], special characters replaced with hyphens.
    'hostname'              => 'localhost', // Overridden by $_SERVER['HOSTNAME'].
    'environment'           => 'local',
    'stackTraceLimit'       => 1000,
    /*'secretToken'           => null,
    'serviceVersion'        => null,
    'frameworkName'         => 'magento2',
    'frameworkVersion'      => '2.4.5-p1',
    'timeout'               => 10,*/
];
  1. Enable the profiler by running
bin/magento dev:profiler:enable '{"drivers":[{"type":"Cmtickle\\ElasticApm\\Profiler\\Driver"}]}'
  1. (Optionally) Enable the database profiler Edit app/etc/env.php and add the below underneath db > connection > default
                'profiler' => [
                    'class' => '\\Cmtickle\\ElasticApm\\Profiler\\Db',
                    'enabled' => true
                ],

Running Kibana + Elastic APM:

A sample docker-compose file is included which provisions Elastic APM, Elasticsearch and Kibana accordingly.

To use these, you will need to have a working Docker install and the docker-compose tool. Once these are in place, follow the instructions below.

Barring any local networking issues with Magento > APM, you should now see APM data. For the MySQL traces (if you enabled these) you can click on these and see which query was being performed.

Thanks

Elastic APM: PHP Agent: The integration agent this module depends on.

Holdenovi_Profiler: Really helpful as a reference for a Magento 2 Profiler Driver.

Elastic APM, Laravel Driver: My reference for how to call Elastic APM from PHP.