From fc29a30e61ac614d2d85b92e1a8049deee7b3937 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Tue, 15 Aug 2023 18:33:38 +0800 Subject: [PATCH 1/3] Allow using the tracer instance from context, append `Trace-Id` to Response Header (#6023) --- src/Middleware/TraceMiddleware.php | 5 ++++ src/SpanStarter.php | 17 ++++++------ src/TracerContext.php | 43 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/TracerContext.php diff --git a/src/Middleware/TraceMiddleware.php b/src/Middleware/TraceMiddleware.php index fe1a319..7b2c8f8 100644 --- a/src/Middleware/TraceMiddleware.php +++ b/src/Middleware/TraceMiddleware.php @@ -52,6 +52,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface }); try { $response = $handler->handle($request); + /** @var \ZipkinOpenTracing\SpanContext $spanContent */ + $spanContent = $span->getContext(); + /** @var \Zipkin\Propagation\TraceContext $traceContext */ + $traceContext = $spanContent->getContext(); + $response = $response->withHeader('Trace-Id', $traceContext->getTraceId()); $span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode()); } catch (Throwable $exception) { $this->switchManager->isEnable('exception') && $this->appendExceptionToSpan($span, $exception); diff --git a/src/SpanStarter.php b/src/SpanStarter.php index 7b9bb25..80de060 100644 --- a/src/SpanStarter.php +++ b/src/SpanStarter.php @@ -31,17 +31,18 @@ protected function startSpan( array $option = [], string $kind = SPAN_KIND_RPC_SERVER ): Span { - $root = Context::get('tracer.root'); + $root = TracerContext::getRoot(); + $tracer = TracerContext::getTracer() ?: $this->tracer; if (! $root instanceof Span) { $container = ApplicationContext::getContainer(); /** @var ServerRequestInterface $request */ $request = Context::get(ServerRequestInterface::class); if (! $request instanceof ServerRequestInterface) { - // If the request object is absent, we are probably in a commandline context. + // If the request object is absent, we are probably in a commandLine context. // Throwing an exception is unnecessary. - $root = $this->tracer->startSpan($name, $option); + $root = $tracer->startSpan($name, $option); $root->setTag(SPAN_KIND, $kind); - Context::set('tracer.root', $root); + TracerContext::setRoot($root); return $root; } $carrier = array_map(function ($header) { @@ -54,17 +55,17 @@ protected function startSpan( } } // Extracts the context from the HTTP headers. - $spanContext = $this->tracer->extract(TEXT_MAP, $carrier); + $spanContext = $tracer->extract(TEXT_MAP, $carrier); if ($spanContext) { $option['child_of'] = $spanContext; } - $root = $this->tracer->startSpan($name, $option); + $root = $tracer->startSpan($name, $option); $root->setTag(SPAN_KIND, $kind); - Context::set('tracer.root', $root); + TracerContext::setRoot($root); return $root; } $option['child_of'] = $root->getContext(); - $child = $this->tracer->startSpan($name, $option); + $child = $tracer->startSpan($name, $option); $child->setTag(SPAN_KIND, $kind); return $child; } diff --git a/src/TracerContext.php b/src/TracerContext.php new file mode 100644 index 0000000..0263d7f --- /dev/null +++ b/src/TracerContext.php @@ -0,0 +1,43 @@ + Date: Wed, 16 Aug 2023 09:43:28 +0800 Subject: [PATCH 2/3] Optimized the tracing in coroutine (#6027) 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 | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Aspect/CoroutineAspect.php diff --git a/src/Aspect/CoroutineAspect.php b/src/Aspect/CoroutineAspect.php new file mode 100644 index 0000000..55feb1c --- /dev/null +++ b/src/Aspect/CoroutineAspect.php @@ -0,0 +1,66 @@ +arguments['keys']['callable']; + $root = TracerContext::getRoot(); + + $proceedingJoinPoint->arguments['keys']['callable'] = function () use ($callable, $root) { + try { + if ($root instanceof Span) { + /** @var Tracer $tracer */ + $tracer = make(Tracer::class); + TracerContext::setTracer($tracer); + $child = $tracer->startSpan('coroutine', [ + 'child_of' => $root->getContext(), + ]); + $child->setTag('coroutine.id', Co::id()); + TracerContext::setRoot($child); + Co::defer(function () use ($child, $tracer) { + $child->finish(); + $tracer->flush(); + }); + } + + $callable(); + } catch (Throwable $e) { + if (isset($child)) { + $child->setTag('error', true); + $child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); + } + + throw $e; + } + }; + + return $proceedingJoinPoint->process(); + } +} From 8bdd7842febd8d020987b7586cdd6d19b45a6bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9F=8E=E9=93=AD?= Date: Thu, 17 Aug 2023 14:16:35 +0800 Subject: [PATCH 3/3] Make `Hyperf\Coroutine\Traits\Container` as deprecated. (#6044) 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 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Aspect/CoroutineAspect.php b/src/Aspect/CoroutineAspect.php index 55feb1c..31a27d6 100644 --- a/src/Aspect/CoroutineAspect.php +++ b/src/Aspect/CoroutineAspect.php @@ -19,7 +19,6 @@ use OpenTracing\Tracer; use Throwable; -use function Hyperf\Support\call; use function Hyperf\Support\make; class CoroutineAspect extends AbstractAspect