Search in sources :

Example 61 with TraceContext

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

the class TracerTest method nextSpan_ensuresSampling_whenCreatingNewChild.

@Test
public void nextSpan_ensuresSampling_whenCreatingNewChild() {
    TraceContext notYetSampled = tracer.newTrace().context().toBuilder().sampled(null).build();
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(notYetSampled);
    assertThat(tracer.nextSpan(extracted).context().sampled()).isTrue();
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 62 with TraceContext

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

the class TracerTest method join_setsShared.

/**
 * When we join a sampled request, we are sharing the same trace identifiers.
 */
@Test
public void join_setsShared() {
    TraceContext fromIncomingRequest = tracer.newTrace().context();
    assertThat(fromIncomingRequest.shared()).isFalse();
    assertThat(fromIncomingRequest.isLocalRoot()).isTrue();
    TraceContext joined = tracer.joinSpan(fromIncomingRequest).context();
    assertThat(joined.shared()).isTrue();
    assertThat(joined.isLocalRoot()).isFalse();
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 63 with TraceContext

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

the class HackedTraceIdTest method testFormatThatEmbedsEnvironmentNumber.

// Let's say environment number zero is invalid, and its desired value is 3
// There are at least 3 ways to embed this!
@Test
public void testFormatThatEmbedsEnvironmentNumber() {
    // Lead with single-digit, then pad-right zeros until the real trace ID.
    // 
    // This is not great because it limits to 9 environment numbers. However, parsing is easy
    // as you look only at the first character.
    headers.put(customTraceIdName, "3000000000000000e457b5a2e4d86bd1");
    assertThat(extractor.extract(headers).context()).satisfies(c -> assertThat(c.traceIdString()).isEqualTo("3000000000000000e457b5a2e4d86bd1")).satisfies(c -> assertThat(c.spanIdString()).isEqualTo("e457b5a2e4d86bd1"));
    headers.put(customTraceIdName, "3000000000000000");
    assertThat(extractor.extract(headers).context()).satisfies(c -> assertThat(c.traceIdString()).isEqualTo("3000000000000000")).satisfies(c -> assertThat(c.spanIdString()).isEqualTo("3000000000000000"));
    // Use the upper 64-bits (left 16 hex) as the environment ID
    // 
    // This allows a lot of env numbers, and is easy to parse. This still gives 64-bit trace IDs
    headers.put(customTraceIdName, "0000000000000003e457b5a2e4d86bd1");
    assertThat(extractor.extract(headers).context()).satisfies(c -> assertThat(c.traceIdString()).isEqualTo("0000000000000003e457b5a2e4d86bd1")).satisfies(c -> assertThat(c.spanIdString()).isEqualTo("e457b5a2e4d86bd1"));
    headers.put(customTraceIdName, "3e457b5a2e4d86bd1");
    assertThat(extractor.extract(headers).context()).satisfies(c -> assertThat(c.traceIdString()).isEqualTo("0000000000000003e457b5a2e4d86bd1")).satisfies(c -> assertThat(c.spanIdString()).isEqualTo("e457b5a2e4d86bd1"));
    // Steal the upper nibble (first hex character) of the 64-bit trace ID as the environment ID
    // 
    // This allows 15 env numbers, and is easy to parse. It allows 60-bits for the trace ID, which
    // is good enough for most sites.
    String customTraceIdString = "3457b5a2e4d86bd1";
    headers.put(customTraceIdName, customTraceIdString);
    TraceContext extractedContext = extractor.extract(headers).context();
    assertThat(extractedContext).satisfies(c -> assertThat(c.traceIdString()).isEqualTo(customTraceIdString)).satisfies(c -> assertThat(c.spanIdString()).isEqualTo(customTraceIdString)).satisfies(c -> assertThat((c.traceId() >>> 64 - 4L) & 0xf).isEqualTo(3));
}
Also used : TreeMap(java.util.TreeMap) B3Propagation(brave.propagation.B3Propagation) Map(java.util.Map) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Propagation(brave.propagation.Propagation) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) Extractor(brave.propagation.TraceContext.Extractor) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 64 with TraceContext

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

the class TracingTest method spanHandler_dataChangesVisibleToZipkin.

@Test
public void spanHandler_dataChangesVisibleToZipkin() {
    String serviceNameOverride = "favistar";
    SpanHandler spanHandler = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            span.localServiceName(serviceNameOverride);
            return true;
        }
    };
    List<zipkin2.Span> zipkinSpans = new ArrayList<>();
    try (Tracing tracing = Tracing.newBuilder().spanReporter(zipkinSpans::add).addSpanHandler(spanHandler).build()) {
        tracing.tracer().newTrace().start().finish();
    }
    assertThat(zipkinSpans.get(0).localServiceName()).isEqualTo(serviceNameOverride);
}
Also used : MutableSpan(brave.handler.MutableSpan) ArrayList(java.util.ArrayList) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TestSpanHandler(brave.test.TestSpanHandler) FinishedSpanHandler(brave.handler.FinishedSpanHandler) SpanHandler(brave.handler.SpanHandler) MutableSpan(brave.handler.MutableSpan) Test(org.junit.Test)

Example 65 with TraceContext

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

the class TracingTest method spanHandler_multiple.

@Test
public void spanHandler_multiple() {
    SpanHandler one = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            return true;
        }
    };
    SpanHandler two = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            return true;
        }
    };
    try (Tracing tracing = Tracing.newBuilder().addSpanHandler(one).addSpanHandler(two).build()) {
        assertThat((Object) tracing.tracer().pendingSpans).extracting("spanHandler.delegate.handlers").asInstanceOf(InstanceOfAssertFactories.array(SpanHandler[].class)).containsExactly(one, two);
    }
}
Also used : MutableSpan(brave.handler.MutableSpan) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TestSpanHandler(brave.test.TestSpanHandler) FinishedSpanHandler(brave.handler.FinishedSpanHandler) SpanHandler(brave.handler.SpanHandler) Test(org.junit.Test)

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