Skip to content

Commit a4a8192

Browse files
authored
Added RpcAspect and use it instead of JsonRpcAspect (#6198)
1 parent 67bed75 commit a4a8192

File tree

4 files changed

+115
-85
lines changed

4 files changed

+115
-85
lines changed

publish/opentracing.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
return [
1717
'default' => env('TRACER_DRIVER', 'zipkin'),
1818
'enable' => [
19-
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
20-
'redis' => env('TRACER_ENABLE_REDIS', false),
2119
'db' => env('TRACER_ENABLE_DB', false),
22-
'method' => env('TRACER_ENABLE_METHOD', false),
2320
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
21+
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
22+
'method' => env('TRACER_ENABLE_METHOD', false),
23+
'redis' => env('TRACER_ENABLE_REDIS', false),
24+
'rpc' => env('TRACER_ENABLE_RPC', false),
2425
'ignore_exceptions' => [],
2526
],
2627
'tracer' => [
@@ -118,5 +119,9 @@
118119
'response' => [
119120
'status_code' => 'response.status_code',
120121
],
122+
'rpc' => [
123+
'path' => 'rpc.path',
124+
'status' => 'rpc.status',
125+
],
121126
],
122127
];

src/Aspect/JsonRpcAspect.php

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,9 @@
1111
*/
1212
namespace Hyperf\Tracer\Aspect;
1313

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 JsonRpcAspect extends AbstractAspect
14+
/**
15+
* @deprecated since v3.0, will be removed in v3.1.
16+
*/
17+
class JsonRpcAspect extends RpcAspect
3118
{
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 ($proceedingJoinPoint->methodName === '__generateRpcPath') {
55-
$path = $proceedingJoinPoint->process();
56-
$key = "JsonRPC send [{$path}]";
57-
$span = $this->startSpan($key);
58-
if ($this->spanTagManager->has('rpc', 'path')) {
59-
$span->setTag($this->spanTagManager->get('rpc', 'path'), $path);
60-
}
61-
$carrier = [];
62-
// Injects the context into the wire
63-
TracerContext::getTracer()->inject(
64-
$span->getContext(),
65-
TEXT_MAP,
66-
$carrier
67-
);
68-
$this->context->set('tracer.carrier', $carrier);
69-
CT::set('tracer.span.' . static::class, $span);
70-
return $path;
71-
}
72-
73-
if ($proceedingJoinPoint->methodName === 'send') {
74-
try {
75-
$result = $proceedingJoinPoint->process();
76-
} catch (Throwable $e) {
77-
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
78-
$span->setTag('error', true);
79-
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
80-
CT::set('tracer.span.' . static::class, $span);
81-
}
82-
throw $e;
83-
} finally {
84-
/** @var Span $span */
85-
if ($span = CT::get('tracer.span.' . static::class)) {
86-
if ($this->spanTagManager->has('rpc', 'status')) {
87-
$span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed');
88-
}
89-
$span->finish();
90-
}
91-
}
92-
93-
return $result;
94-
}
95-
return $proceedingJoinPoint->process();
96-
}
9719
}

src/Aspect/RpcAspect.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 [email protected]
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+
}

src/ConfigProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyperf\Tracer\Aspect\CreateTraceContextAspect;
1616
use Hyperf\Tracer\Aspect\HttpClientAspect;
1717
use Hyperf\Tracer\Aspect\RedisAspect;
18+
use Hyperf\Tracer\Aspect\RpcAspect;
1819
use Hyperf\Tracer\Aspect\TraceAnnotationAspect;
1920
use Hyperf\Tracer\Listener\DbQueryExecutedListener;
2021
use Jaeger\ThriftUdpTransport;
@@ -49,6 +50,7 @@ public function __invoke(): array
4950
CreateTraceContextAspect::class,
5051
HttpClientAspect::class,
5152
RedisAspect::class,
53+
RpcAspect::class,
5254
TraceAnnotationAspect::class,
5355
],
5456
'publish' => [

0 commit comments

Comments
 (0)