Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/sentry/src/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry\Aspect;

use FriendsOfHyperf\Sentry\Integration;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Engine\Coroutine as Co;
Expand Down Expand Up @@ -49,6 +50,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
} catch (Throwable $throwable) {
SentrySdk::getCurrentHub()->captureException($throwable);
throw $throwable;
} finally {
Integration::flushEvents();
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/sentry/src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\Integration\IntegrationInterface;
use Sentry\Logs\Logs;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -108,6 +109,8 @@ public static function flushEvents(): void

if ($client !== null) {
$client->flush();

Logs::getInstance()->flush();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

🧩 Analysis chain

需要验证 Logs 类的导入和使用是否正确

添加了 Logs::getInstance()->flush() 调用,但需要确认:

  1. Sentry\Logs\Logs 类是否在当前使用的 Sentry SDK 版本中存在
  2. 该类是否提供了 getInstance()flush() 方法

🏁 Script executed:

#!/bin/bash
# 检查 composer.json 中的 Sentry SDK 版本
cat composer.json | jq -r '.require | to_entries[] | select(.key | contains("sentry")) | "\(.key): \(.value)"'

# 搜索项目中是否有其他地方使用了 Logs 类
rg -n "Sentry\\\\Logs\\\\Logs" --type php

# 检查是否有 Logs 相关的配置或初始化
rg -n "Logs::getInstance\(\)" --type php

Length of output: 155


🌐 Web query:

Sentry PHP SDK Logs class documentation

💡 Result:

Here are the official docs for the Sentry PHP SDK logs API / Log class:

  • Sentry PHP — Logs (how to enable and use \Sentry\Logs\Log in the SDK; includes examples and before_send_log signature). [1]
  • Sentry SDK developer docs — Logs protocol and SDK logging requirements (spec for log envelope / SDK API). [2]
  • getsentry/sentry-php GitHub repository (official PHP SDK source & code). [3]

References
[1] https://docs.sentry.io/platforms/php/logs/
[2] https://develop.sentry.dev/sdk/telemetry/logs/
[3] https://github.com/getsentry/sentry-php


必须修复:确认并修正 Sentry Logs 的导入与用法(会导致运行时错误)

  • 现状:composer.json 中使用 sentry/sentry: ^4.15.0;文件 src/sentry/src/Integration.php 存在 use Sentry\Logs\Logs;(第17行),并在第113行调用 Logs::getInstance()->flush();
  • 问题:官方文档/SDK 资料中提及的是 \Sentry\Logs\Log(单数),未见 Logs::getInstance()flush() 这类单例 API,当前导入/调用很可能不存在且会在运行时报错。
  • 要求:替换为 SDK 支持的日志接口或正确的类名/命名空间;若这是自定义实现,请在仓库中添加该类定义并更新依赖说明;否则移除该调用或改用 SDK 提供的正确“flush”机制。

}
}

Expand Down
9 changes: 8 additions & 1 deletion src/sentry/src/Listener/AmqpExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public function listen(): array
{
return [
Event\BeforeConsume::class,
Event\AfterConsume::class,
Event\FailToConsume::class,
];
}

/**
* @param Event\FailToConsume $event
* @param Event\FailToConsume|Event\AfterConsume|Event\BeforeConsume|object $event
*/
public function process(object $event): void
{
Expand All @@ -36,5 +37,11 @@ public function process(object $event): void
Event\FailToConsume::class => $this->captureException($event->getThrowable()),
default => $this->setupSentrySdk(),
};

match ($event::class) {
Event\AfterConsume::class,
Event\FailToConsume::class => $this->flushEvents(),
default => null,
};
}
}
12 changes: 11 additions & 1 deletion src/sentry/src/Listener/AsyncQueueExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public function listen(): array
{
return [
Event\BeforeHandle::class,
Event\AfterHandle::class,
Event\RetryHandle::class,
Event\FailedHandle::class,
];
}

/**
* @param Event\FailedHandle $event
* @param Event\FailedHandle|Event\AfterHandle|Event\BeforeHandle|object $event
*/
public function process(object $event): void
{
Expand All @@ -33,8 +35,16 @@ public function process(object $event): void
}

match ($event::class) {
Event\RetryHandle::class,
Event\FailedHandle::class => $this->captureException($event->getThrowable()),
default => $this->setupSentrySdk(),
};
Comment on lines +38 to 41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

🧩 Analysis chain

确认 RetryHandle 是否恒定提供 getThrowable();建议以 instanceof + 守卫式调用更稳妥

RetryHandle 在部分实现中不包含 getThrowable(),当前直接调用将导致致命错误。建议改为 instanceof 并在 RetryHandle 分支做方法存在性守卫:

-        match ($event::class) {
-            Event\RetryHandle::class,
-            Event\FailedHandle::class => $this->captureException($event->getThrowable()),
-            default => $this->setupSentrySdk(),
-        };
+        match (true) {
+            $event instanceof Event\BeforeHandle => $this->setupSentrySdk(),
+            $event instanceof Event\RetryHandle => method_exists($event, 'getThrowable') ? $this->captureException($event->getThrowable()) : null,
+            $event instanceof Event\FailedHandle => $this->captureException($event->getThrowable()),
+            default => null,
+        };

为稳妥,请核实框架事件定义:


🌐 Web query:

