From 0e5ac0715e96d233bf1bd64c1ee6c7067d32fa90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=B7=E6=BA=AA?= Date: Sun, 27 Dec 2020 10:38:09 +0800 Subject: [PATCH] fix: log and tag Throwable in a span (#3011) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: log and tag Throwable in a span * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <715557344@qq.com> --- src/Aspect/HttpClientAspect.php | 4 ++++ src/Aspect/JsonRpcAspect.php | 7 +++++++ src/Aspect/MethodAspect.php | 4 ++++ src/Aspect/RedisAspect.php | 4 ++++ src/Aspect/TraceAnnotationAspect.php | 4 ++++ 5 files changed, 23 insertions(+) diff --git a/src/Aspect/HttpClientAspect.php b/src/Aspect/HttpClientAspect.php index 805f1de..2e78c14 100644 --- a/src/Aspect/HttpClientAspect.php +++ b/src/Aspect/HttpClientAspect.php @@ -97,6 +97,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($result instanceof ResponseInterface) { $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()]); + throw $e; } finally { $span->finish(); } diff --git a/src/Aspect/JsonRpcAspect.php b/src/Aspect/JsonRpcAspect.php index 218e5da..a81c936 100644 --- a/src/Aspect/JsonRpcAspect.php +++ b/src/Aspect/JsonRpcAspect.php @@ -92,6 +92,13 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($proceedingJoinPoint->methodName === 'send') { try { $result = $proceedingJoinPoint->process(); + } catch (\Throwable $e) { + if ($span = CT::get('tracer.span.' . static::class)) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + CT::set('tracer.span.' . static::class, $span); + } + throw $e; } finally { /** @var Span $span */ if ($span = CT::get('tracer.span.' . static::class)) { diff --git a/src/Aspect/MethodAspect.php b/src/Aspect/MethodAspect.php index 18df9f8..d806ae6 100644 --- a/src/Aspect/MethodAspect.php +++ b/src/Aspect/MethodAspect.php @@ -60,6 +60,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $span = $this->startSpan($key); try { $result = $proceedingJoinPoint->process(); + } catch (\Throwable $e) { + $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 3c22714..d0042db 100644 --- a/src/Aspect/RedisAspect.php +++ b/src/Aspect/RedisAspect.php @@ -77,6 +77,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $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()]); + throw $e; } finally { $span->finish(); } diff --git a/src/Aspect/TraceAnnotationAspect.php b/src/Aspect/TraceAnnotationAspect.php index 46ea182..c30e39e 100644 --- a/src/Aspect/TraceAnnotationAspect.php +++ b/src/Aspect/TraceAnnotationAspect.php @@ -60,6 +60,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $span->setTag($tag, $source); try { $result = $proceedingJoinPoint->process(); + } catch (\Throwable $e) { + $span->setTag('error', true); + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + throw $e; } finally { $span->finish(); }