function let(ConsumerInterface $decoratedConsumer, Tracer $tracer, SpanStack $spanStack, AmqpSpanFactory $amqpSpanFactory) { $span = new Span(Identifier::fromString('1234'), 'name', Identifier::fromString('1234')); $amqpSpanFactory->fromReceivedMessage(Argument::type(AMQPMessage::class))->willReturn($span); $amqpSpanFactory->fromConsumedMessage(Argument::type(AMQPMessage::class))->willReturn($span); $this->beConstructedWith($decoratedConsumer, $tracer, $spanStack, $amqpSpanFactory); }
/** * {@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_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('', ''); }
/** * {@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; }