Search in sources :

Example 11 with TraceContext

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

the class KafkaTracingTest method joinSpan_should_retrieve_span_from_headers.

@Test
public void joinSpan_should_retrieve_span_from_headers() {
    addB3Headers(fakeRecord);
    Span span = kafkaTracing.joinSpan(fakeRecord);
    TraceContext context = span.context();
    assertThat(HexCodec.toLowerHex(context.traceId())).isEqualTo(TRACE_ID);
    assertThat(HexCodec.toLowerHex(context.spanId())).isEqualTo(SPAN_ID);
    assertThat(context.sampled()).isEqualTo(true);
}
Also used : TraceContext(brave.propagation.TraceContext) Span(brave.Span) Test(org.junit.Test)

Example 12 with TraceContext

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

the class Tracer method nextSpan.

/**
 * This creates a new span based on parameters extracted from an incoming request. This will
 * always result in a new span. If no trace identifiers were extracted, a span will be created
 * based on the implicit context in the same manner as {@link #nextSpan()}. If a sampling decision
 * has not yet been made, one will happen here.
 *
 * <p>Ex.
 * <pre>{@code
 * extracted = extractor.extract(request);
 * span = tracer.nextSpan(extracted);
 * }</pre>
 *
 * <p><em>Note:</em> Unlike {@link #joinSpan(TraceContext)}, this does not attempt to re-use
 * extracted span IDs. This means the extracted context (if any) is the parent of the span
 * returned.
 *
 * <p><em>Note:</em> If a context could be extracted from the input, that trace is resumed, not
 * whatever the {@link #currentSpan()} was. Make sure you re-apply {@link #withSpanInScope(Span)}
 * so that data is written to the correct trace.
 *
 * @see Propagation
 * @see Extractor#extract(Object)
 * @see TraceContextOrSamplingFlags
 */
public Span nextSpan(TraceContextOrSamplingFlags extracted) {
    TraceContext parent = extracted.context();
    if (extracted.samplingFlags() != null) {
        TraceContext implicitParent = currentTraceContext.get();
        if (implicitParent == null) {
            return toSpan(newRootContext(extracted.samplingFlags(), extracted.extra()));
        }
        // fall through, with an implicit parent, not an extracted one
        parent = appendExtra(implicitParent, extracted.extra());
    }
    long nextId = nextId();
    if (parent != null) {
        Boolean sampled = parent.sampled();
        if (sampled == null)
            sampled = sampler.isSampled(parent.traceId());
        return toSpan(// copies "extra" from the parent
        parent.toBuilder().spanId(nextId).parentId(parent.spanId()).sampled(sampled).build());
    }
    TraceIdContext traceIdContext = extracted.traceIdContext();
    if (extracted.traceIdContext() != null) {
        Boolean sampled = traceIdContext.sampled();
        if (sampled == null)
            sampled = sampler.isSampled(traceIdContext.traceId());
        return toSpan(TraceContext.newBuilder().sampled(sampled).debug(traceIdContext.debug()).traceIdHigh(traceIdContext.traceIdHigh()).traceId(traceIdContext.traceId()).spanId(nextId).extra(extracted.extra()).build());
    }
    // TraceContextOrSamplingFlags is a union of 3 types, we've checked all three
    throw new AssertionError("should not reach here");
}
Also used : CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TraceIdContext(brave.propagation.TraceIdContext)

Example 13 with TraceContext

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

the class TracerTest method newChild.

@Test
public void newChild() {
    TraceContext parent = tracer.newTrace().context();
    assertThat(tracer.newChild(parent)).satisfies(c -> {
        assertThat(c.context().traceIdString()).isEqualTo(parent.traceIdString());
        assertThat(c.context().parentId()).isEqualTo(parent.spanId());
    }).isInstanceOf(RealSpan.class);
}
Also used : HexCodec(brave.internal.HexCodec) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) TraceIdContext(brave.propagation.TraceIdContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) ArrayList(java.util.ArrayList) List(java.util.List) Sampler(brave.sampler.Sampler) Endpoint(zipkin2.Endpoint) Arrays.asList(java.util.Arrays.asList) B3Propagation(brave.propagation.B3Propagation) After(org.junit.After) Propagation(brave.propagation.Propagation) Reporter(zipkin2.reporter.Reporter) SamplingFlags(brave.propagation.SamplingFlags) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 14 with TraceContext

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

the class TracerTest method newChild_ensuresSampling.

@Test
public void newChild_ensuresSampling() {
    TraceContext notYetSampled = tracer.newTrace().context().toBuilder().sampled(null).build();
    assertThat(tracer.newChild(notYetSampled).context().sampled()).isTrue();
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 15 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) TraceContext(brave.propagation.TraceContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Aggregations

TraceContext (brave.propagation.TraceContext)54 Test (org.junit.Test)46 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)21 CurrentTraceContext (brave.propagation.CurrentTraceContext)10 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Span (brave.Span)4 Tracer (brave.Tracer)4 Tracing (brave.Tracing)4 ThreadContextCurrentTraceContext (brave.context.log4j2.ThreadContextCurrentTraceContext)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 After (org.junit.After)4 Endpoint (zipkin2.Endpoint)4 Span (zipkin2.Span)4 Platform (brave.internal.Platform)3 Propagation (brave.propagation.Propagation)3 Reference (java.lang.ref.Reference)3