Skip to content

Commit

Permalink
Fixes length bug in b3 single format
Browse files Browse the repository at this point in the history
Thanks to @zyfjeff for finding this!

See envoyproxy/envoy#4712 (comment)
  • Loading branch information
Adrian Cole authored and adriancole committed Oct 17, 2018
1 parent 9155e14 commit 8ab16a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion brave/src/main/java/brave/propagation/B3SingleFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* <p>See <a href="https://github.com/openzipkin/b3-propagation">B3 Propagation</a>
*/
public final class B3SingleFormat {
static final int FORMAT_MAX_LENGTH = 32 + 1 + 16 + 2 + 16; // traceid128-spanid-1-parentid
static final int FORMAT_MAX_LENGTH = 32 + 1 + 16 + 3 + 16; // traceid128-spanid-1-parentid

/**
* Writes all B3 defined fields in the trace context, except {@link TraceContext#parentIdAsLong()
Expand Down
36 changes: 35 additions & 1 deletion brave/src/test/java/brave/propagation/B3SingleFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.assertj.core.api.Assertions.assertThat;

public class B3SingleFormatTest {
String traceIdHigh = "0000000000000009";
String traceId = "0000000000000001";
String parentId = "0000000000000002";
String spanId = "0000000000000003";
Expand All @@ -27,7 +28,7 @@ public class B3SingleFormatTest {
TraceContext context = TraceContext.newBuilder().traceIdHigh(9).traceId(1).spanId(3).build();

assertThat(writeB3SingleFormat(context))
.isEqualTo("0000000000000009" + traceId + "-" + spanId)
.isEqualTo(traceIdHigh + traceId + "-" + spanId)
.isEqualTo(new String(writeB3SingleFormatAsBytes(context), UTF_8));
}

Expand Down Expand Up @@ -64,6 +65,35 @@ public class B3SingleFormatTest {
.isEqualTo(new String(writeB3SingleFormatAsBytes(context), UTF_8));
}

@Test public void writeB3SingleFormat_largest() {
TraceContext context =
TraceContext.newBuilder()
.traceIdHigh(9)
.traceId(1)
.parentId(2)
.spanId(3)
.sampled(true)
.build();

assertThat(writeB3SingleFormat(context))
.isEqualTo(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId)
.isEqualTo(new String(writeB3SingleFormatAsBytes(context), UTF_8));
}

@Test public void parseB3SingleFormat_largest() {
assertThat(
parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId)
).extracting(TraceContextOrSamplingFlags::context).isEqualToComparingFieldByField(
TraceContext.newBuilder()
.traceIdHigh(9)
.traceId(1)
.parentId(2)
.spanId(3)
.sampled(true)
.build()
);
}

@Test public void writeB3SingleFormatWithoutParent_notYetSampled() {
TraceContext context = TraceContext.newBuilder().traceId(1).spanId(3).build();

Expand Down Expand Up @@ -244,5 +274,9 @@ public class B3SingleFormatTest {
// overall length is not ok
assertThat(parseB3SingleFormat(traceId + traceId + traceId + "-" + spanId + "-" + traceId))
.isNull();
// one character too long is not ok
assertThat(
parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId + "a")
).isNull();
}
}

0 comments on commit 8ab16a9

Please sign in to comment.