Installation:
composer require sentry/sentry-symfony
Add to config/bundles.php:
return [
// ...
Sentry\Symfony\SentryBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console config:dump-reference sentry
Update .env with your DSN (e.g., SENTRY_DSN=https://examplePublicKey@...).
Minimal config/packages/sentry.yaml:
sentry:
dsn: '%env(SENTRY_DSN)%'
options:
traces_sample_rate: 1.0
First Use Case: Trigger an error in a controller to verify integration:
use Symfony\Component\HttpFoundation\Response;
public function testSentry(): Response
{
throw new \RuntimeException('Test error for Sentry');
return new Response('This will not be reached');
}
Check Sentry.io dashboard for the captured event.
Error Handling:
ErrorListener. No manual wrapping needed.use Sentry\Symfony\Facades\Sentry;
Sentry\configureScope(function ($scope) {
$scope->setTag('user', 'admin');
$scope->setExtra('custom_data', ['key' => 'value']);
});
Sentry\captureMessage('This is a manual message');
Sentry\captureException(new \LogicException('Custom exception'));
Request Tracking:
sentry.yaml:
sentry:
options:
send_default_pii: true # For sensitive data (use cautiously)
Sentry\configureScope(function ($scope) use ($request) {
$scope->setUser([
'id' => $request->get('user_id'),
'email' => $request->get('email'),
]);
});
Performance Monitoring:
sentry.yaml:
sentry:
options:
traces_sample_rate: 0.1 # Sample 10% of transactions
$transaction = Sentry\startTransaction('api.endpoint', 'endpoint');
try {
// Business logic
$transaction->setStatus('ok');
} catch (\Exception $e) {
$transaction->setStatus('internal_error');
Sentry\captureException($e);
} finally {
$transaction->finish();
}
Middleware Integration:
Sentry\Symfony\EventListener\ErrorListener to customize error handling:
// config/services.yaml
Sentry\Symfony\EventListener\ErrorListener:
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
DSN Configuration:
SENTRY_DSN is missing or malformed..env and use SENTRY_DSN_VALIDATION=true to throw exceptions on misconfiguration.var/log/dev.log) for transport errors.Sensitive Data:
sentry.yaml:
sentry:
options:
before_send: [App\Service\SentryDataSanitizer]
Implement before_send callback to filter data:
public function __invoke($event) {
unset($event['request']['_token']);
return $event;
}
Performance Overhead:
traces_sample_rate (e.g., 0.1 for 10% sampling) and avoid manual transactions in hot paths.Symfony Cache Conflicts:
sentry.yaml uses a dedicated HTTP client:
sentry:
http_client:
max_size: 100 # Adjust as needed
Local Testing:
SENTRY_ENVIRONMENT=development to distinguish local events.sentry.yaml:
sentry:
options:
debug: true
Event Inspection:
$event = Sentry\configureScope(function () {
return Sentry\lastEventId(); // Get the last event ID
});
file_put_contents('sentry_event.json', json_encode($event, JSON_PRETTY_PRINT));
Transport Issues:
curl -X POST https://your-dsn.sentry.io/api/<project-id>/envelope/ -d @sentry_event.json
Custom Integrations:
Sentry\State\Scope to add app-specific context:
use Sentry\State\Scope;
Sentry\configureScope(function (Scope $scope) {
$scope->setContext('app', [
'version' => '1.0.0',
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
]);
});
Event Filters:
use Sentry\Symfony\EventListener\ErrorListener;
class CustomErrorListener extends ErrorListener
{
protected function isCapturable(\Throwable $exception): bool
{
return !($exception instanceof \App\Exception\IgnoredException);
}
}
Register in services.yaml:
App\EventListener\CustomErrorListener:
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Breadcrumbs:
Sentry\addBreadcrumb([
'category' => 'navigation',
'message' => 'User clicked "Submit"',
'level' => 'info',
]);
How can I help you explore Laravel packages today?