Skip to content
27 changes: 22 additions & 5 deletions src/sentry/src/Aspect/SingletonAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry\Aspect;

use Closure;
use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand All @@ -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();
});
}
}
5 changes: 2 additions & 3 deletions src/sentry/src/Metrics/Aspect/CounterAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Hyperf\Di\Aop\ProceedingJoinPoint;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class CounterAspect extends AbstractAspect
{
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/src/Metrics/Aspect/HistogramAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
});
}

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 @@ -21,7 +21,6 @@
use Sentry\Unit;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class OnBeforeHandle implements ListenerInterface
{
Expand Down Expand Up @@ -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());

Expand All @@ -110,6 +107,8 @@ public function process(object $event): void
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
});
}
}
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 @@ -22,7 +22,6 @@
use Sentry\Unit;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class OnCoroutineServerStart implements ListenerInterface
{
Expand Down Expand Up @@ -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());

Expand All @@ -119,6 +116,8 @@ public function process(object $event): void
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
});
}
}
6 changes: 3 additions & 3 deletions src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Swoole\Server as SwooleServer;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class OnMetricFactoryReady implements ListenerInterface
{
Expand Down Expand Up @@ -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);

Expand All @@ -115,6 +112,7 @@ public function process(object $event): void
}

$load = sys_getloadavg();

metrics()->gauge(
'sys_load',
round($load[0] / System::getCpuCoresNum(), 2),
Expand All @@ -132,6 +130,8 @@ public function process(object $event): void
['worker' => (string) $workerId],
Unit::megabyte()
);

metrics()->flush();
});
}
}
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 @@ -23,7 +23,6 @@
use Swoole\Server;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class OnWorkerStart implements ListenerInterface
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -121,6 +118,8 @@ public function process(object $event): void
['worker' => (string) ($event->workerId ?? 0)],
Unit::megabyte()
);

metrics()->flush();
});
}
}
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 @@ -20,7 +20,6 @@
use Psr\Container\ContainerInterface;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

abstract class PoolWatcher implements ListenerInterface
{
Expand Down Expand Up @@ -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(),
Expand All @@ -97,6 +94,8 @@ public function watch(Pool $pool, string $poolName, int $workerId): void
'worker' => (string) $workerId,
]
);

metrics()->flush();
});
}
}
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 @@ -20,7 +20,6 @@
use Psr\Container\ContainerInterface;

use function FriendsOfHyperf\Sentry\metrics;
use function Hyperf\Coroutine\defer;

class QueueWatcher implements ListenerInterface
{
Expand Down Expand Up @@ -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', []));

Expand Down Expand Up @@ -87,6 +84,8 @@ public function process(object $event): void
['queue' => $name]
);
}

metrics()->flush();
});
}
}
4 changes: 2 additions & 2 deletions src/sentry/src/Metrics/Listener/RequestWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/sentry/src/Metrics/Traits/MetricSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ protected function trySet(string $prefix, array $metrics, array $stats, int $wor
}
}
}

// protected function spawnDefaultMetrics()
// {
// }
}
3 changes: 1 addition & 2 deletions src/sentry/src/Tracing/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
});
Expand Down
12 changes: 6 additions & 6 deletions src/sentry/src/Tracing/Listener/EventHandleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +55,6 @@

use function FriendsOfHyperf\Sentry\startTransaction;
use function FriendsOfHyperf\Sentry\trace;
use function Hyperf\Coroutine\defer;
use function Sentry\continueTrace;

/**
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions src/sentry/src/Tracing/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,6 @@
use Sentry\Tracing\TransactionSource;
use Throwable;

use function Hyperf\Coroutine\defer;
use function Sentry\trace;

class Tracer
Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down