Skip to content

Commit 4d8aa94

Browse files
authored
Added the request body and response body to the tracer (#6793)
1 parent c18803b commit 4d8aa94

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

publish/opentracing.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,14 @@
125125
'uri' => 'request.uri',
126126
'method' => 'request.method',
127127
'header' => 'request.header',
128+
// 'body' => 'request.body',
128129
],
129130
'coroutine' => [
130131
'id' => 'coroutine.id',
131132
],
132133
'response' => [
133134
'status_code' => 'response.status_code',
135+
// 'body' => 'response.body',
134136
],
135137
'rpc' => [
136138
'path' => 'rpc.path',

src/Listener/RequestTraceListener.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ protected function handleRequestTerminated(RequestTerminated $event): void
7777
$tracer = TracerContext::getTracer();
7878
$span = TracerContext::getRoot();
7979
$span->setTag($this->spanTagManager->get('response', 'status_code'), (string) $response->getStatusCode());
80+
if ($this->spanTagManager->has('response', 'body')) {
81+
$span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody());
82+
}
8083

8184
if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception)) {
8285
$this->appendExceptionToSpan($span, $exception = $event->exception);
8386

8487
if ($exception instanceof HttpException) {
8588
$span->setTag($this->spanTagManager->get('response', 'status_code'), $exception->getStatusCode());
89+
if ($this->spanTagManager->has('response', 'body')) {
90+
$span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody());
91+
}
8692
}
8793
}
8894

@@ -107,6 +113,9 @@ protected function buildSpan(ServerRequestInterface $request): Span
107113
$span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri->getPath());
108114
$span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod());
109115
$span->setTag($this->spanTagManager->get('request', 'uri'), (string) $uri);
116+
if ($this->spanTagManager->has('request', 'body')) {
117+
$span->setTag($this->spanTagManager->get('request', 'body'), (string) $request->getBody());
118+
}
110119
foreach ($request->getHeaders() as $key => $value) {
111120
$span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value));
112121
}

src/Middleware/TraceMiddleware.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5858
$response = $response->withHeader('Trace-Id', $traceId);
5959
}
6060
$span->setTag($this->spanTagManager->get('response', 'status_code'), (string) $response->getStatusCode());
61+
if ($this->spanTagManager->has('response', 'body')) {
62+
$span->setTag($this->spanTagManager->get('response', 'body'), (string) $response->getBody());
63+
}
6164
} catch (Throwable $exception) {
6265
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception)) {
6366
$this->appendExceptionToSpan($span, $exception);
@@ -90,6 +93,9 @@ protected function buildSpan(ServerRequestInterface $request): Span
9093
$span->setTag($this->spanTagManager->get('request', 'path'), (string) $uri->getPath());
9194
$span->setTag($this->spanTagManager->get('request', 'method'), $request->getMethod());
9295
$span->setTag($this->spanTagManager->get('request', 'uri'), (string) $uri);
96+
if ($this->spanTagManager->has('request', 'body')) {
97+
$span->setTag($this->spanTagManager->get('request', 'body'), (string) $request->getBody());
98+
}
9399
foreach ($request->getHeaders() as $key => $value) {
94100
$span->setTag($this->spanTagManager->get('request', 'header') . '.' . $key, implode(', ', $value));
95101
}

0 commit comments

Comments
 (0)