|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace Hyperf\Tracer\Aspect; |
| 13 | + |
| 14 | +use Hyperf\Context\Context as CT; |
| 15 | +use Hyperf\Di\Aop\AbstractAspect; |
| 16 | +use Hyperf\Di\Aop\ProceedingJoinPoint; |
| 17 | +use Hyperf\Rpc\Context; |
| 18 | +use Hyperf\RpcClient\AbstractServiceClient; |
| 19 | +use Hyperf\RpcClient\Client; |
| 20 | +use Hyperf\Tracer\SpanStarter; |
| 21 | +use Hyperf\Tracer\SpanTagManager; |
| 22 | +use Hyperf\Tracer\SwitchManager; |
| 23 | +use Hyperf\Tracer\TracerContext; |
| 24 | +use OpenTracing\Span; |
| 25 | +use Psr\Container\ContainerInterface; |
| 26 | +use Throwable; |
| 27 | + |
| 28 | +use const OpenTracing\Formats\TEXT_MAP; |
| 29 | + |
| 30 | +class RpcAspect extends AbstractAspect |
| 31 | +{ |
| 32 | + use SpanStarter; |
| 33 | + |
| 34 | + public array $classes = [ |
| 35 | + AbstractServiceClient::class . '::__generateRpcPath', |
| 36 | + Client::class . '::send', |
| 37 | + ]; |
| 38 | + |
| 39 | + private SwitchManager $switchManager; |
| 40 | + |
| 41 | + private SpanTagManager $spanTagManager; |
| 42 | + |
| 43 | + private Context $context; |
| 44 | + |
| 45 | + public function __construct(private ContainerInterface $container) |
| 46 | + { |
| 47 | + $this->switchManager = $container->get(SwitchManager::class); |
| 48 | + $this->spanTagManager = $container->get(SpanTagManager::class); |
| 49 | + $this->context = $container->get(Context::class); |
| 50 | + } |
| 51 | + |
| 52 | + public function process(ProceedingJoinPoint $proceedingJoinPoint) |
| 53 | + { |
| 54 | + if (static::class == self::class && $this->switchManager->isEnable('rpc') === false) { |
| 55 | + return $proceedingJoinPoint->process(); |
| 56 | + } |
| 57 | + |
| 58 | + if ($proceedingJoinPoint->methodName === '__generateRpcPath') { |
| 59 | + $path = $proceedingJoinPoint->process(); |
| 60 | + $key = "RPC send [{$path}]"; |
| 61 | + $span = $this->startSpan($key); |
| 62 | + if ($this->spanTagManager->has('rpc', 'path')) { |
| 63 | + $span->setTag($this->spanTagManager->get('rpc', 'path'), $path); |
| 64 | + } |
| 65 | + $carrier = []; |
| 66 | + // Injects the context into the wire |
| 67 | + TracerContext::getTracer()->inject( |
| 68 | + $span->getContext(), |
| 69 | + TEXT_MAP, |
| 70 | + $carrier |
| 71 | + ); |
| 72 | + $this->context->set('tracer.carrier', $carrier); |
| 73 | + CT::set('tracer.span.' . static::class, $span); |
| 74 | + return $path; |
| 75 | + } |
| 76 | + |
| 77 | + if ($proceedingJoinPoint->methodName === 'send') { |
| 78 | + try { |
| 79 | + $result = $proceedingJoinPoint->process(); |
| 80 | + } catch (Throwable $e) { |
| 81 | + if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) { |
| 82 | + $span->setTag('error', true); |
| 83 | + $span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); |
| 84 | + CT::set('tracer.span.' . static::class, $span); |
| 85 | + } |
| 86 | + throw $e; |
| 87 | + } finally { |
| 88 | + /** @var Span $span */ |
| 89 | + if ($span = CT::get('tracer.span.' . static::class)) { |
| 90 | + if ($this->spanTagManager->has('rpc', 'status')) { |
| 91 | + $span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed'); |
| 92 | + } |
| 93 | + $span->finish(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $result; |
| 98 | + } |
| 99 | + return $proceedingJoinPoint->process(); |
| 100 | + } |
| 101 | +} |
0 commit comments