From a4a8192b4e021a53ce317612a81b336ad921f0a0 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Tue, 10 Oct 2023 09:39:23 +0800 Subject: [PATCH] Added RpcAspect and use it instead of JsonRpcAspect (#6198) --- publish/opentracing.php | 11 ++-- src/Aspect/JsonRpcAspect.php | 86 ++--------------------------- src/Aspect/RpcAspect.php | 101 +++++++++++++++++++++++++++++++++++ src/ConfigProvider.php | 2 + 4 files changed, 115 insertions(+), 85 deletions(-) create mode 100644 src/Aspect/RpcAspect.php diff --git a/publish/opentracing.php b/publish/opentracing.php index 8b5b308..fabe1a0 100644 --- a/publish/opentracing.php +++ b/publish/opentracing.php @@ -16,11 +16,12 @@ return [ 'default' => env('TRACER_DRIVER', 'zipkin'), 'enable' => [ - 'guzzle' => env('TRACER_ENABLE_GUZZLE', false), - 'redis' => env('TRACER_ENABLE_REDIS', false), 'db' => env('TRACER_ENABLE_DB', false), - 'method' => env('TRACER_ENABLE_METHOD', false), 'exception' => env('TRACER_ENABLE_EXCEPTION', false), + 'guzzle' => env('TRACER_ENABLE_GUZZLE', false), + 'method' => env('TRACER_ENABLE_METHOD', false), + 'redis' => env('TRACER_ENABLE_REDIS', false), + 'rpc' => env('TRACER_ENABLE_RPC', false), 'ignore_exceptions' => [], ], 'tracer' => [ @@ -118,5 +119,9 @@ 'response' => [ 'status_code' => 'response.status_code', ], + 'rpc' => [ + 'path' => 'rpc.path', + 'status' => 'rpc.status', + ], ], ]; diff --git a/src/Aspect/JsonRpcAspect.php b/src/Aspect/JsonRpcAspect.php index 7d1599c..57d5be4 100644 --- a/src/Aspect/JsonRpcAspect.php +++ b/src/Aspect/JsonRpcAspect.php @@ -11,87 +11,9 @@ */ namespace Hyperf\Tracer\Aspect; -use Hyperf\Context\Context as CT; -use Hyperf\Di\Aop\AbstractAspect; -use Hyperf\Di\Aop\ProceedingJoinPoint; -use Hyperf\Rpc\Context; -use Hyperf\RpcClient\AbstractServiceClient; -use Hyperf\RpcClient\Client; -use Hyperf\Tracer\SpanStarter; -use Hyperf\Tracer\SpanTagManager; -use Hyperf\Tracer\SwitchManager; -use Hyperf\Tracer\TracerContext; -use OpenTracing\Span; -use Psr\Container\ContainerInterface; -use Throwable; - -use const OpenTracing\Formats\TEXT_MAP; - -class JsonRpcAspect extends AbstractAspect +/** + * @deprecated since v3.0, will be removed in v3.1. + */ +class JsonRpcAspect extends RpcAspect { - use SpanStarter; - - public array $classes = [ - AbstractServiceClient::class . '::__generateRpcPath', - Client::class . '::send', - ]; - - private SwitchManager $switchManager; - - private SpanTagManager $spanTagManager; - - private Context $context; - - public function __construct(private ContainerInterface $container) - { - $this->switchManager = $container->get(SwitchManager::class); - $this->spanTagManager = $container->get(SpanTagManager::class); - $this->context = $container->get(Context::class); - } - - public function process(ProceedingJoinPoint $proceedingJoinPoint) - { - if ($proceedingJoinPoint->methodName === '__generateRpcPath') { - $path = $proceedingJoinPoint->process(); - $key = "JsonRPC send [{$path}]"; - $span = $this->startSpan($key); - if ($this->spanTagManager->has('rpc', 'path')) { - $span->setTag($this->spanTagManager->get('rpc', 'path'), $path); - } - $carrier = []; - // Injects the context into the wire - TracerContext::getTracer()->inject( - $span->getContext(), - TEXT_MAP, - $carrier - ); - $this->context->set('tracer.carrier', $carrier); - CT::set('tracer.span.' . static::class, $span); - return $path; - } - - if ($proceedingJoinPoint->methodName === 'send') { - try { - $result = $proceedingJoinPoint->process(); - } catch (Throwable $e) { - 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); - } - throw $e; - } finally { - /** @var Span $span */ - if ($span = CT::get('tracer.span.' . static::class)) { - if ($this->spanTagManager->has('rpc', 'status')) { - $span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed'); - } - $span->finish(); - } - } - - return $result; - } - return $proceedingJoinPoint->process(); - } } diff --git a/src/Aspect/RpcAspect.php b/src/Aspect/RpcAspect.php new file mode 100644 index 0000000..e1a9a41 --- /dev/null +++ b/src/Aspect/RpcAspect.php @@ -0,0 +1,101 @@ +switchManager = $container->get(SwitchManager::class); + $this->spanTagManager = $container->get(SpanTagManager::class); + $this->context = $container->get(Context::class); + } + + public function process(ProceedingJoinPoint $proceedingJoinPoint) + { + if (static::class == self::class && $this->switchManager->isEnable('rpc') === false) { + return $proceedingJoinPoint->process(); + } + + if ($proceedingJoinPoint->methodName === '__generateRpcPath') { + $path = $proceedingJoinPoint->process(); + $key = "RPC send [{$path}]"; + $span = $this->startSpan($key); + if ($this->spanTagManager->has('rpc', 'path')) { + $span->setTag($this->spanTagManager->get('rpc', 'path'), $path); + } + $carrier = []; + // Injects the context into the wire + TracerContext::getTracer()->inject( + $span->getContext(), + TEXT_MAP, + $carrier + ); + $this->context->set('tracer.carrier', $carrier); + CT::set('tracer.span.' . static::class, $span); + return $path; + } + + if ($proceedingJoinPoint->methodName === 'send') { + try { + $result = $proceedingJoinPoint->process(); + } catch (Throwable $e) { + 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); + } + throw $e; + } finally { + /** @var Span $span */ + if ($span = CT::get('tracer.span.' . static::class)) { + if ($this->spanTagManager->has('rpc', 'status')) { + $span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed'); + } + $span->finish(); + } + } + + return $result; + } + return $proceedingJoinPoint->process(); + } +} diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 9ebb00b..616b96a 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -15,6 +15,7 @@ use Hyperf\Tracer\Aspect\CreateTraceContextAspect; use Hyperf\Tracer\Aspect\HttpClientAspect; use Hyperf\Tracer\Aspect\RedisAspect; +use Hyperf\Tracer\Aspect\RpcAspect; use Hyperf\Tracer\Aspect\TraceAnnotationAspect; use Hyperf\Tracer\Listener\DbQueryExecutedListener; use Jaeger\ThriftUdpTransport; @@ -49,6 +50,7 @@ public function __invoke(): array CreateTraceContextAspect::class, HttpClientAspect::class, RedisAspect::class, + RpcAspect::class, TraceAnnotationAspect::class, ], 'publish' => [