Sentry Integration
Sentry Architecture
Application → Sentry SDK → Sentry Server → Alert
│ │
│ ▼
└─────── Error Event ◀── Dashboard
Magento Sentry Setup
// composer.json
require {
"sentry/sentry-php": "^4.0"
}
// app/bootstrap.php
use Sentry\SentrySdk;
use Sentry\State\Hub;
SentrySdk::init([
'dsn' => 'https://key@sentry.example.com/project-id',
'environment' => getenv('APP_ENV'),
'release' => MAGENTO_VERSION,
'traces_sample_rate' => 0.1,
'before_send' => function (\Sentry\Event $event) {
// Filter sensitive data
$event->setExtras(array_filter($event->getExtras(), function ($key) {
return !in_array($key, ['password', 'token', 'secret']);
}, ARRAY_FILTER_USE_KEY));
return $event;
}
]);
// Capture exceptions
try {
$this->orderService->process($order);
} catch (Exception $e) {
\Sentry\SentrySdk::captureException($e);
throw $e;
}
Breadcrumbs
// Add context breadcrumbs
\Sentry\SentrySdk::configureScope(function ($scope) {
$scope->setTag('order_id', $orderId);
$scope->setUser([
'id' => $customerId,
'email' => $customerEmail
]);
});
// Add breadcrumb
\Sentry\addBreadcrumb(new \Sentry\Breadcrumb(
\Sentry\Breadcrumb::LEVEL_INFO,
\Sentry\Breadcrumb::TYPE_DEFAULT,
'payment',
'Payment initiated',
['amount' => $amount]
));
Error Grouping
Grouping Strategy
Same Error Group:
- Same exception class
- Same stack trace
- Same error message pattern
Different Error Groups:
- Different exception class
- Different stack trace
- Different message template
Custom Grouping
// Set fingerprint for custom grouping
\Sentry\SentrySdk::configureScope(function ($scope) use ($e) {
$scope->setFingerprint([
$e->getMessage(),
$e->getFile(),
$e->getLine()
]);
});
// Group by exception type
$scope->setFingerprint([
'{{ default }}',
$e->getClass()
]);
Error Deduplication
// Sentry deduplicates by default
// Same error in same version = same issue
// Custom deduplication
$errorHash = md5($e->getMessage() . $e->getFile() . $e->getLine());
if (!$this->cache->load('error_' . $errorHash)) {
\Sentry\SentrySdk::captureException($e);
$this->cache->save('1', 'error_' . $errorHash, [], 300);
}
Error Alerting
Alert Rules
Rule Type | Condition | Action
─────────────────|────────────────────────|──────────────
New Issue | First occurrence | Email + Slack
Regression | Fixed issue reappears | Email + Slack
Frequency Spike | Error rate > 5x | PagerDuty
Total Volume | > 100 errors/hour | Email
Sentry Alert Config
# .sentry/alerts.yml
alerts:
- name: "High Error Rate"
conditions:
- type: error_rate
value: 5
period: 1h
actions:
- type: email
targets: ["ops@example.com"]
- type: slack
channel: "#alerts"
- name: "New Critical Error"
conditions:
- type: new_issue
- type: level_equals
value: "error"
actions:
- type: pagerduty
service: "magento"
Alert Notification
// Add alert context to errors
$event->setTag('severity', 'critical');
$event->setTag('team', 'payments');
$event->setTag('service', 'order-service');
// Set user impact
$event->setUser([
'id' => $customerId,
'email' => $customerEmail,
'ip_address' => $request->getClientIp()
]);
Error Budgets
Error Budget Concept
SLA: 99.9% availability
Error Budget: 0.1% = 43.8 minutes/month
If errors consume budget:
- 50% consumed: Warning
- 80% consumed: Feature freeze
- 100% consumed: Reliability sprint
Error Budget Calculation
// Calculate monthly error budget
$slaTarget = 99.9;
$errorBudget = 100 - $slaTarget; // 0.1%
$monthMinutes = 43200; // 30 days
$budgetMinutes = $monthMinutes * ($errorBudget / 100); // 43.8 minutes
// Track consumption
$actualDowntime = $this->getDowntimeMinutes();
$remainingBudget = $budgetMinutes - $actualDowntime;
$consumptionRate = ($actualDowntime / $budgetMinutes) * 100;
// Alert based on consumption
if ($consumptionRate > 80) {
$this->alertService->notify('Error budget 80% consumed');
}
Error Budget Policy
Budget Remaining | Action
─────────────────|──────────────────────
> 50% | Normal feature development
25-50% | Increase testing, review deployments
10-25% | Feature freeze, focus on reliability
< 10% | Hard freeze, reliability sprint only
0% | All hands on reliability
Quiz
1. What is Sentry used for?
2. What is an error budget?
3. When should feature development freeze?
Flashcards
Question
Sentry purpose?
Click to reveal answer
Answer
Error tracking, grouping, alerting, and monitoring
Question
Error budget concept?
Click to reveal answer
Answer
Allowed downtime percentage before SLA breach
Question
Budget 80% consumed action?
Click to reveal answer
Answer
Feature freeze, focus on reliability
Question
Error deduplication?
Click to reveal answer
Answer
Group same errors by stack trace and message pattern
Revision Notes
Key Takeaways
- 1. Sentry provides real-time error tracking with grouping and alerting
- 2. Error grouping uses stack trace and message patterns
- 3. Alert on new issues, regressions, and frequency spikes
- 4. Error budgets guide feature vs reliability trade-offs
- 5. 80% budget consumption triggers feature freeze
Interview Tips
- • Explain error grouping strategies and deduplication
- • Discuss error budget policies and their business value
- • Describe Sentry integration and alert configuration
Cheat Sheet
Error Tracking:
Sentry: Real-time error monitoring
Grouping: Stack trace + message pattern
Deduplication: Same error = same issue
Alerting:
New issue: Email + Slack
Regression: Email + Slack
Frequency spike: PagerDuty
Error Budgets:
SLA 99.9% = 0.1% budget = 43.8 min/month
>50%: Normal development
25-50%: Increase testing
10-25%: Feature freeze
<10%: Reliability sprint