Search in sources :

Example 86 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class ITTracingJMSConsumer method receiveResumesTrace.

void receiveResumesTrace(Runnable send) {
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    producer.setProperty("b3", parent.traceIdString() + "-" + parent.spanIdString() + "-1");
    send.run();
    Message received = consumer.receive();
    MutableSpan consumerSpan = testSpanHandler.takeRemoteSpan(CONSUMER);
    assertChildOf(consumerSpan, parent);
    assertThat(getPropertyIfString(received, "b3")).isEqualTo(parent.traceIdString() + "-" + consumerSpan.id() + "-1");
}
Also used : MutableSpan(brave.handler.MutableSpan) Message(javax.jms.Message) TraceContext(brave.propagation.TraceContext)

Example 87 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class ITTracingJMSConsumer method messageListener_resumesTrace.

void messageListener_resumesTrace(Runnable send) {
    consumer.setMessageListener(m -> {
        // clearing headers ensures later work doesn't try to use the old parent
        String b3 = getPropertyIfString(m, "b3");
        tracing.tracer().currentSpanCustomizer().tag("b3", String.valueOf(b3 != null));
    });
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    producer.setProperty("b3", parent.traceIdString() + "-" + parent.spanIdString() + "-1");
    send.run();
    MutableSpan consumerSpan = testSpanHandler.takeRemoteSpan(CONSUMER);
    MutableSpan listenerSpan = testSpanHandler.takeLocalSpan();
    assertChildOf(consumerSpan, parent);
    assertChildOf(listenerSpan, consumerSpan);
    assertThat(listenerSpan.tags()).hasSize(// no redundant copy of consumer tags
    1).containsEntry("b3", // b3 header not leaked to listener
    "false");
}
Also used : MutableSpan(brave.handler.MutableSpan) TraceContext(brave.propagation.TraceContext) MessageProperties.getPropertyIfString(brave.jms.MessageProperties.getPropertyIfString)

Example 88 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class TracingJMSConsumerTest method receive_continues_parent_trace_single_header.

@Test
public void receive_continues_parent_trace_single_header() throws Exception {
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setStringProperty("b3", B3SingleFormat.writeB3SingleFormatWithoutParentId(parent));
    receive(message);
    // Ensure the current span in on the message, not the parent
    MutableSpan consumer = testSpanHandler.takeRemoteSpan(CONSUMER);
    assertChildOf(consumer, parent);
    TraceContext messageContext = parseB3SingleFormat(message.getStringProperty("b3")).context();
    assertThat(messageContext.traceIdString()).isEqualTo(consumer.traceId());
    assertThat(messageContext.spanIdString()).isEqualTo(consumer.id());
}
Also used : MutableSpan(brave.handler.MutableSpan) TraceContext(brave.propagation.TraceContext) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Test(org.junit.Test)

Example 89 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class CurrentTraceContextTest method noticesDifferentSpanId.

void noticesDifferentSpanId(Scope scope) {
    TraceContext differentSpanId = context.toBuilder().spanId(context.spanId() + 1L).build();
    try (Scope scope2 = currentTraceContext.maybeScope(differentSpanId)) {
        assertThat(scope2).isNotEqualTo(Scope.NOOP);
        assertThat(currentTraceContext.get()).isEqualTo(differentSpanId);
        verifyImplicitContext(differentSpanId);
    } finally {
        scope.close();
    }
}
Also used : Scope(brave.propagation.CurrentTraceContext.Scope) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext)

Example 90 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class TracingMessagePostProcessor method postProcessMessage.

@Override
public Message postProcessMessage(Message message) {
    MessageProducerRequest request = new MessageProducerRequest(message);
    TraceContext maybeParent = currentTraceContext.get();
    // Unlike message consumers, we try current span before trying extraction. This is the proper
    // order because the span in scope should take precedence over a potentially stale header entry.
    // 
    // NOTE: Brave instrumentation used properly does not result in stale header entries, as we
    // always clear message headers after reading.
    Span span;
    if (maybeParent == null) {
        TraceContextOrSamplingFlags extracted = springRabbitTracing.extractAndClearTraceIdHeaders(extractor, request, message);
        span = springRabbitTracing.nextMessagingSpan(sampler, request, extracted);
    } else {
        // If we have a span in scope assume headers were cleared before
        span = tracer.newChild(maybeParent);
    }
    if (!span.isNoop()) {
        span.kind(PRODUCER).name("publish");
        if (remoteServiceName != null)
            span.remoteServiceName(remoteServiceName);
        // incur timestamp overhead only once
        long timestamp = tracing.clock(span.context()).currentTimeMicroseconds();
        span.start(timestamp).finish(timestamp);
    }
    injector.inject(span.context(), request);
    return message;
}
Also used : CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) Span(brave.Span) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Aggregations

TraceContext (brave.propagation.TraceContext)200 Test (org.junit.Test)163 CurrentTraceContext (brave.propagation.CurrentTraceContext)77 Scope (brave.propagation.CurrentTraceContext.Scope)52 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)52 MutableSpan (brave.handler.MutableSpan)38 Span (brave.Span)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 MockResponse (okhttp3.mockwebserver.MockResponse)12 SpanHandler (brave.handler.SpanHandler)9 Clock (brave.Clock)7 SamplingFlags (brave.propagation.SamplingFlags)7 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestSpanHandler (brave.test.TestSpanHandler)6 AssertableCallback (brave.test.util.AssertableCallback)6 Message (javax.jms.Message)6 After (org.junit.After)6