phase 2
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?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,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
$issue = $this->findOrCreateIssue($project, $data, $fingerprint, $now);
|
||||
|
||||
$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,
|
||||
]);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
private function findOrCreateIssue(
|
||||
Project $project,
|
||||
array $data,
|
||||
string $fingerprint,
|
||||
Carbon $now,
|
||||
): Issue {
|
||||
$issue = Issue::where('project_id', $project->id)
|
||||
->where('fingerprint', $fingerprint)
|
||||
->first();
|
||||
|
||||
if ($issue) {
|
||||
$this->updateExistingIssue($issue, $data, $now);
|
||||
|
||||
return $issue;
|
||||
}
|
||||
|
||||
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,
|
||||
'metadata' => array_filter([
|
||||
'platform' => $data['platform'],
|
||||
'sdk' => $data['sdk'],
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
private function updateExistingIssue(Issue $issue, array $data, Carbon $now): void
|
||||
{
|
||||
$wasResolved = $issue->status === 'resolved';
|
||||
|
||||
$issue->increment('times_seen');
|
||||
$issue->update(['last_seen_at' => $now]);
|
||||
|
||||
if ($wasResolved) {
|
||||
$issue->markRegression();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user