Search in sources :

Example 1 with TraceIdContext

use of brave.propagation.TraceIdContext 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 2 with TraceIdContext

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

the class TracerTest method nextSpan_extractedTraceId.

@Test
public void nextSpan_extractedTraceId() {
    TraceIdContext traceIdContext = TraceIdContext.newBuilder().traceId(1L).build();
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(traceIdContext);
    assertThat(tracer.nextSpan(extracted).context().traceId()).isEqualTo(1L);
}
Also used : TraceIdContext(brave.propagation.TraceIdContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 3 with TraceIdContext

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

the class TracerTest method nextSpan_extractedTraceId_extra.

@Test
public void nextSpan_extractedTraceId_extra() {
    TraceIdContext traceIdContext = TraceIdContext.newBuilder().traceId(1L).build();
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(traceIdContext).toBuilder().addExtra(1L).build();
    assertThat(tracer.nextSpan(extracted).context().extra()).containsExactly(1L);
}
Also used : TraceIdContext(brave.propagation.TraceIdContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Aggregations

TraceIdContext (brave.propagation.TraceIdContext)3 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)2 Test (org.junit.Test)2 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 TraceContext (brave.propagation.TraceContext)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1