This commit is contained in:
Stefanescu Mihai
2026-03-19 19:32:46 +02:00
parent 2673552733
commit 528a55cce7
9 changed files with 484 additions and 11 deletions
+150
View File
@@ -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;
}
}
+85
View File
@@ -0,0 +1,85 @@
<?php
namespace App\Services;
class Fingerprinter
{
/**
* Compute a fingerprint hash for grouping events into issues.
*
* If the payload contains a custom `fingerprint` array (Sentry SDK standard),
* that takes precedence. Otherwise we derive one from exception type, a
* sanitized message, and the top stacktrace frames.
*/
public function compute(array $payload): string
{
if (! empty($payload['fingerprint']) && is_array($payload['fingerprint'])) {
return $this->hash($payload['fingerprint']);
}
$parts = [];
$exception = $this->extractPrimaryException($payload);
if ($exception) {
$parts[] = $exception['type'] ?? '';
$parts[] = $this->sanitizeMessage($exception['value'] ?? '');
$parts = array_merge($parts, $this->topFrames($exception, 5));
} else {
$parts[] = $payload['message'] ?? $payload['logentry']['message'] ?? 'unknown';
}
return $this->hash($parts);
}
public function extractPrimaryException(array $payload): ?array
{
$exceptions = $payload['exception']['values'] ?? [];
return end($exceptions) ?: null;
}
/**
* Strip variable parts (numbers, UUIDs, hex strings) so that
* "User 42 not found" and "User 99 not found" group together.
*/
private function sanitizeMessage(string $message): string
{
$message = preg_replace(
'/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i',
'{uuid}',
$message,
);
$message = preg_replace('/\b[0-9a-f]{24,}\b/i', '{hash}', $message);
$message = preg_replace('/\b\d+\b/', '{n}', $message);
return $message;
}
/**
* Return file+function strings from the top N stacktrace frames.
*/
private function topFrames(array $exception, int $limit): array
{
$frames = $exception['stacktrace']['frames'] ?? [];
$frames = array_reverse($frames);
$frames = array_slice($frames, 0, $limit);
$parts = [];
foreach ($frames as $frame) {
$file = $frame['filename'] ?? $frame['abs_path'] ?? '';
$function = $frame['function'] ?? '';
$parts[] = "{$file}:{$function}";
}
return $parts;
}
private function hash(array $parts): string
{
return md5(implode("\x00", $parts));
}
}
+111
View File
@@ -0,0 +1,111 @@
<?php
namespace App\Services;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
class PayloadNormalizer
{
/**
* Normalize a raw Sentry event payload into the canonical shape
* expected by the EventProcessor.
*/
public function normalize(array $raw): array
{
return [
'event_id' => $raw['event_id'] ?? Str::uuid()->toString(),
'level' => $this->normalizeLevel($raw['level'] ?? 'error'),
'message' => $this->extractMessage($raw),
'platform' => $raw['platform'] ?? null,
'timestamp' => $this->normalizeTimestamp($raw['timestamp'] ?? null),
'server_name' => $raw['server_name'] ?? null,
'release' => $raw['release'] ?? null,
'environment' => $raw['environment'] ?? null,
'exception' => $raw['exception'] ?? ['values' => []],
'breadcrumbs' => $raw['breadcrumbs'] ?? null,
'contexts' => $raw['contexts'] ?? null,
'tags' => $this->normalizeTags($raw['tags'] ?? null),
'extra' => $raw['extra'] ?? null,
'user_context' => $raw['user'] ?? null,
'request_context' => $raw['request'] ?? null,
'sdk' => $raw['sdk'] ?? null,
'fingerprint' => $raw['fingerprint'] ?? null,
];
}
private function normalizeLevel(string $level): string
{
$map = [
'fatal' => 'fatal',
'critical' => 'fatal',
'error' => 'error',
'warning' => 'warning',
'warn' => 'warning',
'info' => 'info',
'log' => 'info',
'debug' => 'debug',
];
return $map[strtolower($level)] ?? 'error';
}
private function extractMessage(array $raw): ?string
{
if (! empty($raw['message'])) {
return Str::limit($raw['message'], 2000);
}
if (! empty($raw['logentry']['message'])) {
return Str::limit($raw['logentry']['message'], 2000);
}
$exceptions = $raw['exception']['values'] ?? [];
$last = end($exceptions);
if ($last) {
$type = $last['type'] ?? 'Error';
$value = $last['value'] ?? '';
return Str::limit("{$type}: {$value}", 2000);
}
return null;
}
private function normalizeTimestamp(mixed $timestamp): Carbon
{
if ($timestamp === null) {
return Carbon::now();
}
if (is_numeric($timestamp)) {
return Carbon::createFromTimestamp((float) $timestamp);
}
return Carbon::parse($timestamp);
}
/**
* Tags can arrive as key-value pairs or as an array of {key, value} objects.
* Normalize to a plain associative array.
*/
private function normalizeTags(mixed $tags): ?array
{
if ($tags === null) {
return null;
}
if (array_is_list($tags)) {
$normalized = [];
foreach ($tags as $tag) {
if (isset($tag['key'], $tag['value'])) {
$normalized[$tag['key']] = $tag['value'];
}
}
return $normalized ?: null;
}
return $tags;
}
}