Skip to content

Commit

Permalink
fix: end tracing when receiving last chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
cyve committed Jan 11, 2024
1 parent ff4d2b1 commit 7cf2cba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Response\StreamableInterface;
use Symfony\Component\HttpClient\Response\StreamWrapper;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand Down Expand Up @@ -93,20 +94,16 @@ public function getInfo(string $type = null): mixed

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

if ($this->response instanceof StreamableInterface) {
return $this->stream = $this->response->toStream(false);
}
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}

return $this->stream = StreamWrapper::createResource($this->response);
} finally {
$this->endTracing();
if ($this->response instanceof StreamableInterface) {
return $this->stream = $this->response->toStream(false);
}

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

/**
Expand All @@ -129,6 +126,14 @@ public static function stream(HttpClientInterface $client, iterable $responses,
}

foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
try {
if ($chunk->isLast() || $chunk->isTimeout()) {
$traceableMap[$r]->endTracing();
}
} catch (TransportExceptionInterface) {
$traceableMap[$r]->endTracing();
}

yield $traceableMap[$r] => $chunk;
}
}
Expand All @@ -150,6 +155,7 @@ protected function endTracing(): void
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;
rewind($this->stream);
}
$this->span->setAttribute('response.body', $this->content);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Http/TracingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Instrumentation\Tracing\Tracing;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SemConv\TraceAttributes;
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Component\HttpClient\HttpClient;
Expand Down Expand Up @@ -119,7 +120,11 @@ public function request(string $method, string $url, array $options = []): Respo

$span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $info['http_code']);
$span->setAttribute(TraceAttributes::HTTP_URL, $info['url']);
$span->addEvent('HTTP headers received');

if (\array_key_exists('total_time', $info)) {
$timestamp = (int) (($info['start_time'] + $info['total_time']) * ClockInterface::NANOS_PER_SECOND);
}
$span->addEvent('http.response.headers', [], $timestamp ?? null);

if ($info['http_code'] >= 400) {
$span->setStatus(StatusCode::STATUS_ERROR);
Expand Down

0 comments on commit 7cf2cba

Please sign in to comment.