Hyperf AsyncQueue Event RetryHandle 是否提供 getThrowable() 方法?

💡 Result:

短答:没有。RetryHandle 事件类并不带 getThrowable() 方法(文档/示例中只有 FailedHandle 使用 event->getThrowable(),RetryHandle 只是标记“重试前”事件)。[1][2]

来源:

  • Hyperf 异步队列事件文档(列出 RetryHandle/FailedHandle). [1]
  • 示例代码:FailedHandle 调用 getThrowable(),RetryHandle 未调用(示例监听器)。[2]

必须修复:RetryHandle 不提供 getThrowable(),当前实现会抛致命错误

已确认:Hyperf 的 RetryHandle 事件类不包含 getThrowable()(文档/示例);在 Retry 事件上直接调用 $event->getThrowable() 会触发运行时错误。

文件:src/sentry/src/Listener/AsyncQueueExceptionListener.php 行 38-41

-        match ($event::class) {
-            Event\RetryHandle::class,
-            Event\FailedHandle::class => $this->captureException($event->getThrowable()),
-            default => $this->setupSentrySdk(),
-        };
+        match (true) {
+            $event instanceof Event\BeforeHandle => $this->setupSentrySdk(),
+            $event instanceof Event\RetryHandle => method_exists($event, 'getThrowable') ? $this->captureException($event->getThrowable()) : null,
+            $event instanceof Event\FailedHandle => $this->captureException($event->getThrowable()),
+            default => null,
+        };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Event\RetryHandle::class,
Event\FailedHandle::class => $this->captureException($event->getThrowable()),
default => $this->setupSentrySdk(),
};
match (true) {
$event instanceof Event\BeforeHandle => $this->setupSentrySdk(),
$event instanceof Event\RetryHandle => method_exists($event, 'getThrowable') ? $this->captureException($event->getThrowable()) : null,
$event instanceof Event\FailedHandle => $this->captureException($event->getThrowable()),
default => null,
};


match ($event::class) {
Event\AfterHandle::class,
Event\RetryHandle::class,
Event\FailedHandle::class => $this->flushEvents(),
default => null,
};
}
}
6 changes: 6 additions & 0 deletions src/sentry/src/Listener/CaptureExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry\Listener;

use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Switcher;
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
Expand Down Expand Up @@ -60,4 +61,9 @@ protected function setupSentrySdk(): void
SentrySdk::init();
Context::set(static::SETUP, true);
}

protected function flushEvents(): void
{
Integration::flushEvents();
}
}
4 changes: 3 additions & 1 deletion src/sentry/src/Listener/CommandExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function listen(): array
return [
Event\BeforeHandle::class,
Event\FailToHandle::class,
Event\AfterExecute::class,
];
}

/**
* @param Event\FailToHandle $event
* @param Event\FailToHandle|Event\BeforeHandle|Event\AfterExecute|object $event
*/
public function process(object $event): void
{
Expand All @@ -34,6 +35,7 @@ public function process(object $event): void

match ($event::class) {
Event\FailToHandle::class => $this->captureException($event->getThrowable()),
Event\AfterExecute::class => $this->flushEvents(),
default => $this->setupSentrySdk(),
};
}
Expand Down
9 changes: 8 additions & 1 deletion src/sentry/src/Listener/CrontabExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public function listen(): array
{
return [
Event\BeforeExecute::class,
Event\AfterExecute::class,
Event\FailToExecute::class,
];
}

/**
* @param Event\FailToExecute $event
* @param Event\FailToExecute|Event\AfterExecute|Event\BeforeExecute|object $event
*/
public function process(object $event): void
{
Expand All @@ -41,5 +42,11 @@ public function process(object $event): void
Event\FailToExecute::class => $this->captureException($event->throwable),
default => $this->setupSentrySdk(),
};

match ($event::class) {
Event\AfterExecute::class,
Event\FailToExecute::class => $this->flushEvents(),
default => null,
};
}
}
9 changes: 8 additions & 1 deletion src/sentry/src/Listener/KafkaExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function listen(): array
return [
Event\BeforeConsume::class,
Event\FailToConsume::class,
Event\AfterConsume::class,
];
}

/**
* @param Event\FailToConsume $event
* @param Event\FailToConsume|Event\AfterConsume|Event\BeforeConsume|object $event
*/
public function process(object $event): void
{
Expand All @@ -36,5 +37,11 @@ public function process(object $event): void
Event\FailToConsume::class => $this->captureException($event->getThrowable()),
default => $this->setupSentrySdk(),
};

match ($event::class) {
Event\AfterConsume::class,
Event\FailToConsume::class => $this->flushEvents(),
default => null,
};
}
}
8 changes: 7 additions & 1 deletion src/sentry/src/Listener/RequestExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function listen(): array
}

/**
* @param RequestTerminated|object $event
* @param RequestTerminated|RpcRequestTerminated|object $event
*/
public function process(object $event): void
{
Expand All @@ -41,5 +41,11 @@ public function process(object $event): void
RequestTerminated::class, RpcRequestTerminated::class => $this->captureException($event->exception),
default => $this->setupSentrySdk(),
};

match ($event::class) {
RequestTerminated::class,
RpcRequestTerminated::class => $this->flushEvents(),
default => null,
};
}
}
Loading