Skip to content

Commit

Permalink
Send Event asynchronously (#513)
Browse files Browse the repository at this point in the history
* Adds AsyncHttpClient

* Update ClientBuilderFactory and Version classes

* Refactor AsyncHttpClient to use arrow function

* Fix formatting in AsyncHttpClient

* Update Sentry HttpClientInterface implementation

* Remove useless codes

* Add Hyperf\Engine\Channel import to HttpClient.php

* Refactor HttpClient and HttpClientFactory

* Add http_client_chanel_size and http_client_concurrent_limit configuration options

* Add http options

* Refactor HttpClient.php to use Hyperf\Engine\Coroutine instead of Hyperf\Coroutine\Coroutine

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Jan 3, 2024
1 parent 998d02d commit 1c880f2
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 6 deletions.
4 changes: 4 additions & 0 deletions publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,8 @@
],
],
],

'http_timeout' => (float) env('SENTRY_HTTP_TIMEOUT', 2.0),
'http_chanel_size' => (int) env('SENTRY_HTTP_CHANEL_SIZE', 65535),
'http_concurrent_limit' => (int) env('SENTRY_HTTP_CONCURRENT_LIMIT', 100),
];
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __invoke(): array
'dependencies' => [
\Sentry\ClientBuilder::class => Factory\ClientBuilderFactory::class,
\Sentry\State\HubInterface::class => Factory\HubFactory::class,
\Sentry\HttpClient\HttpClientInterface::class => HttpClient\HttpClientFactory::class,
],
'listeners' => [
Listener\AmqpExceptionListener::class,
Expand Down
20 changes: 14 additions & 6 deletions src/Factory/ClientBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

use FriendsOfHyperf\Sentry\Version;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Support\Composer;
use Psr\Container\ContainerInterface;
use Sentry\ClientBuilder;
use Sentry\HttpClient\HttpClientInterface;

use function Hyperf\Support\env;
use function Hyperf\Tappable\tap;
Expand All @@ -28,6 +28,8 @@ class ClientBuilderFactory
'integrations',
'enable',
'tracing',
'http_chanel_size',
'http_concurrent_limit',
];

public function __invoke(ContainerInterface $container)
Expand Down Expand Up @@ -63,10 +65,16 @@ public function __invoke(ContainerInterface $container)
$options['environment'] = env('APP_ENV', 'production');
}

return tap(ClientBuilder::create($options), function (ClientBuilder $clientBuilder) {
$clientBuilder->setSdkIdentifier(Version::SDK_IDENTIFIER);
$sdkVersion = Composer::getVersions()['friendsofhyperf/sentry'] ?? Version::SDK_VERSION;
$clientBuilder->setSdkVersion($sdkVersion);
});
if (
! ($options['http_client'] ?? null) instanceof HttpClientInterface
&& $container->has(HttpClientInterface::class)
) {
$options['http_client'] = $container->get(HttpClientInterface::class);
}

return tap(
ClientBuilder::create($options),
fn (ClientBuilder $clientBuilder) => $clientBuilder->setSdkIdentifier(Version::getSdkIdentifier())->setSdkVersion(Version::getSdkVersion())
);
}
}
101 changes: 101 additions & 0 deletions src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Sentry\HttpClient;

use Closure;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coroutine\Concurrent;
use Hyperf\Engine\Channel;
use Hyperf\Engine\Coroutine;
use Sentry\HttpClient\Request;
use Sentry\HttpClient\Response;
use Sentry\Options;
use Throwable;

class HttpClient extends \Sentry\HttpClient\HttpClient
{
protected ?Channel $chan = null;

protected ?Concurrent $concurrent = null;

public function __construct(
string $sdkIdentifier,
string $sdkVersion,
protected int $channelSize = 65535,
int $concurrentLimit = 100,
) {
parent::__construct($sdkIdentifier, $sdkVersion);

if ($concurrentLimit > 0) {
$this->concurrent = new Concurrent($concurrentLimit);
}
}

public function sendRequest(Request $request, Options $options): Response
{
$this->loop();

$chan = $this->chan;
$chan->push(fn () => parent::sendRequest($request, $options));

return new Response(202, [], 'Waiting for sendRequest');
}

public function close(): void
{
$chan = $this->chan;
$this->chan = null;

$chan?->close();
}

protected function loop(): void
{
if ($this->chan != null) {
return;
}

$this->chan = new Channel($this->channelSize);

Coroutine::create(function () {
while (true) {
while (true) {
/** @var Closure|null $closure */
$closure = $this->chan?->pop();
if (! $closure) {
break 2;
}
try {
if ($this->concurrent) {
$this->concurrent->create($closure);
} else {
Coroutine::create($closure);
}
} catch (Throwable) {
break;
} finally {
$closure = null;
}
}
}

$this->close();
});

Coroutine::create(function () {
if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield()) {
$this->close();
}
});
}
}
30 changes: 30 additions & 0 deletions src/HttpClient/HttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Sentry\HttpClient;

use FriendsOfHyperf\Sentry\Version;
use Hyperf\Contract\ConfigInterface;
use Psr\Container\ContainerInterface;

class HttpClientFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get(ConfigInterface::class);
return new HttpClient(
Version::getSdkIdentifier(),
Version::getSdkVersion(),
(int) $config->get('sentry.http_chanel_size', 65535),
(int) $config->get('sentry.http_concurrent_limit', 100)
);
}
}
12 changes: 12 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@

namespace FriendsOfHyperf\Sentry;

use Hyperf\Support\Composer;

final class Version
{
public const SDK_IDENTIFIER = 'sentry.php.hyperf';

public const SDK_VERSION = '3.1.0';

public static function getSdkIdentifier(): string
{
return self::SDK_IDENTIFIER;
}

public static function getSdkVersion(): string
{
return Composer::getVersions()['friendsofhyperf/sentry'] ?? self::SDK_VERSION;
}
}

0 comments on commit 1c880f2

Please sign in to comment.