B3SingleFormat { @Nullable public static TraceContextOrSamplingFlags parseB3SingleFormat(CharSequence b3) { return parseB3SingleFormat(b3, 0, b3.length()); } B3SingleFormat(); static String writeB3SingleFormatWithoutParentId(TraceContext context); static byte[] writeB3SingleFormatWithoutParentIdAsBytes(TraceContext context); static String writeB3SingleFormat(TraceContext context); static byte[] writeB3SingleFormatAsBytes(TraceContext context); @Nullable static TraceContextOrSamplingFlags parseB3SingleFormat(CharSequence b3); @Nullable static TraceContextOrSamplingFlags parseB3SingleFormat(CharSequence value, int beginIndex,
    int endIndex);  }
@Test public void parseB3SingleFormat_largest() { assertThat( parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId).context() ).isEqualToComparingFieldByField(TraceContext.newBuilder() .traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16)) .traceId(Long.parseUnsignedLong(traceId, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .sampled(true).build() ); }
@Test public void parseB3SingleFormat_padded() { assertThat( parseB3SingleFormat("0000000000000000" + traceId + "-" + spanId + "-1-" + parentId).context() ).isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .sampled(true).build() ); }
@Test public void parseTraceparentFormat_padded_right() { assertThat( parseB3SingleFormat(traceIdHigh + "0000000000000000-" + spanId + "-1-" + parentId).context() ).isEqualToComparingFieldByField(TraceContext.newBuilder() .traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .sampled(true).build() ); }
@Test public void parseB3SingleFormat_middleOfString() { String input = "b3=" + traceIdHigh + traceId + "-" + spanId + ","; assertThat(parseB3SingleFormat(input, 3, input.length() - 1).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16)) .traceId(Long.parseUnsignedLong(traceId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)).build() ); }
@Test public void parseB3SingleFormat_middleOfString_debugOnly() { String input = "b2=foo,b3=d,b4=bar"; assertThat(parseB3SingleFormat(input, 10, 11).samplingFlags()) .isSameAs(SamplingFlags.DEBUG); }
@Test public void parseB3SingleFormat_spanIdsNotYetSampled() { assertThat(parseB3SingleFormat(traceId + "-" + spanId).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)).build() ); }
@Test public void parseB3SingleFormat_spanIdsNotYetSampled128() { assertThat(parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16)) .traceId(Long.parseUnsignedLong(traceId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)).build() ); }
@Test public void parseB3SingleFormat_spanIdsUnsampled() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-0").context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .sampled(false).build() ); }
@Test public void parseB3SingleFormat_parent_unsampled() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-0-" + parentId).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .sampled(false).build() ); }
@Test public void parseB3SingleFormat_parent_debug() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-d-" + parentId).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .debug(true).build() ); }
@Test public void parseB3SingleFormat_parentid_notYetSampled() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-" + parentId).context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .parentId(Long.parseUnsignedLong(parentId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)).build() ); }
@Test public void parseB3SingleFormat_spanIdsWithDebug() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-d").context()) .isEqualToComparingFieldByField(TraceContext.newBuilder() .traceId(Long.parseUnsignedLong(traceId, 16)) .spanId(Long.parseUnsignedLong(spanId, 16)) .debug(true).build() ); }
@Test public void parseB3SingleFormat_sampledFalse() { assertThat(parseB3SingleFormat("0")) .isEqualTo(TraceContextOrSamplingFlags.NOT_SAMPLED); }
@Test public void parseB3SingleFormat_sampled() { assertThat(parseB3SingleFormat("1")) .isEqualTo(TraceContextOrSamplingFlags.SAMPLED); }
@Test public void parseB3SingleFormat_debug() { assertThat(parseB3SingleFormat("d")) .isEqualTo(TraceContextOrSamplingFlags.DEBUG); }
@Test public void parseB3SingleFormat_ignoresBeforeAndAfter() { String encoded = traceId + "-" + spanId; String sequence = "??" + encoded + "??"; assertThat(parseB3SingleFormat(sequence, 2, 2 + encoded.length())) .isEqualToComparingFieldByField(parseB3SingleFormat(encoded)); }
@Test public void parseB3SingleFormat_zero_parentId() { assertThat(parseB3SingleFormat(traceId + "-" + spanId + "-1-0000000000000000").context()) .isEqualToComparingFieldByField( parseB3SingleFormat(traceId + "-" + spanId + "-1").context() ); }