Files

194 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2026-03-19 19:32:46 +02:00
<?php
namespace App\Services;
use App\Models\Event;
use App\Models\Issue;
use App\Models\Project;
use Illuminate\Support\Carbon;
class EventProcessor
{
public function __construct(
private Fingerprinter $fingerprinter,
private PayloadNormalizer $normalizer,
2026-03-19 19:47:06 +02:00
private NotificationDispatcher $notificationDispatcher,
2026-03-19 19:32:46 +02:00
) {}
/**
* Process a raw Sentry event payload for the given project.
* Returns the created Event model.
*/
public function process(Project $project, array $rawPayload): Event
{
$data = $this->normalizer->normalize($rawPayload);
$fingerprint = $this->fingerprinter->compute($rawPayload);
$now = Carbon::now();
2026-03-19 19:47:06 +02:00
$isNew = false;
$isRegression = false;
$issue = $this->findOrCreateIssue($project, $data, $fingerprint, $now, $isNew, $isRegression);
2026-03-19 19:32:46 +02:00
$event = Event::create([
'issue_id' => $issue->id,
'project_id' => $project->id,
'event_id' => $data['event_id'],
'level' => $data['level'],
'message' => $data['message'],
'platform' => $data['platform'],
'timestamp' => $data['timestamp'],
'server_name' => $data['server_name'],
'release' => $data['release'],
'environment' => $data['environment'],
'exception' => $data['exception'],
'breadcrumbs' => $data['breadcrumbs'],
'contexts' => $data['contexts'],
'tags' => $data['tags'],
'extra' => $data['extra'],
'user_context' => $data['user_context'],
'request_context' => $data['request_context'],
'sdk' => $data['sdk'],
'raw_payload' => $rawPayload,
]);
2026-03-19 19:47:06 +02:00
$this->dispatchNotifications($project, $issue, $isNew, $isRegression);
2026-03-19 19:32:46 +02:00
return $event;
}
private function findOrCreateIssue(
Project $project,
array $data,
string $fingerprint,
Carbon $now,
2026-03-19 19:47:06 +02:00
bool &$isNew,
bool &$isRegression,
2026-03-19 19:32:46 +02:00
): Issue {
$issue = Issue::where('project_id', $project->id)
->where('fingerprint', $fingerprint)
->first();
if ($issue) {
2026-03-19 19:47:06 +02:00
$isRegression = $this->updateExistingIssue($issue, $data, $now);
$isNew = false;
2026-03-19 19:32:46 +02:00
return $issue;
}
2026-03-19 19:47:06 +02:00
$isNew = true;
$isRegression = false;
2026-03-19 19:32:46 +02:00
return $this->createNewIssue($project, $data, $fingerprint, $now);
}
private function createNewIssue(
Project $project,
array $data,
string $fingerprint,
Carbon $now,
): Issue {
$exception = $this->fingerprinter->extractPrimaryException(
['exception' => $data['exception']],
);
$title = $this->buildTitle($exception, $data['message']);
$culprit = $this->buildCulprit($exception);
return Issue::create([
'project_id' => $project->id,
'fingerprint' => $fingerprint,
'title' => $title,
'culprit' => $culprit,
'level' => $data['level'],
'status' => 'unresolved',
'first_seen_at' => $now,
'last_seen_at' => $now,
'times_seen' => 1,
'is_regression' => false,
2026-03-21 15:25:05 +02:00
'first_release' => $data['release'],
2026-03-19 19:32:46 +02:00
'metadata' => array_filter([
'platform' => $data['platform'],
'sdk' => $data['sdk'],
]),
]);
}
2026-03-19 19:47:06 +02:00
private function updateExistingIssue(Issue $issue, array $data, Carbon $now): bool
2026-03-19 19:32:46 +02:00
{
$wasResolved = $issue->status === 'resolved';
2026-03-21 15:25:05 +02:00
$currentRelease = $data['release'];
$resolvedInRelease = $issue->resolved_in_release;
2026-03-19 19:32:46 +02:00
$issue->increment('times_seen');
$issue->update(['last_seen_at' => $now]);
2026-03-21 15:25:05 +02:00
// Check for regression: issue was resolved, and now appears again
2026-03-19 19:32:46 +02:00
if ($wasResolved) {
2026-03-21 15:25:05 +02:00
// If we have release tracking and the issue was resolved in a release,
// only mark as regression if the current release is different (assumed newer)
if ($resolvedInRelease && $currentRelease && $currentRelease === $resolvedInRelease) {
// Same release as resolved - don't mark as regression
return false;
}
2026-03-19 19:32:46 +02:00
$issue->markRegression();
2026-03-19 19:47:06 +02:00
return true;
2026-03-19 19:32:46 +02:00
}
2026-03-19 19:47:06 +02:00
return false;
2026-03-19 19:32:46 +02:00
}
private function buildTitle(?array $exception, ?string $message): string
{
if ($exception) {
$type = $exception['type'] ?? 'Error';
$value = $exception['value'] ?? '';
return str($value)
->whenEmpty(fn () => str($type))
->prepend("{$type}: ")
->limit(255)
->toString();
}
return str($message ?? 'Unknown Error')->limit(255)->toString();
}
private function buildCulprit(?array $exception): ?string
{
if (! $exception) {
return null;
}
$frames = $exception['stacktrace']['frames'] ?? [];
$last = end($frames);
if (! $last) {
return null;
}
$file = $last['filename'] ?? $last['abs_path'] ?? '?';
$function = $last['function'] ?? null;
return $function ? "{$file} in {$function}" : $file;
}
2026-03-19 19:47:06 +02:00
private function dispatchNotifications(
Project $project,
Issue $issue,
bool $isNew,
bool $isRegression,
): void {
if ($isNew) {
$this->notificationDispatcher->onNewIssue($project, $issue);
}
if ($isRegression) {
$this->notificationDispatcher->onRegression($project, $issue);
}
$this->notificationDispatcher->checkSpike($project, $issue);
}
2026-03-19 19:32:46 +02:00
}