/** * {@inheritdoc} */ public function trace(array $spans) { $key = count($spans) == 1 ? $spans[0]->getName() : count($spans); $key = 'trace (' . $key . ')'; $this->stopwatch->start($key); $this->decoratedTracer->trace($spans); $this->stopwatch->stop($key); }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if (!$event->isMasterRequest()) { return; } $span = $this->httpFoundationSpanFactory->fromIncomingRequest($event->getRequest()); $this->stack->push($span); $this->tracer->trace([$span]); }
/** * @param PostResponseEvent $event */ public function onKernelTerminate(PostResponseEvent $event) { if (!$event->isMasterRequest()) { return; } if ($span = $this->spanStack->pop()) { $this->tracer->trace([$this->httpFoundationSpanFactory->fromOutgoingResponse($event->getResponse(), $span)]); } }
/** * @param RequestInterface $request * @param ResponseInterface|null $response */ public function onEnd(RequestInterface $request, ResponseInterface $response = null) { try { $span = $this->guzzleMessageSpanFactory->fromIncomingResponse($request, $response); } catch (\InvalidArgumentException $e) { return; } $this->tracer->trace([$span]); }
/** * {@inheritdoc} */ public function execute(AMQPMessage $msg) { $span = $this->amqpSpanFactory->fromReceivedMessage($msg); $this->tracer->trace([$span]); $this->spanStack->push($span); $result = $this->decoratedConsumer->execute($msg); $this->tracer->trace([$this->amqpSpanFactory->fromConsumedMessage($msg)]); $this->spanStack->pop(); return $result; }
function it_starts_and_stop_before_an_after_delegating_the_trace(Tracer $decoratedTracer, Stopwatch $stopwatch) { $stopwatch->start(Argument::type('string'))->shouldBeCalled(); $decoratedTracer->trace([])->shouldBeCalled(); $stopwatch->stop(Argument::type('string'))->shouldBeCalled(); $this->trace([]); }
function it_do_not_trace_if_no_request_in_stack(Tracer $tracer, SpanStack $spanStack, Span $span, PostResponseEvent $event) { $event->isMasterRequest()->willReturn(true); $spanStack->pop()->willReturn(null); $tracer->trace(Argument::any())->shouldNotBeCalled(); $this->onKernelTerminate($event); }
/** * {@inheritdoc} */ public function publish($msgBody, $routingKey = '', $additionalProperties = array()) { $message = new AMQPMessage((string) $msgBody, array_merge($additionalProperties, ['routing_key' => $routingKey])); $span = $this->amqpSpanFactory->fromProducedMessage($message); if (!array_key_exists('application_headers', $additionalProperties)) { $additionalProperties['application_headers'] = new AMQPTable(); } elseif (!$additionalProperties['application_headers'] instanceof AMQPTable) { throw new \InvalidArgumentException('Your `application_headers` must be an `AMQPTable`'); } $headers = $additionalProperties['application_headers']; $headers->set('X-B3-SpanId', (string) $span->getIdentifier()); $headers->set('X-B3-TraceId', (string) $span->getTraceIdentifier()); $headers->set('X-B3-ParentSpanId', (string) $span->getParentIdentifier()); $headers->set('X-B3-Flags', $span->getDebug() ? '1' : '0'); $result = $this->decoratedProducer->publish($msgBody, $routingKey, $additionalProperties); $this->tracer->trace([$span]); return $result; }
function it_traces_and_add_a_span_to_the_stack(SpanStack $spanStack, Tracer $tracer, HttpFoundationSpanFactory $httpFoundationSpanFactory, GetResponseEvent $event, Span $span) { $request = Request::create('/'); $event->getRequest()->willReturn($request); $event->isMasterRequest()->willReturn(true); $httpFoundationSpanFactory->fromIncomingRequest($request)->willReturn($span); $spanStack->push($span)->shouldBeCalled(); $tracer->trace([$span])->shouldBeCalled(); $this->onKernelRequest($event); }
function it_traces_the_outgoing_request(Psr7SpanFactory $psr7SpanFactory, Tracer $tracer, RequestInterface $request, PromiseInterface $promise) { $span = $this->generateSpan(); $psr7SpanFactory->fromOutgoingRequest($request)->shouldBeCalled()->willReturn($span); $tracer->trace([$span])->shouldBeCalled(); $middlewareFactory = $this->create(); $middleware = $middlewareFactory(function () use($promise) { 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; }); }; }; }
function it_should_add_the_trace_headers(ProducerInterface $decoratedProducer, AmqpSpanFactory $amqpSpanFactory, Tracer $tracer) { $span = new Span(Identifier::fromString('1234'), 'name', Identifier::fromString('trace')); $amqpSpanFactory->fromProducedMessage(Argument::type(AMQPMessage::class))->willReturn($span); $decoratedProducer->publish('', '', Argument::that(function (array $properties) { if (!array_key_exists('application_headers', $properties)) { return false; } $headers = $properties['application_headers']->getNativeData(); return isset($headers['X-B3-SpanId']) && isset($headers['X-B3-TraceId']); }))->shouldBeCalled(); $tracer->trace([$span])->shouldBeCalled(); $this->publish('', ''); }
function it_traces_the_span(ConsumerInterface $decoratedConsumer, Tracer $tracer) { $tracer->trace(Argument::containing(Argument::type(Span::class)))->shouldBeCalled(); $this->execute(new AMQPMessage('')); }