-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support json-rpc for tracer component. (#2625)
* Update SpanStarter.php * Added JsonRpcAspect. Co-authored-by: jimmy <[email protected]> Co-authored-by: 李铭昕 <[email protected]>
- Loading branch information
1 parent
dadb961
commit 1f1c56c
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters