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