Search in sources :

Example 41 with Message

use of org.springframework.amqp.core.Message in project brave by openzipkin.

the class TracingRabbitListenerAdviceTest method batch_continue_parent_trace.

@Test
public void batch_continue_parent_trace() throws Throwable {
    MessageProperties props = new MessageProperties();
    props.setHeader("X-B3-TraceId", TRACE_ID);
    props.setHeader("X-B3-SpanId", SPAN_ID);
    props.setHeader("X-B3-ParentSpanId", PARENT_ID);
    props.setHeader("X-B3-Sampled", SAMPLED);
    Message message = MessageBuilder.withBody(new byte[0]).andProperties(props).build();
    MessageProperties props2 = new MessageProperties();
    props2.setHeader("X-B3-TraceId", TRACE_ID_2);
    props2.setHeader("X-B3-SpanId", SPAN_ID_2);
    props2.setHeader("X-B3-ParentSpanId", PARENT_ID_2);
    props2.setHeader("X-B3-Sampled", SAMPLED);
    Message message2 = MessageBuilder.withBody(new byte[0]).andProperties(props2).build();
    onBatchMessageConsumed(Arrays.asList(message, message2));
    // cleared the headers to later work doesn't try to use the old parent
    assertThat(message.getMessageProperties().getHeaders()).isEmpty();
    // two traced that listener continues first trace but TODO we aren't handling the second.
    assertThat(spans.get(0)).extracting(MutableSpan::parentId).isEqualTo(SPAN_ID);
}
Also used : Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 42 with Message

use of org.springframework.amqp.core.Message in project brave by openzipkin.

the class TracingRabbitListenerAdvice method invoke.

/**
 * MethodInterceptor for {@link SimpleMessageListenerContainer.ContainerDelegate#invokeListener(Channel,
 * Message)}
 */
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    Message message = null;
    if (methodInvocation.getArguments()[1] instanceof List) {
        message = ((List<? extends Message>) methodInvocation.getArguments()[1]).get(0);
    } else {
        message = (Message) methodInvocation.getArguments()[1];
    }
    MessageConsumerRequest request = new MessageConsumerRequest(message);
    TraceContextOrSamplingFlags extracted = springRabbitTracing.extractAndClearTraceIdHeaders(extractor, request, message);
    // named for BlockingQueueConsumer.nextMessage, which we can't currently see
    Span consumerSpan = springRabbitTracing.nextMessagingSpan(sampler, request, extracted);
    Span listenerSpan = tracer.newChild(consumerSpan.context());
    if (!consumerSpan.isNoop()) {
        setConsumerSpan(consumerSpan, message.getMessageProperties());
        // incur timestamp overhead only once
        long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds();
        consumerSpan.start(timestamp);
        // save a clock reading
        long consumerFinish = timestamp + 1L;
        consumerSpan.finish(consumerFinish);
        // not using scoped span as we want to start with a pre-configured time
        listenerSpan.name("on-message").start(consumerFinish);
    }
    Tracer.SpanInScope ws = tracer.withSpanInScope(listenerSpan);
    Throwable error = null;
    try {
        return methodInvocation.proceed();
    } catch (Throwable t) {
        error = t;
        throw t;
    } finally {
        if (error != null)
            listenerSpan.error(error);
        listenerSpan.finish();
        ws.close();
    }
}
Also used : Message(org.springframework.amqp.core.Message) Tracer(brave.Tracer) List(java.util.List) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span)

Example 43 with Message

use of org.springframework.amqp.core.Message in project brave by openzipkin.

the class ITSpringRabbitTracing method clears_message_headers_after_propagation.

@Test
public void clears_message_headers_after_propagation() {
    produceMessage();
    awaitMessageConsumed();
    Message capturedMessage = awaitMessageConsumed();
    Map<String, Object> headers = capturedMessage.getMessageProperties().getHeaders();
    assertThat(headers.keySet()).containsExactly("not-zipkin-header");
    producerSpanHandler.takeRemoteSpan(PRODUCER);
    consumerSpanHandler.takeRemoteSpan(CONSUMER);
    consumerSpanHandler.takeLocalSpan();
}
Also used : Message(org.springframework.amqp.core.Message) Test(org.junit.Test)

Aggregations

Message (org.springframework.amqp.core.Message)43 Test (org.junit.Test)38 MessageProperties (org.springframework.amqp.core.MessageProperties)15 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)6 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)5 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 AmqpInboundChannelAdapter (org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 AsyncRabbitTemplate (org.springframework.amqp.rabbit.AsyncRabbitTemplate)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Channel (com.rabbitmq.client.Channel)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Advice (org.aopalliance.aop.Advice)2 AmqpTemplate (org.springframework.amqp.core.AmqpTemplate)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)2