felix-lessoer/cakephp-elasticapm

There is no license information available for the latest version (dev-master) of this package.

Monitor your cakephp project with elastic APM

dev-master 2020-03-17 20:13 UTC

This package is not auto-updated.

Last update: 2024-04-11 13:28:05 UTC


README

For now this plugin supports to collect common events from cake php. Tested in cake php v3.8 . This plugin is based on the Elastic APM php agent

Installation

Just use composer to setup the plugin

composer require felix-lessoer/cakephp-elasticapm

Configuration

Include this snippet in config/app.php

'ElasticApm' => [
        'enabled' => true,
        'rumEnabled' => true,
        'appName' => '<app name>',
        'appVersion' => "1.0",
        'serverUrl' => '<apm server url>',
        'secretToken' => '<apm server secret token>',
        'environment' => "development"
    ]

enabled: If false the collection gets deactivated

Include this snippet in src/Application.php in your function bootstrap()

if (Configure::read('ElasticApm.enabled')) {
    $this->addPlugin(ElasticApmPlugin::class);
}

Add RUM agent

The RUM agent is optional, but provides better insights into your page speed.

Download the agent here and add it into your webroot/js dirctory

Add this to into the <head> area of your page <?php echo $this->Html->script('elastic-apm-rum.umd.min.js'); ?>

Add this snipped directly at the beginning of <body>:

  <script>
    var parts = window.location.pathname.split('?');
    var pageName = window.location.pathname;
    if (parts.length > 0) {
      pageName = parts[0]
    }
    if (<?php echo Configure::read('ElasticApm.rumEnabled'); ?>) {
      elasticApm.init({
        serviceName: '<?php echo Configure::read('ElasticApm.appName'); ?> RUM',
        serverUrl: '<?php echo Configure::read('ElasticApm.serverUrl'); ?>',
        environment: '<?php echo Configure::read('ElasticApm.environment'); ?>',
        serviceVersion: '<?php echo Configure::read('ElasticApm.appVersion'); ?>',
        pageLoadTraceId: '<?= $traceId ?>',
        pageLoadSpanId: '<?= $spanId ?>',
        pageLoadSampled: true,
        pageLoadTransactionName: pageName,
        breakdownMetrics: true,
        //monitorLongtasks: true,
      });
      //Optional adding user context
      elasticApm.setUserContext({
        id: <userid>,
        username: <username>,
      });
    }
  </script>