Objective
- To provide instruction on adding Application Performance Monitoring (APM) to a Laravel application
- APM can be easily integrated with Laravel applications using the Elastic APM for Laravel package
User
- Solution Delivery Developers
Procedure
First steps
Add to your local .env first
ELASTIC_APM_ENABLED=false
Composer dependency requirements
- Guzzle Adapter:
- Recommend to use:
composer require php-http/guzzle7-adapter
- This will prevent some post install Composer errors:
composer require arkaitzgarro/elastic-apm-laravel
Update AppServiceProvider
Use the below code as an example to add to the register()
method in the AppServiceProvider to disable certificate validation (we use a self-signed certificate for now):
use AG\ElasticApmLaravel\AgentBuilder;
…
public function register () {
...
$this->app->bind(AgentBuilder::class, function () {
$builder = new AgentBuilder();
$builder->withHttpClient(new \Http\Adapter\Guzzle7\Client(new \GuzzleHttp\Client(['verify' => false])));
return $builder;
});
Add to .env.testing
ELASTIC_APM_ENABLED=false
Optional steps
Local testing .env file:
ELASTIC_APM_ENABLED=true
ELASTIC_APM_SERVER_URL=https://logstash-test.mcs.miamioh.edu:5523
ELASTIC_APM_SECRET_TOKEN=(to be shared later)
ELASTIC_APM_SERVICE_NAME="Your APPNAME"
APM_USEROUTEURI=true
APM_IGNORE_PATTERNS="/_debugbar/"
Add distributed tracing
You need to add the tracing ID to the HTTP header, when making the request to a WebService. If you have access to the HTTP Client then it can be as simple as something like this.
$runner->withPreRequestHook(function (RequestInterface $request) {
return ApmAgent::addTraceParentHeader($request);
});
Add additional spans
use AG\ElasticApmLaravel\Facades\ApmCollector;
ApmCollector::startMeasure('my-custom-span', 'custom', 'measure', 'My custom span');
// do something amazing
ApmCollector::stopMeasure('my-custom-span');