Skip to content
Merged
Show file tree
Hide file tree
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 Sep 20, 2025
30c241d
feat(sentry): replace EventListener with EventHandleListener and upda…
huangdijia Sep 20, 2025
365a7be
feat(sentry): replace CronEventListener with EventHandleListener and …
huangdijia Sep 20, 2025
e22420d
feat(sentry): add EventHandleListener for unified event handling and …
huangdijia Sep 20, 2025
47f9105
refactor(sentry): 删除多个过时的事件监听器以简化代码结构
huangdijia Sep 20, 2025
0c0d8c9
fix(sentry): 添加异常处理以记录 flushEvents 方法中的错误
huangdijia Sep 20, 2025
d5ba2e0
feat(sentry): 添加缓存追踪选项以增强性能监控
huangdijia Sep 20, 2025
1b4391b
refactor(sentry): 合并异步队列重试和失败处理方法,并添加事件参数注释
huangdijia Sep 20, 2025
477d303
refactor(sentry): 重命名处理 Redis 命令的方法以提高可读性
huangdijia Sep 20, 2025
a95a00a
refactor(sentry): 更新常量名称以反映 Sentry Hub 的上下文
huangdijia Sep 20, 2025
0b727b5
fix(sentry): 在处理异常时添加 flushEvents 方法调用以确保事件被正确刷新
huangdijia Sep 20, 2025
67d1f1f
fix(sentry): 在异常处理时根据配置条件添加堆栈跟踪数据
huangdijia Sep 20, 2025
9a77ca8
refactor(sentry): 优化事务数据和标签的设置逻辑,提升代码可读性
huangdijia Sep 20, 2025
862386d
refactor(sentry): 合并设置事务数据和标签的逻辑以提升代码简洁性
huangdijia Sep 20, 2025
4b98d58
refactor(sentry): 简化 Redis 和 AMQP 事件处理中的数据设置逻辑,提升代码可读性
huangdijia Sep 20, 2025
d1567d0
refactor(sentry): 重命名事件处理方法以提高可读性和一致性
huangdijia Sep 20, 2025
03e3ba6
refactor(sentry): 重构事件处理逻辑,提取任务处理方法以提升代码可读性和维护性
huangdijia Sep 20, 2025
93fca7f
fix(EventHandleListener): 将命令退出代码转换为字符串以确保一致性
huangdijia Sep 20, 2025
10325eb
refactor(sentry): 将错误标记的布尔值从 true 转换为字符串 'true' 以确保一致性
huangdijia Sep 20, 2025
82f56dc
fix: 将异常代码转换为字符串以确保一致性
huangdijia Sep 20, 2025
7c1f50e
refactor(sentry): 优化上下文设置和数据处理逻辑以提高可读性
huangdijia Sep 20, 2025
94a370c
refactor: 优化多个方面的代码结构和异常处理逻辑以提升可读性和一致性
huangdijia Sep 20, 2025
f69d5d5
refactor: 重命名数据库查询处理函数以提高一致性和可读性
huangdijia Sep 20, 2025
a9f85df
refactor: 统一事件处理类中的命名空间以提高一致性和可读性
huangdijia Sep 20, 2025
9f1ed03
refactor: 统一Redis事件处理类中的命名空间以提高一致性和可读性
huangdijia Sep 20, 2025
b2f3495
refactor: 更新捕获异常方法的参数类型提示以提高代码可读性
huangdijia Sep 20, 2025
d020b98
refactor: 优化构造函数参数并简化异常处理中的日志记录
huangdijia Sep 20, 2025
7ce77b2
refactor: 移除未使用的Redis命名空间导入以清理代码
huangdijia Sep 20, 2025
a2f1674
refactor: 将缺失的键集中到一个数组中以简化请求生命周期设置
huangdijia Sep 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"Laozi",
"Laravel",
"Livewire",
"longlang",
"Macroable",
"Mailables",
"Mailgun",
Expand All @@ -57,6 +58,7 @@
"openai",
"overtrue",
"phpize",
"phpkafka",
"phpredis",
"pmessage",
"preprod",
Expand Down
22 changes: 3 additions & 19 deletions src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,9 @@ public function __invoke(): array
\Sentry\Transport\TransportInterface::class => Transport\CoHttpTransport::class,
],
'listeners' => [
Listener\AmqpExceptionListener::class,
Listener\AsyncQueueExceptionListener::class,
Listener\CommandExceptionListener::class,
Listener\CrontabExceptionListener::class,
Listener\DbQueryListener::class,
Listener\KafkaExceptionListener::class,
Listener\RedisCommandExecutedListener::class,
Listener\RequestExceptionListener::class,
Listener\SetRedisEventEnableListener::class,
Listener\SetRequestLifecycleListener::class,
Crons\Listener\CronEventListener::class,
Tracing\Listener\TracingAmqpListener::class,
Tracing\Listener\TracingAsyncQueueListener::class,
Tracing\Listener\TracingCommandListener::class,
Tracing\Listener\TracingCrontabListener::class,
Tracing\Listener\TracingDbQueryListener::class,
Tracing\Listener\TracingKafkaListener::class,
Tracing\Listener\TracingRedisListener::class,
Tracing\Listener\TracingRequestListener::class,
Listener\EventHandleListener::class,
Crons\Listener\EventHandleListener::class,
Tracing\Listener\EventHandleListener::class,
],
'annotations' => [
'scan' => [
Expand Down
135 changes: 0 additions & 135 deletions src/sentry/src/Crons/Listener/CronEventListener.php

This file was deleted.

156 changes: 156 additions & 0 deletions src/sentry/src/Crons/Listener/EventHandleListener.php
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,
);
}
}
47 changes: 0 additions & 47 deletions src/sentry/src/Listener/AmqpExceptionListener.php

This file was deleted.

Loading