diff --git a/src/sentry/src/Aspect/SingletonAspect.php b/src/sentry/src/Aspect/SingletonAspect.php index 5c0aec6e4..e022d229f 100644 --- a/src/sentry/src/Aspect/SingletonAspect.php +++ b/src/sentry/src/Aspect/SingletonAspect.php @@ -11,6 +11,7 @@ namespace FriendsOfHyperf\Sentry\Aspect; +use Closure; use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -33,13 +34,29 @@ class SingletonAspect extends AbstractAspect public function process(ProceedingJoinPoint $proceedingJoinPoint) { - $key = $proceedingJoinPoint->className; - $args = $proceedingJoinPoint->getArguments(); + $key = $className = $proceedingJoinPoint->className; + $arguments = $proceedingJoinPoint->getArguments(); - if (! empty($args)) { - $key .= '#' . $args[0]; + if (isset($arguments[0])) { + $key .= '#' . $arguments[0]; } - return Context::getOrSet($key, fn () => $proceedingJoinPoint->process()); + return Context::getOrSet($key, function () use ($proceedingJoinPoint, $className, $arguments) { + // Reset singleton instance before proceeding + Closure::bind(function () use ($className, $arguments) { + if (property_exists($className, 'instance')) { + $className::$instance = null; + } elseif ( + property_exists($className, 'instances') + && isset($arguments[0]) + && array_key_exists($arguments[0], $className::$instances) + ) { + $className::$instances[$arguments[0]] = null; + } + }, null, $className)(); + + // Proceed to get the singleton instance + return $proceedingJoinPoint->process(); + }); } } diff --git a/src/sentry/src/Metrics/Aspect/CounterAspect.php b/src/sentry/src/Metrics/Aspect/CounterAspect.php index b6f25911a..6991903f9 100644 --- a/src/sentry/src/Metrics/Aspect/CounterAspect.php +++ b/src/sentry/src/Metrics/Aspect/CounterAspect.php @@ -17,7 +17,6 @@ use Hyperf\Di\Aop\ProceedingJoinPoint; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class CounterAspect extends AbstractAspect { @@ -46,12 +45,12 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed $name = $source; } - defer(fn () => metrics()->flush()); - metrics()->count($name, 1, [ 'class' => $proceedingJoinPoint->className, 'method' => $proceedingJoinPoint->methodName, ]); + + metrics()->flush(); } return $proceedingJoinPoint->process(); diff --git a/src/sentry/src/Metrics/Aspect/HistogramAspect.php b/src/sentry/src/Metrics/Aspect/HistogramAspect.php index 50845cf19..452db7dbf 100644 --- a/src/sentry/src/Metrics/Aspect/HistogramAspect.php +++ b/src/sentry/src/Metrics/Aspect/HistogramAspect.php @@ -16,8 +16,8 @@ use FriendsOfHyperf\Sentry\Metrics\Timer; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; +use Hyperf\Engine\Coroutine as Co; -use function Hyperf\Coroutine\defer; use function Hyperf\Tappable\tap; class HistogramAspect extends AbstractAspect @@ -54,7 +54,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed ]); return tap($proceedingJoinPoint->process(), function () use ($timer) { - defer(fn () => $timer->end(true)); + Co::defer(fn () => $timer->end(true)); }); } diff --git a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php index de9691bb1..15472535a 100644 --- a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php +++ b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php @@ -21,7 +21,6 @@ use Sentry\Unit; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class OnBeforeHandle implements ListenerInterface { @@ -93,8 +92,6 @@ public function process(object $event): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - $this->trySet('gc_', $metrics, gc_status()); $this->trySet('', $metrics, getrusage()); @@ -110,6 +107,8 @@ public function process(object $event): void ['worker' => '0'], Unit::megabyte() ); + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php index cf371a4aa..3fff861b6 100644 --- a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php +++ b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php @@ -22,7 +22,6 @@ use Sentry\Unit; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class OnCoroutineServerStart implements ListenerInterface { @@ -102,8 +101,6 @@ public function process(object $event): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - $this->trySet('gc_', $metrics, gc_status()); $this->trySet('', $metrics, getrusage()); @@ -119,6 +116,8 @@ public function process(object $event): void ['worker' => '0'], Unit::megabyte() ); + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php index ada52adbb..3496791cc 100644 --- a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php +++ b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php @@ -25,7 +25,6 @@ use Swoole\Server as SwooleServer; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class OnMetricFactoryReady implements ListenerInterface { @@ -101,8 +100,6 @@ public function process(object $event): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - $this->trySet('', $metrics, Coroutine::stats(), $workerId); $this->trySet('timer_', $metrics, Timer::stats(), $workerId); @@ -115,6 +112,7 @@ public function process(object $event): void } $load = sys_getloadavg(); + metrics()->gauge( 'sys_load', round($load[0] / System::getCpuCoresNum(), 2), @@ -132,6 +130,8 @@ public function process(object $event): void ['worker' => (string) $workerId], Unit::megabyte() ); + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/OnWorkerStart.php b/src/sentry/src/Metrics/Listener/OnWorkerStart.php index 046d33798..a038813fc 100644 --- a/src/sentry/src/Metrics/Listener/OnWorkerStart.php +++ b/src/sentry/src/Metrics/Listener/OnWorkerStart.php @@ -23,7 +23,6 @@ use Swoole\Server; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class OnWorkerStart implements ListenerInterface { @@ -92,8 +91,6 @@ public function process(object $event): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - $server = $this->container->get(Server::class); $serverStats = $server->stats(); $this->trySet('gc_', $metrics, gc_status()); @@ -121,6 +118,8 @@ public function process(object $event): void ['worker' => (string) ($event->workerId ?? 0)], Unit::megabyte() ); + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/PoolWatcher.php b/src/sentry/src/Metrics/Listener/PoolWatcher.php index d39d3d6c9..caab50d47 100644 --- a/src/sentry/src/Metrics/Listener/PoolWatcher.php +++ b/src/sentry/src/Metrics/Listener/PoolWatcher.php @@ -20,7 +20,6 @@ use Psr\Container\ContainerInterface; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; abstract class PoolWatcher implements ListenerInterface { @@ -71,8 +70,6 @@ public function watch(Pool $pool, string $poolName, int $workerId): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - metrics()->gauge( $this->getPrefix() . '_connections_in_use', (float) $pool->getCurrentConnections(), @@ -97,6 +94,8 @@ public function watch(Pool $pool, string $poolName, int $workerId): void 'worker' => (string) $workerId, ] ); + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/QueueWatcher.php b/src/sentry/src/Metrics/Listener/QueueWatcher.php index d666d6b6a..c25c97699 100644 --- a/src/sentry/src/Metrics/Listener/QueueWatcher.php +++ b/src/sentry/src/Metrics/Listener/QueueWatcher.php @@ -20,7 +20,6 @@ use Psr\Container\ContainerInterface; use function FriendsOfHyperf\Sentry\metrics; -use function Hyperf\Coroutine\defer; class QueueWatcher implements ListenerInterface { @@ -57,8 +56,6 @@ public function process(object $event): void return Timer::STOP; } - defer(fn () => metrics()->flush()); - $config = $this->container->get(ConfigInterface::class); $queues = array_keys($config->get('async_queue', [])); @@ -87,6 +84,8 @@ public function process(object $event): void ['queue' => $name] ); } + + metrics()->flush(); }); } } diff --git a/src/sentry/src/Metrics/Listener/RequestWatcher.php b/src/sentry/src/Metrics/Listener/RequestWatcher.php index 10bfd66b8..7e08eb972 100644 --- a/src/sentry/src/Metrics/Listener/RequestWatcher.php +++ b/src/sentry/src/Metrics/Listener/RequestWatcher.php @@ -13,7 +13,7 @@ use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats; use FriendsOfHyperf\Sentry\Metrics\Timer; -use Hyperf\Engine\Coroutine; +use Hyperf\Engine\Coroutine as Co; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\HttpServer\Event as HttpEvent; use Hyperf\HttpServer\Router\Dispatched; @@ -49,7 +49,7 @@ public function process(object $event): void 'request_method' => $request->getMethod(), ]); - Coroutine::defer(function () use ($timer) { + Co::defer(function () use ($timer) { ++$this->stats->close_count; ++$this->stats->response_count; --$this->stats->connection_num; diff --git a/src/sentry/src/Metrics/Traits/MetricSetter.php b/src/sentry/src/Metrics/Traits/MetricSetter.php index 89708ae0b..9b5a5f9db 100644 --- a/src/sentry/src/Metrics/Traits/MetricSetter.php +++ b/src/sentry/src/Metrics/Traits/MetricSetter.php @@ -31,8 +31,4 @@ protected function trySet(string $prefix, array $metrics, array $stats, int $wor } } } - - // protected function spawnDefaultMetrics() - // { - // } } diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index bc0170170..f3c58fa82 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -25,7 +25,6 @@ use function FriendsOfHyperf\Sentry\startTransaction; use function FriendsOfHyperf\Sentry\trace; -use function Hyperf\Coroutine\defer; use function Sentry\continueTrace; class CoroutineAspect extends AbstractAspect @@ -83,7 +82,7 @@ function (Scope $scope) use ($proceedingJoinPoint, $callingOnFunction) { ); // Defer the finishing of the transaction and flushing of events until the coroutine completes. - defer(function () use ($transaction) { + Co::defer(function () use ($transaction) { $transaction->finish(); Integration::flushEvents(); }); diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 8d0df8b08..b88191973 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -29,6 +29,7 @@ use Hyperf\Crontab\Event as CrontabEvent; use Hyperf\Database\Events as DbEvent; use Hyperf\DbConnection\Pool\PoolFactory; +use Hyperf\Engine\Coroutine as Co; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\HttpServer\Event as HttpEvent; use Hyperf\HttpServer\Router\Dispatched; @@ -54,7 +55,6 @@ use function FriendsOfHyperf\Sentry\startTransaction; use function FriendsOfHyperf\Sentry\trace; -use function Hyperf\Coroutine\defer; use function Sentry\continueTrace; /** @@ -328,7 +328,7 @@ protected function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\Requ SentrySdk::getCurrentHub()->setSpan($span); - defer(function () use ($transaction, $span) { + Co::defer(function () use ($transaction, $span) { // Make sure the span is finished after the request is handled $span->finish(); @@ -531,7 +531,7 @@ protected function handleCrontabTaskStarting(CrontabEvent\BeforeExecute $event): ]) ); - defer(function () use ($transaction) { + Co::defer(function () use ($transaction) { // Make sure the transaction is finished after the task is executed SentrySdk::getCurrentHub()->setSpan($transaction); @@ -600,7 +600,7 @@ protected function handleAmqpMessageProcessing(AmqpEvent\BeforeConsume $event): ]) ); - defer(function () use ($transaction) { + Co::defer(function () use ($transaction) { // Make sure the transaction is finished after the message is processed SentrySdk::getCurrentHub()->setSpan($transaction); @@ -668,7 +668,7 @@ protected function handleKafkaMessageProcessing(KafkaEvent\BeforeConsume $event) ]) ); - defer(function () use ($transaction) { + Co::defer(function () use ($transaction) { // Make sure the transaction is finished after the message is processed SentrySdk::getCurrentHub()->setSpan($transaction); @@ -718,7 +718,7 @@ protected function handleAsyncQueueJobProcessing(AsyncQueueEvent\BeforeHandle $e ]) ); - defer(function () use ($transaction) { + Co::defer(function () use ($transaction) { // Make sure the transaction is finished after the job is processed SentrySdk::getCurrentHub()->setSpan($transaction); diff --git a/src/sentry/src/Tracing/Tracer.php b/src/sentry/src/Tracing/Tracer.php index 60c216909..0c9a3e885 100644 --- a/src/sentry/src/Tracing/Tracer.php +++ b/src/sentry/src/Tracing/Tracer.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Sentry\Tracing; -use Hyperf\Engine\Coroutine; +use Hyperf\Engine\Coroutine as Co; use Sentry\SentrySdk; use Sentry\State\Scope; use Sentry\Tracing\SpanContext; @@ -21,7 +21,6 @@ use Sentry\Tracing\TransactionSource; use Throwable; -use function Hyperf\Coroutine\defer; use function Sentry\trace; class Tracer @@ -35,9 +34,9 @@ public function startTransaction(TransactionContext $transactionContext, array $ $hub->pushScope(); $hub->configureScope(static fn (Scope $scope) => $scope->clearBreadcrumbs()); - defer(static fn () => $hub->popScope()); + Co::defer(static fn () => $hub->popScope()); - $transactionContext->setData(['coroutine.id' => Coroutine::id()] + $transactionContext->getData()); + $transactionContext->setData(['coroutine.id' => Co::id()] + $transactionContext->getData()); if ($transactionContext->getStartTimestamp() === null) { $transactionContext->setStartTimestamp(microtime(true)); @@ -77,7 +76,7 @@ public function trace(callable $trace, SpanContext $context) $context->setStartTimestamp(microtime(true)); } - $context->setData(['coroutine.id' => Coroutine::id()] + $context->getData()); + $context->setData(['coroutine.id' => Co::id()] + $context->getData()); return trace( function (Scope $scope) use ($trace) {