Installation:
composer require sentry/sentry-symfony
Add to config/packages/sentry.yaml (auto-generated on first run):
sentry:
dsn: 'https://<key>@<project>.ingest.sentry.io/<project_id>'
options:
traces_sample_rate: 1.0
First Use Case:
use Sentry\SentrySdk;
public function testSentry()
{
SentrySdk::captureMessage('This is a test event');
return new Response('Event sent!');
}
Where to Look First:
config/packages/sentry.yaml (auto-generated config)src/Sentry/ExceptionListener.php (kernel exception listener)Exception Handling:
ExceptionListener:
// config/services.yaml
Sentry\Symfony\ExceptionListener:
tags: [kernel.event_listener]
arguments:
$level: 'error' // Override default 'critical'
Manual Event Capture:
SentrySdk::captureException($exception)SentrySdk::captureMessage('User logged in', ['user_id' => 123])SentrySdk::startTransaction('checkout.process_payment', 'checkout');
try {
// Payment logic
} finally {
SentrySdk::finishTransaction();
}
Middleware Integration:
use Sentry\State\Scope;
public function onKernelRequest(RequestEvent $event)
{
$scope = SentrySdk::getScope();
$scope->setUser(['id' => $event->getRequest()->get('user_id')]);
}
Performance Monitoring:
SentrySdk::startSpan('slow_operation', ['op' => 'database_query']);
// ... execute query ...
SentrySdk::finishSpan();
sentry-laravel/sentry alongside this package for Laravel-specific features (e.g., Eloquent models)..env:
# config/packages/sentry.yaml
sentry:
dsn: '%env(SENTRY_DSN)%'
config/packages/sentry.yaml:
sentry:
options:
debug: false
DSN Leaks:
config/packages/sentry.yaml to version control if it contains a real DSN..env or environment variables for production DSNs.Performance Overhead:
# config/packages/sentry.yaml
sentry:
enabled: '%kernel.debug% ? false : true%' # Disable in debug mode
traces_sample_rate in production to avoid high cardinality.Context Overwriting:
SentrySdk::configureScope()—it replaces the entire scope. Use SentrySdk::getScope()->setTag() for incremental updates.Symfony Cache Warmup:
sentry.yaml:
php bin/console cache:clear
Local Testing:
SentrySdk::captureException() with a dummy exception to verify setup:
throw new \RuntimeException('Test exception');
Logging:
config/packages/sentry.yaml:
sentry:
options:
debug: true
var/log/dev.log for Sentry-related entries.Common Errors:
enabled: true in config and network/firewall rules.SentrySdk::init() is called only once (handled automatically in Symfony).Custom Event Processors:
use Sentry\Event\EventInterface;
use Sentry\EventProcessorInterface;
class CustomProcessor implements EventProcessorInterface
{
public function __invoke(EventInterface $event): EventInterface
{
if ($event->getLevel() === 'debug') {
return null; // Drop debug events
}
return $event;
}
}
Register in config/services.yaml:
Sentry\Symfony\EventProcessor:
arguments:
$processors: ['@custom_processor']
Breadcrumbs:
SentrySdk::addBreadcrumb([
'category' => 'navigation',
'message' => 'User clicked "Submit"',
'level' => 'info',
]);
Release Tracking:
sentry:
options:
release: '%env(APP_VERSION)%'
SentrySdk::setRelease('my-app@1.0.0');
Ignoring Exceptions:
HttpException):
sentry:
options:
ignore_exceptions: [Symfony\Component\HttpKernel\Exception\HttpException]
How can I help you explore Laravel packages today?