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);
}
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");
}
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);
}
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();
}
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();
}
Aggregations