mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Added RpcAspect and use it instead of JsonRpcAspect (#6198)
This commit is contained in:
parent
357afa6803
commit
71312060c7
@ -4,6 +4,7 @@
|
||||
|
||||
- [#6188](https://github.com/hyperf/hyperf/pull/6188) Added Redis options name to support string type.
|
||||
- [#6193](https://github.com/hyperf/hyperf/pull/6193) Support http and websocket protocols at the same port for swow server.
|
||||
- [#6198](https://github.com/hyperf/hyperf/pull/6198) Added `RpcAspect` and use it instead of `JsonRpcAspect`.
|
||||
|
||||
# v3.0.38 - 2023-10-05
|
||||
|
||||
|
@ -16,11 +16,12 @@ use function Hyperf\Support\env;
|
||||
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 @@ return [
|
||||
'response' => [
|
||||
'status_code' => 'response.status_code',
|
||||
],
|
||||
'rpc' => [
|
||||
'path' => 'rpc.path',
|
||||
'status' => 'rpc.status',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
@ -11,87 +11,9 @@ declare(strict_types=1);
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
101
src/tracer/src/Aspect/RpcAspect.php
Normal file
101
src/tracer/src/Aspect/RpcAspect.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
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 RpcAspect extends AbstractAspect
|
||||
{
|
||||
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 (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();
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ use GuzzleHttp\Client;
|
||||
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 @@ class ConfigProvider
|
||||
CreateTraceContextAspect::class,
|
||||
HttpClientAspect::class,
|
||||
RedisAspect::class,
|
||||
RpcAspect::class,
|
||||
TraceAnnotationAspect::class,
|
||||
],
|
||||
'publish' => [
|
||||
|
Loading…
Reference in New Issue
Block a user