From fa922b727f0e4e0e624527fb95520df84d13ffd2 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Fri, 29 Jan 2021 20:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=93=BE=E8=B7=AF=E8=BF=BD?= =?UTF-8?q?=E8=B8=AA=E4=B8=AD=E9=97=B4=E4=BB=B6=EF=BC=8C=E9=9C=80=E5=AE=89?= =?UTF-8?q?=E8=A3=85think-tracing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- src/middleware/TraceRpcClient.php | 46 +++++++++++++++++++++++++++++++ src/middleware/TraceRpcServer.php | 46 +++++++++++++++++++++++++++++++ src/rpc/client/Gateway.php | 2 +- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/middleware/TraceRpcClient.php create mode 100644 src/middleware/TraceRpcServer.php diff --git a/composer.json b/composer.json index 3c7d115..9f35711 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "topthink/framework": "^6.0" }, "require-dev": { - "symfony/var-dumper": "^4.3|^5.1" + "symfony/var-dumper": "^4.3|^5.1", + "topthink/think-tracing": "^1.0" }, "autoload": { "psr-4": { diff --git a/src/middleware/TraceRpcClient.php b/src/middleware/TraceRpcClient.php new file mode 100644 index 0000000..8469db6 --- /dev/null +++ b/src/middleware/TraceRpcClient.php @@ -0,0 +1,46 @@ +tracer = $tracer; + } + + public function handle(Protocol $protocol, $next) + { + $scope = $this->tracer->startActiveSpan( + 'rpc.client:' . $protocol->getInterface() . '@' . $protocol->getMethod(), + [ + 'tags' => [ + SPAN_KIND => SPAN_KIND_RPC_CLIENT, + ], + ] + ); + $span = $scope->getSpan(); + $context = $protocol->getContext(); + $this->tracer->inject($span->getContext(), TEXT_MAP, $context); + $protocol->setContext($context); + + try { + return $next($protocol); + } catch (Throwable $e) { + $span->setTag(ERROR, $e); + throw $e; + } finally { + $scope->close(); + } + } +} diff --git a/src/middleware/TraceRpcServer.php b/src/middleware/TraceRpcServer.php new file mode 100644 index 0000000..b5e6cae --- /dev/null +++ b/src/middleware/TraceRpcServer.php @@ -0,0 +1,46 @@ +tracer = $tracer; + } + + public function handle(Protocol $protocol, $next) + { + $context = $this->tracer->extract(TEXT_MAP, $protocol->getContext()); + $scope = $this->tracer->startActiveSpan( + 'rpc.server:' . $protocol->getInterface() . '@' . $protocol->getMethod(), + [ + 'child_of' => $context, + 'tags' => [ + SPAN_KIND => SPAN_KIND_RPC_SERVER, + ], + ] + ); + $span = $scope->getSpan(); + + try { + return $next($protocol); + } catch (Throwable $e) { + $span->setTag(ERROR, $e); + throw $e; + } finally { + $scope->close(); + $this->tracer->flush(); + } + } +} diff --git a/src/rpc/client/Gateway.php b/src/rpc/client/Gateway.php index 7a0d8c7..42c50ca 100644 --- a/src/rpc/client/Gateway.php +++ b/src/rpc/client/Gateway.php @@ -52,7 +52,7 @@ protected function encodeData(Protocol $protocol) } } - $protocol = Protocol::make($protocol->getInterface(), $protocol->getMethod(), $params); + $protocol->setParams($params); $data = $this->parser->encode($protocol);