bethinkpl / elastic-apm-laravel by macbre
forked from philkra/elastic-apm-laravel

A package to integrate Elastic APM 7.x into Laravel
Package Data
Maintainer Username: macbre
Maintainer Contact: (Philip Krauss)
Package Create Date: 2019-12-30
Package Last Update: 2021-02-26
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2022-08-03 03:11:25
Package Statistics
Total Downloads: 23,759
Monthly Downloads: 281
Daily Downloads: 15
Total Stars: 1
Total Watchers: 1
Total Forks: 1
Total Open Issues: 0

PHP Elastic APM for Laravel & Lumen

Laravel package of the library, automatically handling transactions and errors/exceptions. If using Illuminate\Support\Facades\Auth the user Id added to the context. Tested with Laravel 5.7.* and the philkra/elastic-apm-php-agent version 7.x.


composer require bethinkpl/elastic-apm-laravel

Additional features

  • X-Requested-By HTTP request header value is set as labels.requested_by APM transaction field (end-user-ajax value is used when X-Requested-With: XMLHttpRequest is set)
  • tracking of HTTP requests performed using GuzzleHttp library. Simply add the following middleware to your Guzzle client:

use PhilKra\ElasticApmLaravel\Providers\ElasticApmServiceProvider;
use GuzzleHttp\HandlerStack;

$handler = HandlerStack::create();

// create your client with 'handler' option passed



Register as (e.g.) global middleware to be called with every request.

Register the middleware in app/Http/Kernel.php

protected $middleware = [
    // ... more middleware


In bootstrap/app.php register PhilKra\ElasticApmLaravel\Middleware\RecordTransaction::class as middleware:


Service Provider


No need to register service provider manually. It is registered automatically by package discovery.


In bootstrap/app.php register \PhilKra\ElasticApmLaravel\Providers\ElasticApmServiceProvider::class as service provider:




A Transaction object is made available via the dependency container and can be used to start a new span at any point in the application. The Span will automatically add itself to the Transaction when it is ended.

// Use any normal Laravel method of resolving the dependency
$transaction = app(\PhilKra\ElasticApmLaravel\Apm\Transaction::class);

$span = $transaction->startNewSpan('My Span', 'app.component_name');

// do some stuff




Error/Exception Handling


In app/Exceptions/Handler, add the following to the report method:


Make sure to import the facade at the top of your file:

use ElasticApm;


not tested yet.

Agent Configuration


The following environment variables are supported in the default configuration:

| Variable | Description | |-------------------|-------------| |APM_ACTIVE | true or false defaults to true. If false, the agent will collect, but not send, transaction data. | |APM_APPNAME | Name of the app as it will appear in APM. | |APM_APPVERSION | Version of the app as it will appear in APM. | |APM_SERVERURL | URL to the APM intake service. | |APM_SECRETTOKEN | Secret token, if required. | |APM_APIVERSION | APM API version, defaults to v1 (only v1 is supported at this time). | |APM_USEROUTEURI | true or false defaults to false. The default behavior is to record the URL as sent in the request. This can result in excessive unique entries in APM. Set to true to have the agent use the route URL instead. | |APM_NORMALIZEURI | true or false defaults to false. If enabled, removes variable parts from URI when generating transaction name (e.g. GET /foo/bar/123 is reported as GET /foo/bar/N). APM_USEROUTEURI needs to be enabled. | |APM_QUERYLOG | true or false defaults to 'true'. Set to false to completely disable query logging, or to auto if you would like to use the threshold feature. | |APM_THRESHOLD | Query threshold in milliseconds, defaults to 200. If a query takes longer then 200ms, we enable the query log. Make sure you set APM_QUERYLOG=auto. | |APM_BACKTRACEDEPTH | Defaults to 25. Depth of backtrace in query span. | |APM_RENDERSOURCE | Defaults to true. Include source code in query span. | |APM_HTTPLOG | Defaults to true. Will record HTTP requests performed via GuzzleHttp. |

You may also publish the elastic-apm.php configuration file to change additional settings:

php artisan vendor:publish --tag=config

Once published, open the config/elastic-apm.php file and review the various settings.

Laravel Test Setup

Laravel provides classes to support running unit and feature tests with PHPUnit. In most cases, you will want to explicitly disable APM during testing since it is enabled by default. Refer to the Laravel documentation for more information (

Because the APM agent checks it's active status using a strict boolean type, you must ensure your APM_ACTIVE value is a boolean false rather than simply a falsy value. The best way to accomplish this is to create an .env.testing file and include APM_ACTIVE=false, along with any other environment settings required for your tests. This file should be safe to include in your SCM.