vendor/sentry/sentry-symfony/src/Tracing/HttpClient/AbstractTraceableResponse.php line 83

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\Tracing\HttpClient;
  4. use Sentry\Tracing\Span;
  5. use Sentry\Tracing\SpanStatus;
  6. use Symfony\Contracts\HttpClient\ChunkInterface;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. use Symfony\Contracts\HttpClient\ResponseInterface;
  9. /**
  10.  * @internal
  11.  */
  12. abstract class AbstractTraceableResponse implements ResponseInterface
  13. {
  14.     /**
  15.      * @var ResponseInterface
  16.      */
  17.     protected $response;
  18.     /**
  19.      * @var HttpClientInterface
  20.      */
  21.     protected $client;
  22.     /**
  23.      * @var Span|null
  24.      */
  25.     protected $span;
  26.     public function __construct(HttpClientInterface $clientResponseInterface $response, ?Span $span)
  27.     {
  28.         $this->client $client;
  29.         $this->response $response;
  30.         $this->span $span;
  31.     }
  32.     public function __destruct()
  33.     {
  34.         try {
  35.             if (method_exists($this->response'__destruct')) {
  36.                 $this->response->__destruct();
  37.             }
  38.         } finally {
  39.             $this->finishSpan();
  40.         }
  41.     }
  42.     public function __sleep(): array
  43.     {
  44.         throw new \BadMethodCallException('Serializing instances of this class is forbidden.');
  45.     }
  46.     public function __wakeup()
  47.     {
  48.         throw new \BadMethodCallException('Unserializing instances of this class is forbidden.');
  49.     }
  50.     public function getStatusCode(): int
  51.     {
  52.         return $this->response->getStatusCode();
  53.     }
  54.     public function getHeaders(bool $throw true): array
  55.     {
  56.         return $this->response->getHeaders($throw);
  57.     }
  58.     public function getContent(bool $throw true): string
  59.     {
  60.         try {
  61.             return $this->response->getContent($throw);
  62.         } finally {
  63.             $this->finishSpan();
  64.         }
  65.     }
  66.     public function toArray(bool $throw true): array
  67.     {
  68.         try {
  69.             return $this->response->toArray($throw);
  70.         } finally {
  71.             $this->finishSpan();
  72.         }
  73.     }
  74.     public function cancel(): void
  75.     {
  76.         $this->response->cancel();
  77.         $this->finishSpan();
  78.     }
  79.     /**
  80.      * @internal
  81.      *
  82.      * @param iterable<AbstractTraceableResponse> $responses
  83.      *
  84.      * @return \Generator<AbstractTraceableResponse, ChunkInterface>
  85.      */
  86.     public static function stream(HttpClientInterface $clientiterable $responses, ?float $timeout): \Generator
  87.     {
  88.         /** @var \SplObjectStorage<ResponseInterface, AbstractTraceableResponse> $traceableMap */
  89.         $traceableMap = new \SplObjectStorage();
  90.         $wrappedResponses = [];
  91.         foreach ($responses as $response) {
  92.             if (!$response instanceof self) {
  93.                 throw new \TypeError(\sprintf('"%s::stream()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.'TraceableHttpClient::class, get_debug_type($response)));
  94.             }
  95.             $traceableMap[$response->response] = $response;
  96.             $wrappedResponses[] = $response->response;
  97.         }
  98.         foreach ($client->stream($wrappedResponses$timeout) as $response => $chunk) {
  99.             $traceableResponse $traceableMap[$response];
  100.             $traceableResponse->finishSpan();
  101.             yield $traceableResponse => $chunk;
  102.         }
  103.     }
  104.     private function finishSpan(): void
  105.     {
  106.         if (null === $this->span) {
  107.             return;
  108.         }
  109.         // We finish the span (which means setting the span end timestamp) first
  110.         // to ensure the measured time is as close as possible to the duration of
  111.         // the HTTP request
  112.         $this->span->finish();
  113.         /** @var int $statusCode */
  114.         $statusCode $this->response->getInfo('http_code');
  115.         // If the returned status code is 0, it means that this info isn't available
  116.         // yet (e.g. an error happened before the request was sent), hence we cannot
  117.         // determine what happened.
  118.         if (=== $statusCode) {
  119.             $this->span->setStatus(SpanStatus::unknownError());
  120.         } else {
  121.             $this->span->setStatus(SpanStatus::createFromHttpStatusCode($statusCode));
  122.         }
  123.         $this->span null;
  124.     }
  125. }