Skip to content
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"psr/http-factory-implementation": "*",
"psy/psysh": "^0.10.0 || ^0.11.0",
"ramsey/uuid": "^4.7",
"sentry/sentry": "^4.19.0",
"sentry/sentry": "^4.21.0",
"symfony/console": "^5.3 || ^6.0 || ^7.0",
"symfony/http-foundation": "^5.3 || ^6.0 || ^7.0",
"symfony/polyfill-php84": "^1.33",
Expand Down Expand Up @@ -330,4 +330,4 @@
"test:types": "@php vendor/bin/pest --type-coverage",
"test:unit": "@php vendor/bin/pest"
}
}
}
65 changes: 0 additions & 65 deletions src/sentry/class_map/SentrySdk.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/sentry/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"hyperf/http-server": "~3.1.0",
"hyperf/support": "~3.1.0",
"hyperf/tappable": "~3.1.0",
"sentry/sentry": "^4.19.0",
"sentry/sentry": "^4.21.0",
"symfony/polyfill-php85": "^1.33"
},
"suggest": {
Expand Down Expand Up @@ -65,4 +65,4 @@
"config": "FriendsOfHyperf\\Sentry\\ConfigProvider"
}
}
}
}
3 changes: 1 addition & 2 deletions src/sentry/src/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace FriendsOfHyperf\Sentry\Aspect;

use FriendsOfHyperf\Sentry\Integration;
use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand Down Expand Up @@ -60,7 +59,7 @@ protected function handleCreate(ProceedingJoinPoint $proceedingJoinPoint): void
}

// Defer the flushing of events until the coroutine completes.
defer(fn () => Integration::flushEvents());
defer(fn () => SentrySdk::flush());

// Continue the callable in the new Coroutine.
$callable();
Expand Down
72 changes: 72 additions & 0 deletions src/sentry/src/Aspect/SentrySdkAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Sentry\Aspect;

use Closure;
use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\RuntimeContextManager;

use function Hyperf\Support\make;

/**
* @mixin \Sentry\SentrySdk
*/
class SentrySdkAspect extends AbstractAspect
{
public array $classes = [
SentrySdk::class . '::init',
SentrySdk::class . '::setCurrentHub',
SentrySdk::class . '::getRuntimeContextManager',
];

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return match ($proceedingJoinPoint->methodName) {
'init' => $this->handleInit($proceedingJoinPoint),
'setCurrentHub' => $this->handleSetCurrentHub($proceedingJoinPoint),
'getRuntimeContextManager' => $this->handleGetRuntimeContextManager($proceedingJoinPoint),
default => $proceedingJoinPoint->process(),
};
}

private function handleInit(ProceedingJoinPoint $proceedingJoinPoint)
{
Context::set(
RuntimeContextManager::class,
new RuntimeContextManager(make(HubInterface::class))
);

return SentrySdk::getCurrentHub();
}

