Skip to content

Commit

Permalink
Added the request body and response body to the tracer (#6793)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-elias committed May 24, 2024
1 parent c18803b commit 4d8aa94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions src/Listener/RequestTraceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand All @@ -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));
}
Expand Down
6 changes: 6 additions & 0 deletions src/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 4d8aa94

Please sign in to comment.