function it_add_the_tracing_http_headers_the_outgoing_request(Psr7SpanFactory $psr7SpanFactory, Tracer $tracer, RequestInterface $request, PromiseInterface $promise) { $span = $this->generateSpan(); $psr7SpanFactory->fromOutgoingRequest($request)->willReturn($span); $request->withHeader('X-B3-SpanId', (string) $span->getIdentifier())->shouldBeCalled()->willReturn($request); $request->withHeader('X-B3-TraceId', (string) $span->getTraceIdentifier())->shouldBeCalled()->willReturn($request); $request->withHeader('X-B3-ParentSpanId', (string) $span->getParentIdentifier())->shouldBeCalled()->willReturn($request); $middlewareFactory = $this->create(); $middleware = $middlewareFactory(function (RequestInterface $request) use($promise, $span) { return $promise; }); $middleware($request, []); }
/** * @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; }); }; }; }