Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Tracing): end tracing after calling TracedResponse::toStream() #47

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/Propagation/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(private ConnectionInterface $decorated, private Trac

public function prepare(string $sql): Statement
{
$sql = $sql.self::formatComments($this->infoProvider->getTraceContext());
$sql .= self::formatComments($this->infoProvider->getTraceContext());

return $this->decorated->prepare($sql);
}
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
Loading