Skip to content

Commit

Permalink
fix(Tracing): end tracing after calling TracedResponse::toStream()
Browse files Browse the repository at this point in the history
  • Loading branch information
cyve committed Oct 3, 2023
1 parent 0865514 commit 6d912a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class TracedResponse implements ResponseInterface, StreamableInterface
{
private ?string $content = null;
/** @var resource|null */
private $stream;

public function __construct(
private ResponseInterface $response,
Expand Down Expand Up @@ -91,16 +93,20 @@ public function getInfo(string $type = null): mixed

public function toStream(bool $throw = true)
{
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}
try {
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}

if ($this->response instanceof StreamableInterface) {
return $this->response->toStream(false);
}
if ($this->response instanceof StreamableInterface) {
return $this->stream = $this->response->toStream(false);
}

return StreamWrapper::createResource($this->response);
return $this->stream = StreamWrapper::createResource($this->response);
} finally {
$this->endTracing();
}
}

/**
Expand Down Expand Up @@ -142,6 +148,9 @@ protected function endTracing(): void
$this->span->setAttribute('response.headers', $this->getHeaders(false));
}
if (\in_array('response.body', $info['user_data']['span_attributes'] ?? [])) {
if (empty($this->content) && \is_resource($this->stream)) {
$this->content = stream_get_contents($this->stream) ?: null;
}
$this->span->setAttribute('response.body', $this->content);
}
} catch (\Throwable) {
Expand Down
2 changes: 1 addition & 1 deletion src/Tracing/Serializer/Normalizer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(private NormalizerInterface $decorated, private bool
{
}

public function setSerializer(SerializerInterface $serializer)
public function setSerializer(SerializerInterface $serializer): void
{
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
Expand Down

0 comments on commit 6d912a5

Please sign in to comment.