-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat(sentry): add SentrySdkAspect for coroutine-safe SDK usage #1063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6cce8a1
78999fb
8d0c644
5f543e6
35252e7
7949d63
042ed4e
4ba375b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| 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(); | ||||||||||||||||||||||||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private function handleSetCurrentHub(ProceedingJoinPoint $proceedingJoinPoint) | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| $arguments = $proceedingJoinPoint->arguments['keys'] ?? []; | ||||||||||||||||||||||||
| $hub = $arguments['hub']; | ||||||||||||||||||||||||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 参数访问缺少防御性检查,可能导致运行时错误。 当 🛡️ 建议的修复方案 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| // @phpstan-ignore-next-line | ||||||||||||||||||||||||
| Closure::bind(fn () => static::getRuntimeContextManager()->setCurrentHub($hub), null, SentrySdk::class)(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return $hub; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private function handleGetRuntimeContextManager(ProceedingJoinPoint $proceedingJoinPoint) | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| return Context::getOrSet( | ||||||||||||||||||||||||
| RuntimeContextManager::class, | ||||||||||||||||||||||||
| fn () => new RuntimeContextManager(make(HubInterface::class)) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.