-
-
Notifications
You must be signed in to change notification settings - Fork 27
refactor(sentry): consolidate event listeners into unified EventListener #913
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d267cfc
refactor(sentry): consolidate event listeners into unified EventListener
huangdijia 30c241d
feat(sentry): replace EventListener with EventHandleListener and upda…
huangdijia 365a7be
feat(sentry): replace CronEventListener with EventHandleListener and …
huangdijia e22420d
feat(sentry): add EventHandleListener for unified event handling and …
huangdijia 47f9105
refactor(sentry): 删除多个过时的事件监听器以简化代码结构
huangdijia 0c0d8c9
fix(sentry): 添加异常处理以记录 flushEvents 方法中的错误
huangdijia d5ba2e0
feat(sentry): 添加缓存追踪选项以增强性能监控
huangdijia 1b4391b
refactor(sentry): 合并异步队列重试和失败处理方法,并添加事件参数注释
huangdijia 477d303
refactor(sentry): 重命名处理 Redis 命令的方法以提高可读性
huangdijia a95a00a
refactor(sentry): 更新常量名称以反映 Sentry Hub 的上下文
huangdijia 0b727b5
fix(sentry): 在处理异常时添加 flushEvents 方法调用以确保事件被正确刷新
huangdijia 67d1f1f
fix(sentry): 在异常处理时根据配置条件添加堆栈跟踪数据
huangdijia 9a77ca8
refactor(sentry): 优化事务数据和标签的设置逻辑,提升代码可读性
huangdijia 862386d
refactor(sentry): 合并设置事务数据和标签的逻辑以提升代码简洁性
huangdijia 4b98d58
refactor(sentry): 简化 Redis 和 AMQP 事件处理中的数据设置逻辑,提升代码可读性
huangdijia d1567d0
refactor(sentry): 重命名事件处理方法以提高可读性和一致性
huangdijia 03e3ba6
refactor(sentry): 重构事件处理逻辑,提取任务处理方法以提升代码可读性和维护性
huangdijia 93fca7f
fix(EventHandleListener): 将命令退出代码转换为字符串以确保一致性
huangdijia 10325eb
refactor(sentry): 将错误标记的布尔值从 true 转换为字符串 'true' 以确保一致性
huangdijia 82f56dc
fix: 将异常代码转换为字符串以确保一致性
huangdijia 7c1f50e
refactor(sentry): 优化上下文设置和数据处理逻辑以提高可读性
huangdijia 94a370c
refactor: 优化多个方面的代码结构和异常处理逻辑以提升可读性和一致性
huangdijia f69d5d5
refactor: 重命名数据库查询处理函数以提高一致性和可读性
huangdijia a9f85df
refactor: 统一事件处理类中的命名空间以提高一致性和可读性
huangdijia 9f1ed03
refactor: 统一Redis事件处理类中的命名空间以提高一致性和可读性
huangdijia b2f3495
refactor: 更新捕获异常方法的参数类型提示以提高代码可读性
huangdijia d020b98
refactor: 优化构造函数参数并简化异常处理中的日志记录
huangdijia 7ce77b2
refactor: 移除未使用的Redis命名空间导入以清理代码
huangdijia a2f1674
refactor: 将缺失的键集中到一个数组中以简化请求生命周期设置
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?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\Crons\Listener; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Constants; | ||
| use FriendsOfHyperf\Sentry\Switcher; | ||
| use Hyperf\Context\Context; | ||
| use Hyperf\Contract\ConfigInterface; | ||
| use Hyperf\Contract\StdoutLoggerInterface; | ||
| use Hyperf\Crontab\Event; | ||
| use Hyperf\Event\Contract\ListenerInterface; | ||
| use Sentry\CheckInStatus; | ||
| use Sentry\SentrySdk; | ||
|
|
||
| class EventHandleListener implements ListenerInterface | ||
| { | ||
| public function __construct( | ||
| protected ConfigInterface $config, | ||
| protected Switcher $switcher, | ||
| protected StdoutLoggerInterface $logger | ||
| ) { | ||
| } | ||
|
|
||
| public function listen(): array | ||
| { | ||
| return [ | ||
| Event\BeforeExecute::class, | ||
| Event\AfterExecute::class, | ||
| Event\FailToExecute::class, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Event\BeforeExecute|Event\AfterExecute|Event\FailToExecute $event | ||
| */ | ||
| public function process(object $event): void | ||
| { | ||
| if (! $this->switcher->isCronsEnable()) { | ||
| return; | ||
| } | ||
|
|
||
| $options = []; | ||
| if (method_exists($event->crontab, 'getOptions')) { | ||
| $options = $event->crontab->getOptions(); | ||
| } | ||
|
|
||
| if (isset($options['monitor']) && $options['monitor'] === false) { | ||
| return; | ||
| } | ||
|
|
||
| match ($event::class) { | ||
| Event\BeforeExecute::class => $this->handleCrontabTaskStarting($event, $options), | ||
| Event\AfterExecute::class => $this->handleCrontabTaskFinished($event), | ||
| Event\FailToExecute::class => $this->handleCrontabTaskFailed($event), | ||
| default => null, | ||
| }; | ||
| } | ||
|
|
||
| protected function handleCrontabTaskStarting(Event\BeforeExecute $event, array $options): void | ||
| { | ||
| $hub = SentrySdk::getCurrentHub(); | ||
| $slug = $event->crontab->getName(); | ||
| $rule = $event->crontab->getRule(); | ||
| $rules = explode(' ', $rule); | ||
|
|
||
| if (count($rules) > 5) { | ||
| $this->logger->warning(sprintf('Crontab rule %s is not supported by sentry', $rule)); | ||
| return; | ||
| } | ||
|
|
||
| $updateMonitorConfig = (bool) ($options['update_monitor_config'] ?? true); | ||
| $monitorConfig = null; | ||
|
|
||
| if ($updateMonitorConfig) { | ||
| $monitorConfig = $this->createMonitorConfig($event, $options, $rule); | ||
| } | ||
|
|
||
| $checkInId = $hub->captureCheckIn( | ||
| slug: $slug, | ||
| status: CheckInStatus::inProgress(), | ||
| monitorConfig: $monitorConfig, | ||
| ); | ||
|
|
||
| Context::set(Constants::CRON_CHECKIN_ID, $checkInId); | ||
| } | ||
|
|
||
| protected function handleCrontabTaskFinished(Event\AfterExecute $event): void | ||
| { | ||
| /** @var string $checkInId */ | ||
| $checkInId = Context::get(Constants::CRON_CHECKIN_ID); | ||
| if (! $checkInId) { | ||
| return; | ||
| } | ||
|
|
||
| $hub = SentrySdk::getCurrentHub(); | ||
| $slug = $event->crontab->getName(); | ||
|
|
||
| $hub->captureCheckIn( | ||
| slug: $slug, | ||
| status: CheckInStatus::ok(), | ||
| checkInId: $checkInId, | ||
| ); | ||
| } | ||
|
|
||
| protected function handleCrontabTaskFailed(Event\FailToExecute $event): void | ||
| { | ||
| /** @var string $checkInId */ | ||
| $checkInId = Context::get(Constants::CRON_CHECKIN_ID); | ||
| if (! $checkInId) { | ||
| return; | ||
| } | ||
|
|
||
| $hub = SentrySdk::getCurrentHub(); | ||
| $slug = $event->crontab->getName(); | ||
|
|
||
| $hub->captureCheckIn( | ||
| slug: $slug, | ||
| status: CheckInStatus::error(), | ||
| checkInId: $checkInId, | ||
| ); | ||
| } | ||
|
|
||
| protected function createMonitorConfig(Event\BeforeExecute $event, array $options, string $rule): \Sentry\MonitorConfig | ||
| { | ||
| $monitorSchedule = \Sentry\MonitorSchedule::crontab($rule); | ||
| $checkinMargin = (int) ($options['checkin_margin'] ?? $this->config->get('sentry.crons.checkin_margin', 5)); | ||
| $maxRuntime = (int) ($options['max_runtime'] ?? $this->config->get('sentry.crons.max_runtime', 15)); | ||
|
|
||
| $timezone = null; | ||
| if (method_exists($event->crontab, 'getTimezone')) { | ||
| $timezone = $event->crontab->getTimezone(); | ||
| } | ||
| $timezone ??= $this->config->get('sentry.crons.timezone', date_default_timezone_get()); | ||
|
|
||
| $failureIssueThreshold = isset($options['failure_issue_threshold']) ? (int) $options['failure_issue_threshold'] : null; | ||
| $recoveryThreshold = isset($options['recovery_threshold']) ? (int) $options['recovery_threshold'] : null; | ||
|
|
||
| return new \Sentry\MonitorConfig( | ||
| schedule: $monitorSchedule, | ||
| checkinMargin: $checkinMargin, | ||
| maxRuntime: $maxRuntime, | ||
| timezone: $timezone, | ||
| failureIssueThreshold: $failureIssueThreshold, | ||
| recoveryThreshold: $recoveryThreshold, | ||
| ); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.