Tolerance\Tracer\SpanFactory\Psr7\Psr7SpanFactory::fromIncomingResponse PHP Method

fromIncomingResponse() public method

public fromIncomingResponse ( Span $originalSpan, Psr\Http\Message\ResponseInterface $response = null ) : Span
$originalSpan Tolerance\Tracer\Span\Span
$response Psr\Http\Message\ResponseInterface
return Tolerance\Tracer\Span\Span
    public function fromIncomingResponse(Span $originalSpan, ResponseInterface $response = null)
    {
        return new Span($originalSpan->getIdentifier(), $originalSpan->getName(), $originalSpan->getTraceIdentifier(), [new Annotation(Annotation::CLIENT_RECEIVE, $this->clock->microseconds(), $this->endpointResolver->resolve())], [new BinaryAnnotation('http.status', $response !== null ? $response->getStatusCode() : 0, BinaryAnnotation::TYPE_INTEGER_16)], $originalSpan->getParentIdentifier(), $originalSpan->getDebug());
    }

Usage Example

 /**
  * @return callable
  */
 public function create()
 {
     return function (callable $handler) {
         return function (RequestInterface $request, array $options) use($handler) {
             // Store outgoing trace
             $span = $this->psr7SpanFactory->fromOutgoingRequest($request);
             $this->tracer->trace([$span]);
             // Add outgoing headers
             $request = $request->withHeader('X-B3-SpanId', (string) $span->getIdentifier())->withHeader('X-B3-TraceId', (string) $span->getTraceIdentifier())->withHeader('X-B3-ParentSpanId', (string) $span->getParentIdentifier())->withHeader('X-B3-Flags', $span->getDebug() ? '1' : '0');
             return $handler($request, $options)->then(function (ResponseInterface $response) use($span) {
                 $this->tracer->trace([$this->psr7SpanFactory->fromIncomingResponse($span, $response)]);
                 return $response;
             }, function ($reason) use($span) {
                 if ($reason instanceof RequestException) {
                     $this->tracer->trace([$this->psr7SpanFactory->fromIncomingResponse($span, $reason->getResponse())]);
                 }
                 throw $reason;
             });
         };
     };
 }