druidvav/sentry-extension-bundle
Symfony bundle that integrates Sentry error tracking into your application. It provides a configurable extension to capture exceptions, report errors, and monitor performance with minimal setup, helping you keep production issues visible.
Requirements & Installation
This is a Symfony bundle (not a Laravel package) — it ships a DI extension and kernel event listeners. As of v1.3.0 the extension class extends Symfony's public Symfony\Component\DependencyInjection\Extension\Extension, so the bundle boots cleanly on modern Symfony (4.4 / 5.x / 6.x) without depending on the removed HttpKernel\DependencyInjection\Extension import.
composer require druidvav/sentry-extension-bundle
Register in config/bundles.php:
return [
// ...
Druidvav\SentryExtensionBundle\DruidvavSentryExtensionBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console druidvav:sentry-extension:install
Edit config/packages/druidvav_sentry_extension.yaml to include your Sentry DSN and project-specific settings.
Smoke Test the Wiring Before writing any capture code, confirm the container loads the extension (this is the single fastest way to catch a bad upgrade or a malformed YAML tree):
php bin/console debug:config druidvav_sentry_extension
First Use Case Trigger a test event in a controller:
use Druidvav\SentryExtensionBundle\Sentry\SentryClient;
public function testSentry(SentryClient $sentry)
{
$sentry->captureMessage('Test event from my app');
return 'Event sent to Sentry';
}
config/packages/druidvav_sentry_extension.yaml (main config)src/DependencyInjection/DruidvavSentryExtensionExtension.php (DI extension — changed in v1.3.0)src/Sentry/SentryClient.php (core client class)src/EventListener/SentryListener.php (default event listeners)Event Capture
// Basic message
$sentry->captureMessage('User login failed');
// Exception handling
try {
// Risky operation
} catch (\Exception $e) {
$sentry->captureException($e);
}
// Structured data
$sentry->captureMessage('Payment processed', [
'amount' => $order->amount,
'user_id' => auth()->id(),
'status' => 'completed'
]);
Middleware / Kernel Request Listener Integration
// src/EventSubscriber/TrackUserActivitySubscriber.php
public function onKernelResponse(ResponseEvent $event)
{
$request = $event->getRequest();
$this->sentry->captureMessage('User accessed: ' . $request->getPathInfo(), [
'user_id' => $this->tokenStorage->getToken()?->getUserIdentifier(),
'ip' => $request->getClientIp()
]);
}
Command-Line Events
// In a console command
public function execute(InputInterface $input, OutputInterface $output)
{
try {
// Command logic
} catch (\Exception $e) {
$this->sentry->captureException($e, [
'command' => $this->getName(),
'arguments' => $input->getArguments()
]);
}
}
Symfony Event Dispatcher Bind to kernel events for automatic tracking:
# config/packages/druidvav_sentry_extension.yaml
listeners:
kernel.exception: ['Druidvav\SentryExtensionBundle\EventListener\SentryListener::onKernelException']
Service Autowiring
The SentryClient is registered as a container service by the bundle extension, so prefer constructor injection over manually building it:
public function __construct(private SentryClient $sentry) {}
Custom Tags/Context Attach metadata globally via config:
tags:
environment: 'production'
application: 'my-app'
Symfony Version Compatibility (v1.3.0)
DependencyInjection\Extension\Extension base class to drop the removed HttpKernel import. Practically: before v1.3.0 the bundle could fail at container compile-time on Symfony 5+/6 with a Class "Symfony\Component\HttpKernel\DependencyInjection\Extension" not found fatal — the "extension behavior" was fine, only the parent class was gone.druidvav_sentry_extension.yaml files keep working unchanged.php bin/console cache:clear and php bin/console debug:config druidvav_sentry_extension — if the extension didn't register, debug:config errors immediately instead of failing on the first request.DSN Configuration
config/packages/druidvav_sentry_extension.yaml matches your Sentry project.debug: true and check the container-level error first; a missing DSN usually surfaces as "no events delivered," not as an exception.Environment-Specific Config
# config/packages/dev/druidvav_sentry_extension.yaml
dsn: '%env(SENTRY_DSN_DEV)%'
debug: true
Event Deduplication
fingerprint to avoid duplicate events:
$sentry->captureMessage('Failed login', [], [
'fingerprint' => ['{{ default_user }}', '{{ input_ip }}']
]);
Verify the Extension Loaded (regression test for the v1.3.0 change)
php bin/console debug:config druidvav_sentry_extension
php bin/console debug:container druidvav_sentry_extension
If these fail while other commands work, the bundle registration in config/bundles.php is the culprit, not the Sentry code.
Log Events Locally Enable debug mode to log events before sending:
debug: true
Event Listener Debugging Temporarily disable listeners:
listeners:
kernel.exception: false
Custom Event Types
Extend SentryClient to add domain-specific methods:
// src/Sentry/CustomSentryClient.php
class CustomSentryClient extends \Druidvav\SentryExtensionBundle\Sentry\SentryClient
{
public function capturePaymentFailure($payment, \Exception $e)
{
$this->captureException($e, [
'payment_id' => $payment->id,
'amount' => $payment->amount,
'gateway' => $payment->gateway
]);
}
}
Alias the bundle's service ID to your class in config/services.yaml so autowiring picks it up.
Event Transformers Override event data before sending:
transformers:
exception: 'App\Sentry\ExceptionTransformer'
Async Processing For performance, queue events:
// In a Messenger handler
public function __invoke(SentryEventMessage $message)
{
$this->sentry->captureMessage($message->getText(), [], ['queue' => true]);
}
Breadcrumbs Enable HTTP breadcrumbs for request tracking:
options:
send_default_pii: true
traces_sample_rate: 1.0
Release Tracking Auto-set release version:
release: '{{ env("APP_VERSION") }}'
Ignored Exceptions Exclude specific exceptions from being captured:
ignored_exceptions:
- 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
How can I help you explore Laravel packages today?