Optimized tag of exception for tracer. (#4082)

* Optimized tag of exception for tracer.

* allow user to change the tags of exception when using tracer.

* Use setTag instead of log.
This commit is contained in:
李铭昕 2021-09-21 17:06:07 +08:00 committed by GitHub
parent d34e162f72
commit b54125f0ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 19 deletions

View File

@ -9,7 +9,7 @@
## Added ## Added
- [#4014](https://github.com/hyperf/hyperf/pull/4014) [#4080](https://github.com/hyperf/hyperf/pull/4080) Support `sasl` and `ssl` for kafka. - [#4014](https://github.com/hyperf/hyperf/pull/4014) [#4080](https://github.com/hyperf/hyperf/pull/4080) Support `sasl` and `ssl` for kafka.
- [#4045](https://github.com/hyperf/hyperf/pull/4045) Support to control whether to report by `tracer` through config `opentracing.enable.error`. - [#4045](https://github.com/hyperf/hyperf/pull/4045) [#4082](https://github.com/hyperf/hyperf/pull/4082) Support to control whether to report by `tracer` through config `opentracing.enable.exception`.
- [#4086](https://github.com/hyperf/hyperf/pull/4086) Support annotation for interface. - [#4086](https://github.com/hyperf/hyperf/pull/4086) Support annotation for interface.
# Optimized # Optimized

View File

@ -18,7 +18,7 @@ return [
'redis' => env('TRACER_ENABLE_REDIS', false), 'redis' => env('TRACER_ENABLE_REDIS', false),
'db' => env('TRACER_ENABLE_DB', false), 'db' => env('TRACER_ENABLE_DB', false),
'method' => env('TRACER_ENABLE_METHOD', false), 'method' => env('TRACER_ENABLE_METHOD', false),
'error' => false, 'exception' => env('TRACER_ENABLE_EXCEPTION', false),
], ],
'tracer' => [ 'tracer' => [
'zipkin' => [ 'zipkin' => [
@ -72,5 +72,22 @@ return [
'db.statement' => 'db.statement', 'db.statement' => 'db.statement',
'db.query_time' => 'db.query_time', 'db.query_time' => 'db.query_time',
], ],
'exception' => [
'class' => 'exception.class',
'code' => 'exception.code',
'message' => 'exception.message',
'stack_trace' => 'exception.stack_trace',
],
'request' => [
'path' => 'request.path',
'method' => 'request.method',
'header' => 'request.header',
],
'coroutine' => [
'id' => 'coroutine.id',
],
'response' => [
'status_code' => 'response.status_code',
],
], ],
]; ];

View File

@ -13,6 +13,7 @@ namespace Hyperf\Tracer\Middleware;
use Hyperf\HttpMessage\Exception\HttpException; use Hyperf\HttpMessage\Exception\HttpException;
use Hyperf\Tracer\SpanStarter; use Hyperf\Tracer\SpanStarter;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager; use Hyperf\Tracer\SwitchManager;
use Hyperf\Utils\Coroutine; use Hyperf\Utils\Coroutine;
use OpenTracing\Span; use OpenTracing\Span;
@ -31,15 +32,21 @@ class TraceMiddleware implements MiddlewareInterface
*/ */
protected $switchManager; protected $switchManager;
/**
* @var SpanTagManager
*/
protected $spanTagManager;
/** /**
* @var Tracer * @var Tracer
*/ */
private $tracer; private $tracer;
public function __construct(Tracer $tracer, SwitchManager $switchManager) public function __construct(Tracer $tracer, SwitchManager $switchManager, SpanTagManager $spanTagManager)
{ {
$this->tracer = $tracer; $this->tracer = $tracer;
$this->switchManager = $switchManager; $this->switchManager = $switchManager;
$this->spanTagManager = $spanTagManager;
} }
/** /**
@ -60,9 +67,12 @@ class TraceMiddleware implements MiddlewareInterface
}); });
try { try {
$response = $handler->handle($request); $response = $handler->handle($request);
$span->setTag('response.statusCode', $response->getStatusCode()); $span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->switchManager->isEnable('error') && $this->appendExceptionToSpan($span, $exception); $this->switchManager->isEnable('exception') && $this->appendExceptionToSpan($span, $exception);
if ($exception instanceof HttpException) {
$span->setTag($this->spanTagManager->get('response', 'status_code'), $exception->getStatusCode());
}
throw $exception; throw $exception;
} finally { } finally {
$span->finish(); $span->finish();
@ -74,27 +84,21 @@ class TraceMiddleware implements MiddlewareInterface
protected function appendExceptionToSpan(Span $span, \Throwable $exception): void protected function appendExceptionToSpan(Span $span, \Throwable $exception): void
{ {
$span->setTag('error', true); $span->setTag('error', true);
$span->setTag('error.code', $exception->getCode()); $span->setTag($this->spanTagManager->get('exception', 'class'), get_class($exception));
$span->setTag('error.file', $exception->getFile()); $span->setTag($this->spanTagManager->get('exception', 'code'), $exception->getCode());
$span->setTag('error.line', $exception->getLine()); $span->setTag($this->spanTagManager->get('exception', 'message'), $exception->getMessage());
$span->setTag('error.message', $exception->getMessage()); $span->setTag($this->spanTagManager->get('exception', 'stack_trace'), (string) $exception);
$span->setTag('error.stackTrace', $exception->getTraceAsString());
$span->setTag('error.type', get_class($exception));
if ($exception instanceof HttpException) {
$span->setTag('error.statusCode', $exception->getStatusCode());
}
} }
protected function buildSpan(ServerRequestInterface $request): Span protected function buildSpan(ServerRequestInterface $request): Span
{ {
$uri = $request->getUri(); $uri = $request->getUri();
$span = $this->startSpan('request'); $span = $this->startSpan('request');
$span->setTag('coroutine.id', (string) Coroutine::id()); $span->setTag($this->spanTagManager->get('coroutine', 'id'), (string) Coroutine::id());
$span->setTag('request.path', (string) $uri); $span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri);
$span->setTag('request.method', $request->getMethod()); $span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod());
foreach ($request->getHeaders() as $key => $value) { foreach ($request->getHeaders() as $key => $value) {
$span->setTag('request.header.' . $key, implode(', ', $value)); $span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value));
} }
return $span; return $span;
} }

View File

@ -31,6 +31,23 @@ class SpanTagManager
'path' => 'rpc.path', 'path' => 'rpc.path',
'status' => 'rpc.status', 'status' => 'rpc.status',
], ],
'exception' => [
'class' => 'exception.class',
'code' => 'exception.code',
'message' => 'exception.message',
'stack_trace' => 'exception.stack_trace',
],
'request' => [
'path' => 'request.path',
'method' => 'request.method',
'header' => 'request.header',
],
'coroutine' => [
'id' => 'coroutine.id',
],
'response' => [
'status_code' => 'response.status_code',
],
]; ];
public function apply(array $tags): void public function apply(array $tags): void