Skip to content

Commit

Permalink
Fixes bug where debug did not imply sampled (openzipkin#663)
Browse files Browse the repository at this point in the history
It is true the b3-propagation spec suggests debug implies sampled.
This leniently parses a missing `X-B3-Sampled` header as long as
`X-B3-Flags: 1` is present.

Fixes openzipkin#661
  • Loading branch information
adriancole committed Mar 21, 2018
1 parent 854a7c4 commit bcc97a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions brave-tests/src/test/java/brave/propagation/B3PropagationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ public class B3PropagationTest extends PropagationTest<String> {
assertThat(result)
.isEqualTo(SamplingFlags.EMPTY);
}

@Test public void extractTraceContext_debug_with_ids() {
MapEntry mapEntry = new MapEntry();
map.put("X-B3-TraceId", "463ac35c9f6413ad48485a3953bb6124"); // ok
map.put("X-B3-SpanId", "48485a3953bb6124"); // ok
map.put("X-B3-Flags", "1"); // accidentally missing sampled flag

TraceContext result = propagation().extractor(mapEntry).extract(map).context();

assertThat(result.sampled())
.isTrue();
}
}
3 changes: 3 additions & 0 deletions brave/src/main/java/brave/propagation/TraceIdContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@ InternalBuilder sampled(@Nullable Boolean sampled) {
return this;
}

/** Ensures sampled is set when debug is */
InternalBuilder debug(boolean debug) {
if (debug) {
flags |= FLAG_DEBUG;
flags |= FLAG_SAMPLED_SET;
flags |= FLAG_SAMPLED;
} else {
flags &= ~FLAG_DEBUG;
}
Expand Down
14 changes: 14 additions & 0 deletions brave/src/test/java/brave/propagation/TraceIdContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public class TraceIdContextTest {
.isEqualTo("00000000000000de000000000000014d");
}

@Test public void debugImpliesSampled() {
TraceIdContext primitives = context.toBuilder()
.debug(true)
.build();

TraceIdContext objects = context.toBuilder()
.sampled(Boolean.TRUE)
.debug(Boolean.TRUE)
.build();

assertThat(primitives)
.isEqualToComparingFieldByField(objects);
}

@Test public void canUsePrimitiveOverloads() {
TraceIdContext primitives = context.toBuilder()
.sampled(true)
Expand Down

0 comments on commit bcc97a2

Please sign in to comment.