Skip to content

Commit

Permalink
完善
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Apr 20, 2022
1 parent b5fe17c commit 7f6fa8d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/middleware/TraceRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use think\Request;
use think\Response;
use think\tracing\Tracer;
use const OpenTracing\Formats\TEXT_MAP;
use const OpenTracing\Tags\HTTP_METHOD;
use const OpenTracing\Tags\HTTP_STATUS_CODE;
use const OpenTracing\Tags\HTTP_URL;
Expand All @@ -25,16 +26,20 @@ public function __construct(Tracer $tracer)
/**
* @param Request $request
* @param Closure $next
* @return mixed
* @return Response
*/
public function handle($request, Closure $next)
{
$context = $this->tracer->extract(TEXT_MAP, $request->header());

$scope = $this->tracer->startActiveSpan(
"http:" . $request->baseUrl(),
[
'tags' => [
'child_of' => $context,
'tags' => [
HTTP_METHOD => $request->method(),
HTTP_URL => $request->url(true),
'http.ip' => $request->ip(),
],
]
);
Expand All @@ -43,9 +48,15 @@ public function handle($request, Closure $next)
/** @var Response $response */
$response = $next($request);

$scope->getSpan()->setTag(HTTP_STATUS_CODE, $response->getCode());
$span = $scope->getSpan();

$span->setTag(HTTP_STATUS_CODE, $response->getCode());

$headers = [];

$this->tracer->inject($span->getContext(), TEXT_MAP, $headers);

return $response;
return $response->header($headers);
} finally {
$scope->close();
}
Expand Down

0 comments on commit 7f6fa8d

Please # to comment.