Skip to content

Commit

Permalink
Add sentry-sdk ^4.0 support (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: Florent Baldino <[email protected]>
  • Loading branch information
WaylandAce and Baldinof committed Apr 29, 2024
1 parent 3b2b421 commit 5c4b631
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"require-dev": {
"symfony/var-dumper": "^6.0 || ^7.0",
"sentry/sentry-symfony": "^4.5",
"sentry/sentry-symfony": "^4.5 || ^5.0",
"symfony/framework-bundle": "^6.0 || ^7.0",
"nyholm/psr7": "^1.2",
"doctrine/mongodb-odm": "^2.2",
Expand Down
8 changes: 5 additions & 3 deletions src/Integration/Sentry/SentryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Baldinof\RoadRunnerBundle\Event\WorkerExceptionEvent;
use Baldinof\RoadRunnerBundle\Event\WorkerStopEvent;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\State\HubInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -17,9 +18,10 @@ public function __construct(private HubInterface $hub)

public function onWorkerStop(WorkerStopEvent $event): void
{
$client = $this->hub->getClient();

$client?->flush()->wait(false);
$result = $this->hub->getClient()?->flush();
if (class_exists(PromiseInterface::class) && $result instanceof PromiseInterface) {
$result->wait(false);
}
}

public function onWorkerException(WorkerExceptionEvent $event): void
Expand Down
7 changes: 5 additions & 2 deletions src/Integration/Sentry/SentryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Baldinof\RoadRunnerBundle\Integration\Sentry;

use Baldinof\RoadRunnerBundle\Http\MiddlewareInterface;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -25,8 +26,10 @@ public function process(Request $request, HttpKernelInterface $next): \Iterator
try {
yield $next->handle($request);
} finally {
$client = $this->hub->getClient();
$client?->flush()->wait(false);
$result = $this->hub->getClient()?->flush();
if (class_exists(PromiseInterface::class) && $result instanceof PromiseInterface) {
$result->wait(false);
}

$this->hub->popScope();
}
Expand Down
30 changes: 27 additions & 3 deletions tests/Integration/Sentry/SentryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
use GuzzleHttp\Promise\PromiseInterface;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Event;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\Transport\Result;
use Sentry\Transport\ResultStatus;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -49,9 +52,13 @@ public function initHub(array $options): void

$opts = new Options(array_merge($options, ['default_integrations' => true]));

$client = (new ClientBuilder($opts))
->setTransportFactory($this->getTransportFactoryMock())
->getClient();
$builder = new ClientBuilder($opts);
if (version_compare(Client::SDK_VERSION, '4.0.0', '<')) {
$builder->setTransportFactory($this->getTransportFactoryMock());
} else {
$builder->setTransport($this->getV4TransportMock());
}
$client = $builder->getClient();

$hub = new Hub($client);

Expand Down Expand Up @@ -115,4 +122,21 @@ public function close(?int $timeout = null): PromiseInterface
}
};
}

private function getV4TransportMock(): TransportInterface
{
return new class() implements TransportInterface {
public function send(Event $event): Result
{
SentryMiddlewareTest::$collectedEvents->push($event);

return new Result(ResultStatus::success(), $event);
}

public function close(?int $timeout = null): Result
{
return new Result(ResultStatus::success());
}
};
}
}

0 comments on commit 5c4b631

Please sign in to comment.