diff --git a/composer.json b/composer.json index 0c231b823..e4eeefcd1 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -330,4 +330,4 @@ "test:types": "@php vendor/bin/pest --type-coverage", "test:unit": "@php vendor/bin/pest" } -} +} \ No newline at end of file diff --git a/src/sentry/composer.json b/src/sentry/composer.json index 0390dddfb..fedcfd358 100644 --- a/src/sentry/composer.json +++ b/src/sentry/composer.json @@ -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": { @@ -65,4 +65,4 @@ "config": "FriendsOfHyperf\\Sentry\\ConfigProvider" } } -} +} \ No newline at end of file diff --git a/src/sentry/src/Aspect/SentrySdkAspect.php b/src/sentry/src/Aspect/SentrySdkAspect.php new file mode 100644 index 000000000..6416424e0 --- /dev/null +++ b/src/sentry/src/Aspect/SentrySdkAspect.php @@ -0,0 +1,69 @@ +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']; + // @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)) + ); + } +} diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index e101e53cb..35f454be6 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -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, @@ -76,7 +77,7 @@ public function __invoke(): array 'annotations' => [ 'scan' => [ 'class_map' => [ - \Sentry\SentrySdk::class => __DIR__ . '/../class_map/SentrySdk.php', + // \Sentry\SentrySdk::class => __DIR__ . '/../class_map/SentrySdk.php', ], ], ],