Skip to content

Commit 2bb4bcd

Browse files
authored
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.
1 parent c740741 commit 2bb4bcd

3 files changed

Lines changed: 56 additions & 18 deletions

File tree

publish/opentracing.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'redis' => env('TRACER_ENABLE_REDIS', false),
1919
'db' => env('TRACER_ENABLE_DB', false),
2020
'method' => env('TRACER_ENABLE_METHOD', false),
21-
'error' => false,
21+
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
2222
],
2323
'tracer' => [
2424
'zipkin' => [
@@ -72,5 +72,22 @@
7272
'db.statement' => 'db.statement',
7373
'db.query_time' => 'db.query_time',
7474
],
75+
'exception' => [
76+
'class' => 'exception.class',
77+
'code' => 'exception.code',
78+
'message' => 'exception.message',
79+
'stack_trace' => 'exception.stack_trace',
80+
],
81+
'request' => [
82+
'path' => 'request.path',
83+
'method' => 'request.method',
84+
'header' => 'request.header',
85+
],
86+
'coroutine' => [
87+
'id' => 'coroutine.id',
88+
],
89+
'response' => [
90+
'status_code' => 'response.status_code',
91+
],
7592
],
7693
];

src/Middleware/TraceMiddleware.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Hyperf\HttpMessage\Exception\HttpException;
1515
use Hyperf\Tracer\SpanStarter;
16+
use Hyperf\Tracer\SpanTagManager;
1617
use Hyperf\Tracer\SwitchManager;
1718
use Hyperf\Utils\Coroutine;
1819
use OpenTracing\Span;
@@ -31,15 +32,21 @@ class TraceMiddleware implements MiddlewareInterface
3132
*/
3233
protected $switchManager;
3334

35+
/**
36+
* @var SpanTagManager
37+
*/
38+
protected $spanTagManager;
39+
3440
/**
3541
* @var Tracer
3642
*/
3743
private $tracer;
3844

39-
public function __construct(Tracer $tracer, SwitchManager $switchManager)
45+
public function __construct(Tracer $tracer, SwitchManager $switchManager, SpanTagManager $spanTagManager)
4046
{
4147
$this->tracer = $tracer;
4248
$this->switchManager = $switchManager;
49+
$this->spanTagManager = $spanTagManager;
4350
}
4451

4552
/**
@@ -60,9 +67,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6067
});
6168
try {
6269
$response = $handler->handle($request);
63-
$span->setTag('response.statusCode', $response->getStatusCode());
70+
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
6471
} catch (\Throwable $exception) {
65-
$this->switchManager->isEnable('error') && $this->appendExceptionToSpan($span, $exception);
72+
$this->switchManager->isEnable('exception') && $this->appendExceptionToSpan($span, $exception);
73+
if ($exception instanceof HttpException) {
74+
$span->setTag($this->spanTagManager->get('response', 'status_code'), $exception->getStatusCode());
75+
}
6676
throw $exception;
6777
} finally {
6878
$span->finish();
@@ -74,27 +84,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7484
protected function appendExceptionToSpan(Span $span, \Throwable $exception): void
7585
{
7686
$span->setTag('error', true);
77-
$span->setTag('error.code', $exception->getCode());
78-
$span->setTag('error.file', $exception->getFile());
79-
$span->setTag('error.line', $exception->getLine());
80-
$span->setTag('error.message', $exception->getMessage());
81-
$span->setTag('error.stackTrace', $exception->getTraceAsString());
82-
$span->setTag('error.type', get_class($exception));
83-
84-
if ($exception instanceof HttpException) {
85-
$span->setTag('error.statusCode', $exception->getStatusCode());
86-
}
87+
$span->setTag($this->spanTagManager->get('exception', 'class'), get_class($exception));
88+
$span->setTag($this->spanTagManager->get('exception', 'code'), $exception->getCode());
89+
$span->setTag($this->spanTagManager->get('exception', 'message'), $exception->getMessage());
90+
$span->setTag($this->spanTagManager->get('exception', 'stack_trace'), (string) $exception);
8791
}
8892

8993
protected function buildSpan(ServerRequestInterface $request): Span
9094
{
9195
$uri = $request->getUri();
9296
$span = $this->startSpan('request');
93-
$span->setTag('coroutine.id', (string) Coroutine::id());
94-
$span->setTag('request.path', (string) $uri);
95-
$span->setTag('request.method', $request->getMethod());
97+
$span->setTag($this->spanTagManager->get('coroutine', 'id'), (string) Coroutine::id());
98+
$span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri);
99+
$span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod());
96100
foreach ($request->getHeaders() as $key => $value) {
97-
$span->setTag('request.header.' . $key, implode(', ', $value));
101+
$span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value));
98102
}
99103
return $span;
100104
}

src/SpanTagManager.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ class SpanTagManager
3131
'path' => 'rpc.path',
3232
'status' => 'rpc.status',
3333
],
34+
'exception' => [
35+
'class' => 'exception.class',
36+
'code' => 'exception.code',
37+
'message' => 'exception.message',
38+
'stack_trace' => 'exception.stack_trace',
39+
],
40+
'request' => [
41+
'path' => 'request.path',
42+
'method' => 'request.method',
43+
'header' => 'request.header',
44+
],
45+
'coroutine' => [
46+
'id' => 'coroutine.id',
47+
],
48+
'response' => [
49+
'status_code' => 'response.status_code',
50+
],
3451
];
3552

3653
public function apply(array $tags): void

0 commit comments

Comments
 (0)