Skip to content

Commit

Permalink
Support json-rpc for tracer component. (#2625)
Browse files Browse the repository at this point in the history
* Update SpanStarter.php
* Added JsonRpcAspect.

Co-authored-by: jimmy <[email protected]>
Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2020
1 parent dadb961 commit 1f1c56c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/Aspect/JsonRpcAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Tracer\Aspect;

use Hyperf\Di\Aop\AroundInterface;
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\Utils\Context as CT;
use OpenTracing\Tracer;
use Psr\Container\ContainerInterface;
use Zipkin\Span;
use const OpenTracing\Formats\TEXT_MAP;

class JsonRpcAspect implements AroundInterface
{
use SpanStarter;

public $classes = [
AbstractServiceClient::class . '::__generateRpcPath',
Client::class . '::send',
];

/**
* @var ContainerInterface
*/
private $container;

/**
* @var Tracer
*/
private $tracer;

/**
* @var SwitchManager
*/
private $switchManager;

/**
* @var SpanTagManager
*/
private $spanTagManager;

/**
* @var Context
*/
private $context;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->tracer = $container->get(Tracer::class);
$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
$this->tracer->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') {
$result = $proceedingJoinPoint->process();
/** @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();
}
}
9 changes: 9 additions & 0 deletions src/SpanStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
namespace Hyperf\Tracer;

use Hyperf\Rpc;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use OpenTracing\Span;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -30,6 +32,7 @@ protected function startSpan(
): Span {
$root = Context::get('tracer.root');
if (! $root instanceof Span) {
$container = ApplicationContext::getContainer();
/** @var ServerRequestInterface $request */
$request = Context::get(ServerRequestInterface::class);
if (! $request instanceof ServerRequestInterface) {
Expand All @@ -38,6 +41,12 @@ protected function startSpan(
$carrier = array_map(function ($header) {
return $header[0];
}, $request->getHeaders());
if ($container->has(Rpc\Context::class) && $rpcContext = $container->get(Rpc\Context::class)) {
$rpcCarrier = $rpcContext->get('tracer.carrier');
if (! empty($rpcCarrier)) {
$carrier = $rpcCarrier;
}
}
// Extracts the context from the HTTP headers.
$spanContext = $this->tracer->extract(TEXT_MAP, $carrier);
if ($spanContext) {
Expand Down
4 changes: 4 additions & 0 deletions src/SpanTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SpanTagManager
'db.statement' => 'db.sql',
'db.query_time' => 'db.query_time',
],
'rpc' => [
'path' => 'rpc.path',
'status' => 'rpc.status',
],
];

public function apply(array $tags): void
Expand Down

0 comments on commit 1f1c56c

Please sign in to comment.