Skip to content

Commit

Permalink
Optimized tag of exception for tracer. (#4082)
Browse files Browse the repository at this point in the history
* Optimized tag of exception for tracer.

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

* Use setTag instead of log.
  • Loading branch information
limingxinleo committed Sep 21, 2021
1 parent c740741 commit 2bb4bcd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
19 changes: 18 additions & 1 deletion publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'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' => [
Expand Down Expand Up @@ -72,5 +72,22 @@
'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',
],
],
];
38 changes: 21 additions & 17 deletions src/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Hyperf\HttpMessage\Exception\HttpException;
use Hyperf\Tracer\SpanStarter;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager;
use Hyperf\Utils\Coroutine;
use OpenTracing\Span;
Expand All @@ -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;
}

/**
Expand All @@ -60,9 +67,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
});
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();
Expand All @@ -74,27 +84,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
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;
}
Expand Down
17 changes: 17 additions & 0 deletions src/SpanTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2bb4bcd

Please sign in to comment.