1313
1414use Hyperf \HttpMessage \Exception \HttpException ;
1515use Hyperf \Tracer \SpanStarter ;
16+ use Hyperf \Tracer \SpanTagManager ;
1617use Hyperf \Tracer \SwitchManager ;
1718use Hyperf \Utils \Coroutine ;
1819use 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 }
0 commit comments