private function handleSetCurrentHub(ProceedingJoinPoint $proceedingJoinPoint)
{
$arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
$hub = $arguments['hub'];
Comment on lines +54 to +55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

参数访问缺少防御性检查,可能导致运行时错误。

$arguments 数组中不存在 hub 键时,第 55 行会抛出 Undefined index 错误。建议添加空值检查或更明确的错误处理。

🛡️ 建议的修复方案
     private function handleSetCurrentHub(ProceedingJoinPoint $proceedingJoinPoint)
     {
         $arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
-        $hub = $arguments['hub'];
+        $hub = $arguments['hub'] ?? throw new \InvalidArgumentException('Hub argument is required');
         // `@phpstan-ignore-next-line`
         Closure::bind(fn () => static::getRuntimeContextManager()->setCurrentHub($hub), null, SentrySdk::class)();

         return $hub;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
$hub = $arguments['hub'];
private function handleSetCurrentHub(ProceedingJoinPoint $proceedingJoinPoint)
{
$arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
$hub = $arguments['hub'] ?? throw new \InvalidArgumentException('Hub argument is required');
// `@phpstan-ignore-next-line`
Closure::bind(fn () => static::getRuntimeContextManager()->setCurrentHub($hub), null, SentrySdk::class)();
return $hub;
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/sentry/src/Aspect/SentrySdkAspect.php` around lines 54 - 55, The code is
reading $arguments = $proceedingJoinPoint->arguments['keys'] ?? []; and then
accessing $arguments['hub'] without checking existence which can throw an
Undefined index error; update the access to guard against missing 'hub' (e.g.,
retrieve with $arguments['hub'] ?? null or use isset($arguments['hub']) before
use) and add handling when hub is null (early return, skip Sentry logic, or
throw a clear exception such as InvalidArgumentException) so the code using
$arguments and $proceedingJoinPoint does not assume the 'hub' key is present.

// @phpstan-ignore-next-line
Closure::bind(fn () => static::getRuntimeContextManager()->setCurrentHub($hub), null, SentrySdk::class)();

return $hub;
}

private function handleGetRuntimeContextManager(ProceedingJoinPoint $proceedingJoinPoint)
{
return Context::getOrSet(
RuntimeContextManager::class,
fn () => new RuntimeContextManager(make(HubInterface::class))
);
}
}
5 changes: 2 additions & 3 deletions src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __invoke(): array
Aspect\LoggerAspect::class,
Aspect\RedisAspect::class,
// Aspect\SingletonAspect::class,
Aspect\SentrySdkAspect::class,
Metrics\Aspect\CounterAspect::class,
Metrics\Aspect\HistogramAspect::class,
Tracing\Aspect\AmqpProducerAspect::class,
Expand Down Expand Up @@ -75,9 +76,7 @@ public function __invoke(): array
],
'annotations' => [
'scan' => [
'class_map' => [
\Sentry\SentrySdk::class => __DIR__ . '/../class_map/SentrySdk.php',
],
'class_map' => [],
],
],
'publish' => [
Expand Down
6 changes: 1 addition & 5 deletions src/sentry/src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\Integration\IntegrationInterface;
use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -106,9 +104,7 @@ public static function setTransaction(?string $transaction): void
*/
public static function flushEvents(): void
{
Logs::getInstance()->flush();
TraceMetrics::getInstance()->flush();
SentrySdk::getCurrentHub()->getClient()?->flush();
SentrySdk::flush();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/OnBeforeHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
use FriendsOfHyperf\Sentry\SentryContext;
Expand All @@ -22,6 +21,7 @@
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Sentry\SentrySdk;
use Sentry\Unit;

use function FriendsOfHyperf\Sentry\metrics;
Expand Down Expand Up @@ -115,8 +115,7 @@ function () use ($metrics) {
Unit::megabyte()
);

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace FriendsOfHyperf\Sentry\Metrics\Listener;

use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Server\Event\MainCoroutineServerStart;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Sentry\SentrySdk;
use Sentry\Unit;

use function FriendsOfHyperf\Sentry\metrics;
Expand Down Expand Up @@ -113,8 +113,7 @@ function () use ($metrics) {
Unit::megabyte()
);

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use FriendsOfHyperf\Sentry\Constants as SentryConstants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
Expand All @@ -22,6 +21,7 @@
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Support\System;
use Psr\Container\ContainerInterface;
use Sentry\SentrySdk;
use Sentry\Unit;
use Swoole\Server as SwooleServer;

Expand Down Expand Up @@ -130,8 +130,7 @@ function () use ($metrics, $serverStatsFactory, $workerId) {
Unit::megabyte()
);

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/OnWorkerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace FriendsOfHyperf\Sentry\Metrics\Listener;

use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BeforeWorkerStart;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Sentry\SentrySdk;
use Sentry\Unit;
use Swoole\Server;

Expand Down Expand Up @@ -120,8 +120,7 @@ function () use ($metrics, $event) {
Unit::megabyte()
);

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/PoolWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
namespace FriendsOfHyperf\Sentry\Metrics\Listener;

use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BeforeWorkerStart;
use Hyperf\Pool\Pool;
use Hyperf\Server\Event\MainCoroutineServerStart;
use Psr\Container\ContainerInterface;
use Sentry\SentrySdk;

use function FriendsOfHyperf\Sentry\metrics;

Expand Down Expand Up @@ -90,8 +90,7 @@ function () use ($pool, $workerId, $poolName) {
]
);

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Listener/QueueWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
namespace FriendsOfHyperf\Sentry\Metrics\Listener;

use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use Sentry\SentrySdk;

use function FriendsOfHyperf\Sentry\metrics;

Expand Down Expand Up @@ -84,8 +84,7 @@ function () {
);
}

// metrics()->flush();
Integration::flushEvents();
SentrySdk::flush();
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/src/Metrics/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry\Metrics;

use Sentry\SentrySdk;
use Sentry\Unit;

use function FriendsOfHyperf\Sentry\metrics;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function end(bool $flush = false): void
$this->unit ?? Unit::second()
);

$flush && metrics()->flush();
$flush && SentrySdk::flush();

$this->ended = true;
}
Expand Down
Loading