Skip to content

Commit ae9abcc

Browse files
lixinhanhuangdijia
andauthored
Added Hyperf\Tracer\Aspect\GrpcAspect (#6203)
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent 11029e3 commit ae9abcc

15 files changed

Lines changed: 150 additions & 12 deletions

publish/opentracing.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'db' => env('TRACER_ENABLE_DB', false),
2121
'elasticserach' => env('TRACER_ENABLE_ELASTICSERACH', false),
2222
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
23+
'grpc' => env('TRACER_ENABLE_GRPC', false),
2324
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
2425
'method' => env('TRACER_ENABLE_METHOD', false),
2526
'redis' => env('TRACER_ENABLE_REDIS', false),

src/Aspect/CoroutineAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
5858

5959
$callable();
6060
} catch (Throwable $e) {
61-
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
61+
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
6262
$child->setTag('error', true);
6363
$child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
6464
}

src/Aspect/DbAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
4646
try {
4747
$result = $proceedingJoinPoint->process();
4848
} catch (Throwable $e) {
49-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
49+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
5050
$span->setTag('error', true);
5151
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
5252
}

src/Aspect/ElasticserachAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
5757
try {
5858
$result = $proceedingJoinPoint->process();
5959
} catch (Throwable $e) {
60-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
60+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
6161
$span->setTag('error', true);
6262
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
6363
}

src/Aspect/GrpcAspect.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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\GrpcClient\GrpcClient;
18+
use Hyperf\GrpcClient\Request;
19+
use Hyperf\Rpc\Context;
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 Swoole\Http2\Response;
27+
use Throwable;
28+
29+
use const OpenTracing\Formats\TEXT_MAP;
30+
31+
class GrpcAspect extends AbstractAspect
32+
{
33+
use SpanStarter;
34+
35+
public array $classes = [
36+
GrpcClient::class . '::send',
37+
GrpcClient::class . '::recv',
38+
];
39+
40+
private SwitchManager $switchManager;
41+
42+
private SpanTagManager $spanTagManager;
43+
44+
private Context $context;
45+
46+
public function __construct(private ContainerInterface $container)
47+
{
48+
$this->switchManager = $container->get(SwitchManager::class);
49+
$this->spanTagManager = $container->get(SpanTagManager::class);
50+
$this->context = $container->get(Context::class);
51+
}
52+
53+
public function process(ProceedingJoinPoint $proceedingJoinPoint)
54+
{
55+
if (! $this->switchManager->isEnable('grpc')) {
56+
return $proceedingJoinPoint->process();
57+
}
58+
59+
return match ($proceedingJoinPoint->methodName) {
60+
'send' => $this->processSend($proceedingJoinPoint),
61+
'recv' => $this->processRecv($proceedingJoinPoint),
62+
default => $proceedingJoinPoint->process(),
63+
};
64+
}
65+
66+
private function processSend(ProceedingJoinPoint $proceedingJoinPoint)
67+
{
68+
$arguments = $proceedingJoinPoint->getArguments();
69+
/** @var Request $request */
70+
$request = $arguments[0];
71+
$key = "GRPC send [{$request->path}]";
72+
$span = $this->startSpan($key);
73+
$carrier = [];
74+
// Injects the context into the wire
75+
TracerContext::getTracer()->inject(
76+
$span->getContext(),
77+
TEXT_MAP,
78+
$carrier
79+
);
80+
81+
// Merge tracer info
82+
$request->headers = array_merge($request->headers, $carrier);
83+
if ($this->spanTagManager->has('grpc', 'request.header')) {
84+
foreach ($request->headers as $key => $value) {
85+
$span->setTag($this->spanTagManager->get('grpc', 'request.header') . '.' . $key, $value);
86+
}
87+
}
88+
89+
$this->context->set('tracer.carrier', $carrier);
90+
CT::set('tracer.span.' . static::class, $span);
91+
92+
try {
93+
return $proceedingJoinPoint->process();
94+
} catch (Throwable $e) {
95+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
96+
$span->setTag('error', true);
97+
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
98+
}
99+
throw $e;
100+
}
101+
}
102+
103+
private function processRecv(ProceedingJoinPoint $proceedingJoinPoint)
104+
{
105+
/** @var null|Span $span */
106+
$span = CT::get('tracer.span.' . static::class);
107+
108+
try {
109+
/** @var bool|Response $result */
110+
$result = $proceedingJoinPoint->process();
111+
if ($result instanceof Response) {
112+
if ($this->spanTagManager->has('grpc', 'response.header')) {
113+
foreach ($result->headers as $key => $value) {
114+
$span?->setTag($this->spanTagManager->get('grpc', 'response.header') . '.' . $key, $value);
115+
}
116+
}
117+
}
118+
} catch (Throwable $e) {
119+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
120+
$span?->setTag('error', true);
121+
$span?->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
122+
}
123+
throw $e;
124+
} finally {
125+
$span?->finish();
126+
}
127+
128+
return $result;
129+
}
130+
}

src/Aspect/HttpClientAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
8080
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
8181
}
8282
} catch (Throwable $e) {
83-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
83+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
8484
$span->setTag('error', true);
8585
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
8686
}

src/Aspect/MethodAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
4343
try {
4444
$result = $proceedingJoinPoint->process();
4545
} catch (Throwable $e) {
46-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
46+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
4747
$span->setTag('error', true);
4848
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
4949
}

src/Aspect/RedisAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
4747
$result = $proceedingJoinPoint->process();
4848
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
4949
} catch (Throwable $e) {
50-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
50+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
5151
$span->setTag('error', true);
5252
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
5353
}

src/Aspect/RpcAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
7878
try {
7979
$result = $proceedingJoinPoint->process();
8080
} catch (Throwable $e) {
81-
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
81+
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
8282
$span->setTag('error', true);
8383
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
8484
CT::set('tracer.span.' . static::class, $span);

src/Aspect/TraceAnnotationAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
5050
try {
5151
$result = $proceedingJoinPoint->process();
5252
} catch (Throwable $e) {
53-
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
53+
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
5454
$span->setTag('error', true);
5555
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
5656
}

0 commit comments

Comments
 (0)