diff --git a/publish/opentracing.php b/publish/opentracing.php index eb73e25..ca59f26 100644 --- a/publish/opentracing.php +++ b/publish/opentracing.php @@ -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' => [ @@ -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', + ], ], ]; diff --git a/src/Middleware/TraceMiddleware.php b/src/Middleware/TraceMiddleware.php index 7050428..aff810a 100644 --- a/src/Middleware/TraceMiddleware.php +++ b/src/Middleware/TraceMiddleware.php @@ -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; @@ -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 @@ 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(); @@ -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; } diff --git a/src/SpanTagManager.php b/src/SpanTagManager.php index d182145..294537f 100644 --- a/src/SpanTagManager.php +++ b/src/SpanTagManager.php @@ -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