From 4d8aa94c67fb9c75ebe47eea7b6f02ab6e2b9a36 Mon Sep 17 00:00:00 2001 From: jonas-elias <48037643+jonas-elias@users.noreply.github.com> Date: Thu, 23 May 2024 23:50:41 -0300 Subject: [PATCH] Added the request body and response body to the tracer (#6793) --- publish/opentracing.php | 2 ++ src/Listener/RequestTraceListener.php | 9 +++++++++ src/Middleware/TraceMiddleware.php | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/publish/opentracing.php b/publish/opentracing.php index b0a2b4e..9839059 100644 --- a/publish/opentracing.php +++ b/publish/opentracing.php @@ -125,12 +125,14 @@ 'uri' => 'request.uri', 'method' => 'request.method', 'header' => 'request.header', + // 'body' => 'request.body', ], 'coroutine' => [ 'id' => 'coroutine.id', ], 'response' => [ 'status_code' => 'response.status_code', + // 'body' => 'response.body', ], 'rpc' => [ 'path' => 'rpc.path', diff --git a/src/Listener/RequestTraceListener.php b/src/Listener/RequestTraceListener.php index f5630a8..5befbfb 100644 --- a/src/Listener/RequestTraceListener.php +++ b/src/Listener/RequestTraceListener.php @@ -77,12 +77,18 @@ protected function handleRequestTerminated(RequestTerminated $event): void $tracer = TracerContext::getTracer(); $span = TracerContext::getRoot(); $span->setTag($this->spanTagManager->get('response', 'status_code'), (string) $response->getStatusCode()); + if ($this->spanTagManager->has('response', 'body')) { + $span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody()); + } if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception)) { $this->appendExceptionToSpan($span, $exception = $event->exception); if ($exception instanceof HttpException) { $span->setTag($this->spanTagManager->get('response', 'status_code'), $exception->getStatusCode()); + if ($this->spanTagManager->has('response', 'body')) { + $span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody()); + } } } @@ -107,6 +113,9 @@ protected function buildSpan(ServerRequestInterface $request): Span $span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri->getPath()); $span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod()); $span->setTag($this->spanTagManager->get('request', 'uri'), (string) $uri); + if ($this->spanTagManager->has('request', 'body')) { + $span->setTag($this->spanTagManager->get('request', 'body'), (string) $request->getBody()); + } foreach ($request->getHeaders() as $key => $value) { $span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value)); } diff --git a/src/Middleware/TraceMiddleware.php b/src/Middleware/TraceMiddleware.php index 594e962..65828a6 100644 --- a/src/Middleware/TraceMiddleware.php +++ b/src/Middleware/TraceMiddleware.php @@ -58,6 +58,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $response = $response->withHeader('Trace-Id', $traceId); } $span->setTag($this->spanTagManager->get('response', 'status_code'), (string) $response->getStatusCode()); + if ($this->spanTagManager->has('response', 'body')) { + $span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody()); + } } catch (Throwable $exception) { if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception)) { $this->appendExceptionToSpan($span, $exception); @@ -90,6 +93,9 @@ protected function buildSpan(ServerRequestInterface $request): Span $span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri->getPath()); $span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod()); $span->setTag($this->spanTagManager->get('request', 'uri'), (string) $uri); + if ($this->spanTagManager->has('request', 'body')) { + $span->setTag($this->spanTagManager->get('request', 'body'), (string) $request->getBody()); + } foreach ($request->getHeaders() as $key => $value) { $span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value)); }