Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactors Sentry module: #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions app/config/migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@
use Cycle\Schema\Generator\Migrations\Strategy\MultipleFilesStrategy;

return [
/**
* Directory to store migration files
*/
'directory' => directory('app') . 'database/Migrations/',

/**
* Table name to store information about migrations status (per database)
*/
'table' => 'migrations',

'strategy' => MultipleFilesStrategy::class,

/**
* When set to true no confirmation will be requested on migration run.
*/
'safe' => true,

'namespace' => 'Database\Migrations',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;

class OrmDefault706076e16879158d6f8d79771cf7e44c extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('sentry_traces')
->addColumn('uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('trace_id', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 32])
->addColumn('public_key', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addColumn('environment', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addColumn('sampled', 'boolean', ['nullable' => false, 'defaultValue' => null])
->addColumn('sample_rate', 'float', ['nullable' => false, 'defaultValue' => null])
->addColumn('transaction', 'string', ['nullable' => true, 'defaultValue' => null, 'size' => 255])
->addColumn('sdk', 'jsonb', ['nullable' => false, 'defaultValue' => null])
->addColumn('language', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addIndex(['trace_id'], ['name' => 'sentry_traces_index_trace_id_666ebc74b7a32', 'unique' => true])
->setPrimaryKeys(['uuid'])
->create();
}

public function down(): void
{
$this->table('sentry_traces')->drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;

class OrmDefault2125b91024f8895d66c10f3ce9668b4f extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('sentry_issues')
->addColumn('created_at', 'datetime', ['nullable' => false, 'defaultValue' => null, 'withTimezone' => false])
->addColumn('uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('trace_uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('title', 'text', ['nullable' => false, 'defaultValue' => null])
->addColumn('platform', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 32])
->addColumn('logger', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 32])
->addColumn('type', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 32])
->addColumn('transaction', 'string', ['nullable' => true, 'defaultValue' => null, 'size' => 255])
->addColumn('server_name', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addColumn('payload', 'jsonb', ['nullable' => false, 'defaultValue' => null])
->addIndex(['trace_uuid'], ['name' => 'sentry_issues_index_trace_uuid_666ebc74b7900', 'unique' => false])
->addForeignKey(['trace_uuid'], 'sentry_traces', ['uuid'], [
'name' => 'sentry_issues_foreign_trace_uuid_666ebc74b78fb',
'delete' => 'CASCADE',
'update' => 'CASCADE',
'indexCreate' => true,
])
->setPrimaryKeys(['uuid'])
->create();
}

public function down(): void
{
$this->table('sentry_issues')->drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;

class OrmDefaultEaf9d271c936d277c83848f39607bce4 extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('sentry_issue_tag')
->addColumn('issue_uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('tag', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addColumn('value', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 255])
->addIndex(['issue_uuid'], ['name' => 'sentry_issue_tag_index_issue_uuid_666ebc74b7863', 'unique' => false])
->addForeignKey(['issue_uuid'], 'sentry_issues', ['uuid'], [
'name' => 'sentry_issue_tag_foreign_issue_uuid_666ebc74b7870',
'delete' => 'CASCADE',
'update' => 'CASCADE',
'indexCreate' => true,
])
->setPrimaryKeys(['issue_uuid', 'tag'])
->create();
}

public function down(): void
{
$this->table('sentry_issue_tag')->drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;

class OrmDefault010519bf047ccc1faaca91e6f526f378 extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('sentry_issue_fingerprints')
->addColumn('created_at', 'datetime', ['nullable' => false, 'defaultValue' => null, 'withTimezone' => false],
)
->addColumn('uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('issue_uuid', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 36])
->addColumn('fingerprint', 'string', ['nullable' => false, 'defaultValue' => null, 'size' => 50])
->addIndex(['issue_uuid'],
['name' => 'sentry_issue_fingerprints_index_issue_uuid_666ebc74b7929', 'unique' => false])
->addIndex(['issue_uuid', 'fingerprint'], ['name' => '9961459aa46305dec16ff24fb1284ae6', 'unique' => true])
->addForeignKey(['issue_uuid'], 'sentry_issues', ['uuid'], [
'name' => 'bad38aad05e5c71fac6b43c8f4ef8066',
'delete' => 'CASCADE',
'update' => 'CASCADE',
'indexCreate' => true,
])
->setPrimaryKeys(['uuid'])
->create();
}

public function down(): void
{
$this->table('sentry_issue_fingerprints')->drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;

class OrmDefaultB85343121e5fbd05ec587256dfb444e9 extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->table('events')
->addColumn('group_id', 'string', ['nullable' => true, 'defaultValue' => null, 'size' => 255])
->addIndex(['group_id'], ['name' => 'events_index_group_id_667079a9c5a94', 'unique' => false])
->update();
}

public function down(): void
{
$this->table('events')
->dropIndex(['group_id'])
->dropColumn('group_id')
->update();
}
}
13 changes: 13 additions & 0 deletions app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)]
#[Index(columns: ['type'])]
#[Index(columns: ['project'])]
#[Index(columns: ['group_id'])]
class Event
{
/** @internal */
Expand All @@ -29,6 +30,8 @@ public function __construct(
private Json $payload,
#[Column(type: 'string(25)', typecast: Timestamp::class)]
private Timestamp $timestamp,
#[Column(type: 'string', name: 'group_id', nullable: true)]
private ?string $groupId = null,
#[Column(type: 'string', nullable: true, typecast: Key::class)]
private ?Key $project = null,
) {}
Expand Down Expand Up @@ -58,8 +61,18 @@ public function getTimestamp(): Timestamp
return $this->timestamp;
}

public function updateTimestamp(?Timestamp $timestamp = null): void
{
$this->timestamp = $timestamp ?? Timestamp::create();
}

public function getProject(): ?Key
{
return $this->project;
}

public function getGroupId(): ?string
{
return $this->groupId;
}
}
3 changes: 2 additions & 1 deletion app/modules/Events/Domain/EventRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Modules\Events\Domain;

use App\Application\Event\StackStrategy;
use Cycle\ORM\RepositoryInterface;

/**
Expand All @@ -16,7 +17,7 @@ public function findAll(array $scope = [], array $orderBy = [], int $limit = 30,

public function countAll(array $scope = []): int;

public function store(Event $event): bool;
public function store(Event $event, StackStrategy $stackStrategy): bool;

public function deleteAll(array $scope = []): void;

Expand Down
37 changes: 34 additions & 3 deletions app/modules/Events/Integration/CycleOrm/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Modules\Events\Integration\CycleOrm;

use App\Application\Event\StackStrategy;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Select\Repository;
Expand All @@ -23,10 +24,33 @@ public function __construct(
parent::__construct($select);
}

public function store(Event $event): bool
public function store(Event $event, StackStrategy $stackStrategy): bool
{
if (($found = $this->findByPK($event->getUuid())) !== null) {
$found->setPayload($event->getPayload());
$found = null;
if ($event->getGroupId() !== null && $stackStrategy === StackStrategy::All) {
$found = $this->findOne(['group_id' => $event->getGroupId()]);
if (!$found) {
$found = $event;
} else {
$found->setPayload($event->getPayload());
$found->updateTimestamp();
}
} elseif ($event->getGroupId() !== null && $stackStrategy === StackStrategy::OnlyLatest) {
$found = $this->findLatest();
if ($found && $found->getGroupId() === $event->getGroupId()) {
$found->setPayload($event->getPayload());
$found->updateTimestamp();
} else {
$found = $event;
}
}

// if (!$found && $found = $this->findByPK($event->getUuid())) {
// $found->setPayload($event->getPayload());
// $found->updateTimestamp();
// }

if ($found) {
$this->em->persist($found);
} else {
$this->em->persist($event);
Expand Down Expand Up @@ -98,4 +122,11 @@ private function buildScope(array $scope): array

return $newScope;
}

private function findLatest(): ?Event
{
return $this->select()
->orderBy(['timestamp' => 'DESC'])
->fetchOne();
}
}
2 changes: 2 additions & 0 deletions app/modules/Events/Interfaces/Commands/StoreEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public function handle(HandleReceivedEvent $command): void
type: $command->type,
payload: new Json($command->payload),
timestamp: Timestamp::create(),
groupId: $command->groupId,
project: $project?->getKey(),
),
$command->stackStrategy,
);

$this->dispatcher->dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Application\Commands\FindEventByUuid;
use App\Application\Domain\ValueObjects\Uuid;
use App\Application\Exception\EntityNotFoundException;
use Modules\Sentry\Application\EventHandlerInterface;
use Modules\Ray\Application\EventHandlerInterface;
use Spiral\Cqrs\QueryBusInterface;

final readonly class MergeEventsHandler implements EventHandlerInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Modules\Ray\Application\Handlers;

use Modules\Ray\Application\DumpIdParser;
use Modules\Sentry\Application\EventHandlerInterface;
use Modules\Ray\Application\EventHandlerInterface;

final class RemoveSfDumpScriptHandler implements EventHandlerInterface
{
Expand Down Expand Up @@ -45,9 +45,9 @@ private function cleanHtml(string $html): string

// Remove everything except <pre> tags and their content
return \preg_replace(
'/(?s)(.*?)(<pre[^>]*>.*?<\/pre>)(.*)|(?s)(.*)/',
'$2',
$html,
) . '<script>Sfdump("' . $sfDumpId . '")</script>';
'/(?s)(.*?)(<pre[^>]*>.*?<\/pre>)(.*)|(?s)(.*)/',
'$2',
$html,
) . '<script>Sfdump("' . $sfDumpId . '")</script>';
}
}
5 changes: 4 additions & 1 deletion app/modules/Ray/Interfaces/Http/Handler/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Application\Commands\HandleReceivedEvent;
use App\Application\Domain\ValueObjects\Uuid;
use App\Application\Event\EventType;
use App\Application\Event\StackStrategy;
use App\Application\Service\HttpHandler\HandlerInterface;
use Carbon\CarbonInterval;
use Modules\Ray\Application\EventHandlerInterface;
Expand Down Expand Up @@ -70,7 +71,9 @@ private function handleEvent(ServerRequestInterface $request, EventType $eventTy
type: $eventType->type,
payload: $event,
project: $eventType->project,
uuid: Uuid::fromString($event['uuid']),
// uuid: Uuid::fromString($event['uuid']),
groupId: $event['uuid'],
stackStrategy: StackStrategy::All,
),
);

Expand Down
Loading
Loading