Skip to content

Commit

Permalink
Added Hyperf\Tracer\Aspect\GrpcAspect (#6203)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
lixinhan and huangdijia authored Oct 12, 2023
1 parent 11029e3 commit ae9abcc
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 12 deletions.
1 change: 1 addition & 0 deletions publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'db' => env('TRACER_ENABLE_DB', false),
'elasticserach' => env('TRACER_ENABLE_ELASTICSERACH', false),
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
'grpc' => env('TRACER_ENABLE_GRPC', false),
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
'method' => env('TRACER_ENABLE_METHOD', false),
'redis' => env('TRACER_ENABLE_REDIS', false),
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)

$callable();
} catch (Throwable $e) {
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$child->setTag('error', true);
$child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/DbAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/ElasticserachAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
130 changes: 130 additions & 0 deletions src/Aspect/GrpcAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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\Context\Context as CT;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\GrpcClient\GrpcClient;
use Hyperf\GrpcClient\Request;
use Hyperf\Rpc\Context;
use Hyperf\Tracer\SpanStarter;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager;
use Hyperf\Tracer\TracerContext;
use OpenTracing\Span;
use Psr\Container\ContainerInterface;
use Swoole\Http2\Response;
use Throwable;

use const OpenTracing\Formats\TEXT_MAP;

class GrpcAspect extends AbstractAspect
{
use SpanStarter;

public array $classes = [
GrpcClient::class . '::send',
GrpcClient::class . '::recv',
];

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 (! $this->switchManager->isEnable('grpc')) {
return $proceedingJoinPoint->process();
}

return match ($proceedingJoinPoint->methodName) {
'send' => $this->processSend($proceedingJoinPoint),
'recv' => $this->processRecv($proceedingJoinPoint),
default => $proceedingJoinPoint->process(),
};
}

private function processSend(ProceedingJoinPoint $proceedingJoinPoint)
{
$arguments = $proceedingJoinPoint->getArguments();
/** @var Request $request */
$request = $arguments[0];
$key = "GRPC send [{$request->path}]";
$span = $this->startSpan($key);
$carrier = [];
// Injects the context into the wire
TracerContext::getTracer()->inject(
$span->getContext(),
TEXT_MAP,
$carrier
);

// Merge tracer info
$request->headers = array_merge($request->headers, $carrier);
if ($this->spanTagManager->has('grpc', 'request.header')) {
foreach ($request->headers as $key => $value) {
$span->setTag($this->spanTagManager->get('grpc', 'request.header') . '.' . $key, $value);
}
}

$this->context->set('tracer.carrier', $carrier);
CT::set('tracer.span.' . static::class, $span);

try {
return $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
throw $e;
}
}

private function processRecv(ProceedingJoinPoint $proceedingJoinPoint)
{
/** @var null|Span $span */
$span = CT::get('tracer.span.' . static::class);

try {
/** @var bool|Response $result */
$result = $proceedingJoinPoint->process();
if ($result instanceof Response) {
if ($this->spanTagManager->has('grpc', 'response.header')) {
foreach ($result->headers as $key => $value) {
$span?->setTag($this->spanTagManager->get('grpc', 'response.header') . '.' . $key, $value);
}
}
}
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
$span?->setTag('error', true);
$span?->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
throw $e;
} finally {
$span?->finish();
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Aspect/HttpClientAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
}
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/MethodAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/RedisAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$result = $proceedingJoinPoint->process();
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/RpcAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
CT::set('tracer.span.' . static::class, $span);
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/TraceAnnotationAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Hyperf\Tracer\Aspect\CoroutineAspect;
use Hyperf\Tracer\Aspect\CreateTraceContextAspect;
use Hyperf\Tracer\Aspect\ElasticserachAspect;
use Hyperf\Tracer\Aspect\GrpcAspect;
use Hyperf\Tracer\Aspect\HttpClientAspect;
use Hyperf\Tracer\Aspect\RedisAspect;
use Hyperf\Tracer\Aspect\RpcAspect;
Expand Down Expand Up @@ -52,6 +53,7 @@ public function __invoke(): array
CoroutineAspect::class,
CreateTraceContextAspect::class,
ElasticserachAspect::class,
GrpcAspect::class,
HttpClientAspect::class,
RedisAspect::class,
RpcAspect::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/RequestTraceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function handleRequestTerminated(RequestTerminated $event): void
$span = TracerContext::getRoot();
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());

if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception::class)) {
if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception)) {
$this->appendExceptionToSpan($span, $exception = $event->exception);

if ($exception instanceof HttpException) {
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
} catch (Throwable $exception) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception)) {
$this->appendExceptionToSpan($span, $exception);
}
if ($exception instanceof HttpException) {
Expand Down
4 changes: 4 additions & 0 deletions src/SpanTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class SpanTagManager
'http.method' => 'http.method',
'http.status_code' => 'http.status_code',
],
'grpc' => [
'request.header' => 'grpc.request.header',
'response.header' => 'grpc.response.header',
],
'redis' => [
'arguments' => 'redis.arguments',
'result' => 'redis.result',
Expand Down
5 changes: 3 additions & 2 deletions src/SwitchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Hyperf\Context\Context;
use OpenTracing\Span;
use Throwable;

class SwitchManager
{
Expand Down Expand Up @@ -50,11 +51,11 @@ public function isEnable(string $identifier): bool
return $this->config[$identifier] && Context::get('tracer.root') instanceof Span;
}

public function isIgnoreException(string $className): bool
public function isIgnoreException(Throwable|string $exception): bool
{
$ignoreExceptions = $this->config['ignore_exceptions'] ?? [];
foreach ($ignoreExceptions as $ignoreException) {
if (is_a($className, $ignoreException, true)) {
if (is_a($exception, $ignoreException, true)) {
return true;
}
}
Expand Down

0 comments on commit ae9abcc

Please sign in to comment.