mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
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:
parent
d34e162f72
commit
b54125f0ea
@ -9,7 +9,7 @@
|
||||
## Added
|
||||
|
||||
- [#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.
|
||||
|
||||
# Optimized
|
||||
|
@ -18,7 +18,7 @@ return [
|
||||
'redis' => env('TRACER_ENABLE_REDIS', false),
|
||||
'db' => env('TRACER_ENABLE_DB', false),
|
||||
'method' => env('TRACER_ENABLE_METHOD', false),
|
||||
'error' => false,
|
||||
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
|
||||
],
|
||||
'tracer' => [
|
||||
'zipkin' => [
|
||||
@ -72,5 +72,22 @@ return [
|
||||
'db.statement' => 'db.statement',
|
||||
'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',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
@ -13,6 +13,7 @@ namespace Hyperf\Tracer\Middleware;
|
||||
|
||||
use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Hyperf\Tracer\SpanStarter;
|
||||
use Hyperf\Tracer\SpanTagManager;
|
||||
use Hyperf\Tracer\SwitchManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use OpenTracing\Span;
|
||||
@ -31,15 +32,21 @@ class TraceMiddleware implements MiddlewareInterface
|
||||
*/
|
||||
protected $switchManager;
|
||||
|
||||
/**
|
||||
* @var SpanTagManager
|
||||
*/
|
||||
protected $spanTagManager;
|
||||
|
||||
/**
|
||||
* @var Tracer
|
||||
*/
|
||||
private $tracer;
|
||||
|
||||
public function __construct(Tracer $tracer, SwitchManager $switchManager)
|
||||
public function __construct(Tracer $tracer, SwitchManager $switchManager, SpanTagManager $spanTagManager)
|
||||
{
|
||||
$this->tracer = $tracer;
|
||||
$this->switchManager = $switchManager;
|
||||
$this->spanTagManager = $spanTagManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,9 +67,12 @@ class TraceMiddleware implements MiddlewareInterface
|
||||
});
|
||||
try {
|
||||
$response = $handler->handle($request);
|
||||
$span->setTag('response.statusCode', $response->getStatusCode());
|
||||
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
|
||||
} 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;
|
||||
} finally {
|
||||
$span->finish();
|
||||
@ -74,27 +84,21 @@ class TraceMiddleware implements MiddlewareInterface
|
||||
protected function appendExceptionToSpan(Span $span, \Throwable $exception): void
|
||||
{
|
||||
$span->setTag('error', true);
|
||||
$span->setTag('error.code', $exception->getCode());
|
||||
$span->setTag('error.file', $exception->getFile());
|
||||
$span->setTag('error.line', $exception->getLine());
|
||||
$span->setTag('error.message', $exception->getMessage());
|
||||
$span->setTag('error.stackTrace', $exception->getTraceAsString());
|
||||
$span->setTag('error.type', get_class($exception));
|
||||
|
||||
if ($exception instanceof HttpException) {
|
||||
$span->setTag('error.statusCode', $exception->getStatusCode());
|
||||
}
|
||||
$span->setTag($this->spanTagManager->get('exception', 'class'), get_class($exception));
|
||||
$span->setTag($this->spanTagManager->get('exception', 'code'), $exception->getCode());
|
||||
$span->setTag($this->spanTagManager->get('exception', 'message'), $exception->getMessage());
|
||||
$span->setTag($this->spanTagManager->get('exception', 'stack_trace'), (string) $exception);
|
||||
}
|
||||
|
||||
protected function buildSpan(ServerRequestInterface $request): Span
|
||||
{
|
||||
$uri = $request->getUri();
|
||||
$span = $this->startSpan('request');
|
||||
$span->setTag('coroutine.id', (string) Coroutine::id());
|
||||
$span->setTag('request.path', (string) $uri);
|
||||
$span->setTag('request.method', $request->getMethod());
|
||||
$span->setTag($this->spanTagManager->get('coroutine', 'id'), (string) Coroutine::id());
|
||||
$span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri);
|
||||
$span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod());
|
||||
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;
|
||||
}
|
||||
|
@ -31,6 +31,23 @@ class SpanTagManager
|
||||
'path' => 'rpc.path',
|
||||
'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
|
||||
|
Loading…
Reference in New Issue
Block a user