From d43896583271e5a454762af4da7d8b8cf9e44621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=A3=E8=A8=80=E5=B0=B1=E6=98=AFSiam?= <59419979@qq.com> Date: Mon, 18 Sep 2023 20:44:58 +0800 Subject: [PATCH 1/3] Record the exception message only by opening the exception switch and closing the ignore switch when using `trace`. (#6157) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- src/Aspect/CoroutineAspect.php | 7 ++++++- src/Aspect/DbAspect.php | 6 ++++-- src/Aspect/ElasticserachAspect.php | 6 ++++-- src/Aspect/HttpClientAspect.php | 6 ++++-- src/Aspect/JsonRpcAspect.php | 2 +- src/Aspect/MethodAspect.php | 6 ++++-- src/Aspect/RedisAspect.php | 6 ++++-- src/Aspect/TraceAnnotationAspect.php | 9 ++++++--- 8 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Aspect/CoroutineAspect.php b/src/Aspect/CoroutineAspect.php index 557000c..96a381a 100644 --- a/src/Aspect/CoroutineAspect.php +++ b/src/Aspect/CoroutineAspect.php @@ -14,6 +14,7 @@ use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Engine\Coroutine as Co; +use Hyperf\Tracer\SwitchManager; use Hyperf\Tracer\TracerContext; use OpenTracing\Span; use Throwable; @@ -24,6 +25,10 @@ class CoroutineAspect extends AbstractAspect 'Hyperf\Coroutine\Coroutine::create', ]; + public function __construct(private SwitchManager $switchManager) + { + } + public function process(ProceedingJoinPoint $proceedingJoinPoint) { $callable = $proceedingJoinPoint->arguments['keys']['callable']; @@ -46,7 +51,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $callable(); } catch (Throwable $e) { - if (isset($child)) { + if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { $child->setTag('error', true); $child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); } diff --git a/src/Aspect/DbAspect.php b/src/Aspect/DbAspect.php index cea9ca0..701e2f2 100644 --- a/src/Aspect/DbAspect.php +++ b/src/Aspect/DbAspect.php @@ -46,8 +46,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); diff --git a/src/Aspect/ElasticserachAspect.php b/src/Aspect/ElasticserachAspect.php index 8cf21ce..7d1eac1 100644 --- a/src/Aspect/ElasticserachAspect.php +++ b/src/Aspect/ElasticserachAspect.php @@ -53,8 +53,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); diff --git a/src/Aspect/HttpClientAspect.php b/src/Aspect/HttpClientAspect.php index 93b3750..036873d 100644 --- a/src/Aspect/HttpClientAspect.php +++ b/src/Aspect/HttpClientAspect.php @@ -80,8 +80,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode()); } } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); diff --git a/src/Aspect/JsonRpcAspect.php b/src/Aspect/JsonRpcAspect.php index 07f2459..7d1599c 100644 --- a/src/Aspect/JsonRpcAspect.php +++ b/src/Aspect/JsonRpcAspect.php @@ -74,7 +74,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); } catch (Throwable $e) { - if ($span = CT::get('tracer.span.' . static::class)) { + if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { $span->setTag('error', true); $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); CT::set('tracer.span.' . static::class, $span); diff --git a/src/Aspect/MethodAspect.php b/src/Aspect/MethodAspect.php index bf6ce00..72f5832 100644 --- a/src/Aspect/MethodAspect.php +++ b/src/Aspect/MethodAspect.php @@ -43,8 +43,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); diff --git a/src/Aspect/RedisAspect.php b/src/Aspect/RedisAspect.php index 409e36e..0b537de 100644 --- a/src/Aspect/RedisAspect.php +++ b/src/Aspect/RedisAspect.php @@ -47,8 +47,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $result = $proceedingJoinPoint->process(); $span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result)); } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); diff --git a/src/Aspect/TraceAnnotationAspect.php b/src/Aspect/TraceAnnotationAspect.php index 4d9bf83..c17279a 100644 --- a/src/Aspect/TraceAnnotationAspect.php +++ b/src/Aspect/TraceAnnotationAspect.php @@ -15,6 +15,7 @@ use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Tracer\Annotation\Trace; use Hyperf\Tracer\SpanStarter; +use Hyperf\Tracer\SwitchManager; use Throwable; class TraceAnnotationAspect extends AbstractAspect @@ -25,7 +26,7 @@ class TraceAnnotationAspect extends AbstractAspect Trace::class, ]; - public function __construct() + public function __construct(private SwitchManager $switchManager) { } @@ -49,8 +50,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); } catch (Throwable $e) { - $span->setTag('error', true); - $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } throw $e; } finally { $span->finish(); From 0922d80a4b6721ba122b1ee17deef5f05a4a543b Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Tue, 19 Sep 2023 09:50:18 +0800 Subject: [PATCH 2/3] Upgrade actions/checkout to v4 (#6159) --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0f7d23f..4ebb5d0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Create Release id: create_release uses: actions/create-release@v1 From 67bed75e69dd6f69a4b84c1b9379951a9828e3e6 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Thu, 21 Sep 2023 12:06:36 +0800 Subject: [PATCH 3/3] Optimize `KafkaClientFactory` of `hyperf/tracer` (#6167) --- src/Adapter/Reporter/KafkaClientFactory.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Reporter/KafkaClientFactory.php b/src/Adapter/Reporter/KafkaClientFactory.php index 344080b..4a6cf8a 100644 --- a/src/Adapter/Reporter/KafkaClientFactory.php +++ b/src/Adapter/Reporter/KafkaClientFactory.php @@ -22,6 +22,8 @@ use Throwable; use Zipkin\Reporters\Http\ClientFactory; +use function Hyperf\Support\msleep; + class KafkaClientFactory implements ClientFactory { protected ?Channel $chan = null; @@ -93,7 +95,10 @@ protected function loop(): void try { $closure->call($this); } catch (Throwable) { - $this->producer->close(); + try { + $this->producer->close(); + } catch (Throwable) { + } break; } finally { $closure = null; @@ -106,6 +111,9 @@ protected function loop(): void Coroutine::create(function () { if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield()) { + while (! $this->chan->isEmpty()) { + msleep(100); + } $this->close(); } });