Installation:
composer require divlooper/elastic-apm-bundle
Add to bundles.php:
DivLooper\ElasticAPMBundle\DivLooperElasticAPMBundle::class => ['all' => true],
Basic Configuration (config/packages/div_looper_elastic_apm.yaml):
div_looper_elastic_apm:
app_name: 'MyApp'
app_version: '1.0'
elastic_apm_server: 'http://localhost:8200'
secret_token: 'your-secret-token' # Required for APM server auth
First Use Case:
Auto-Instrumentation:
HttpFoundation and HttpClient components.Manual Transaction Spans:
ElasticAPM service to manually start/end spans (e.g., for business logic):
$transaction = $this->get('elastic_apm')->startTransaction('custom_operation');
try {
// Business logic here
$transaction->end();
} catch (\Exception $e) {
$transaction->captureException($e);
$transaction->end();
}
Custom Context Data:
$transaction->setContext('user', ['id' => 123, 'role' => 'admin']);
Error Tracking:
$this->get('elastic_apm')->captureError(new \RuntimeException('Custom error'), [
'custom_field' => 'value'
]);
doctrine.dbal.connection events to track SQL queries:
# config/packages/div_looper_elastic_apm.yaml
div_looper_elastic_apm:
db_instrumentation: true
HttpClient and Guzzle. For Guzzle, ensure the bundle’s middleware is added:
$client = new \GuzzleHttp\Client([
'handler' => \DivLooper\ElasticAPMBundle\Handler\APMHandler::create()
]);
ElasticAPM service in background workers (e.g., Symfony Messenger) to track job execution.Secret Token:
secret_token must match the APM server’s configuration. Omitting or misconfiguring it silently disables APM.parameter_bag.Performance Overhead:
div_looper_elastic_apm:
enabled: '%env(bool:ELASTIC_APM_ENABLED)%'
Deprecated Features:
Database Instrumentation:
ElasticAPM service.Enable Debug Mode:
div_looper_elastic_apm:
debug: true
Logs APM events to Symfony’s dev.log.
Validate Payloads:
Use Elastic APM’s server logs or tools like APM Agent’s curl test to verify data format.
Common Issues:
secret_token, network connectivity, and Symfony’s monolog logs.Custom Payloads:
Override the bundle’s APMClient to modify payloads before sending:
// src/Service/APMClient.php
class CustomAPMClient extends \DivLooper\ElasticAPMBundle\Client\APMClient {
public function sendEvent($event) {
$event['custom_field'] = 'value';
parent::sendEvent($event);
}
}
Bind it in services.yaml:
services:
DivLooper\ElasticAPMBundle\Client\APMClient: '@App\Service\CustomAPMClient'
Event Listeners:
Subscribe to elastic_apm.transaction or elastic_apm.error events to enrich data:
// src/EventListener/APMListener.php
class APMListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'elastic_apm.transaction' => 'onTransaction',
];
}
public function onTransaction(TransactionEvent $event) {
$event->getTransaction()->setContext('custom', ['data' => 'value']);
}
}
Environment-Specific Config:
Use Symfony’s %kernel.environment% to toggle APM in dev/staging:
div_looper_elastic_apm:
enabled: '%kernel.debug%'
How can I help you explore Laravel packages today?