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

feat: add sentry-sdk ^4.0 support #141

Merged
merged 7 commits into from
Apr 29, 2024
Merged
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
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());
}
};
}
}
Loading