@Test public void testFromHeader() { TestTraceOptionsFactory optionsFactory = new TestTraceOptionsFactory(); SpanContextFactory factory = new SpanContextFactory(optionsFactory, new SequentialTraceIdFactory(), new SequentialSpanIdFactory()); optionsFactory.toReturn = TraceOptions.forTraceEnabled(); SpanContext empty = factory.fromHeader(""); assertThat(empty.getTraceId().isValid()).isFalse(); assertThat(empty.getSpanId().isValid()).isFalse(); assertThat(empty.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext badTraceId = factory.fromHeader("xyz"); assertThat(badTraceId.getTraceId().isValid()).isFalse(); assertThat(badTraceId.getSpanId().isValid()).isFalse(); assertThat(badTraceId.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext invalidTraceId = factory.fromHeader("fffffffffffffffffffffffffffffffff"); assertThat(invalidTraceId.getTraceId().isValid()).isFalse(); assertThat(invalidTraceId.getSpanId().isValid()).isFalse(); assertThat(invalidTraceId.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext badSpanId = factory.fromHeader("123456789abcdef0123456789abcdef0/m"); assertThat(badSpanId.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(badSpanId.getSpanId().isValid()).isFalse(); assertThat(badSpanId.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext invalidSpanId = factory.fromHeader("123456789abcdef0123456789abcdef0/0"); assertThat(invalidSpanId.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(invalidSpanId.getSpanId().isValid()).isFalse(); assertThat(invalidSpanId.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext badOptions = factory.fromHeader("123456789abcdef0123456789abcdef0/13;o=q"); assertThat(badOptions.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(badOptions.getSpanId()).isEqualTo(new SpanId(13)); assertThat(badOptions.getTraceOptions().getOptionsMask()).isEqualTo(1); SpanContext context = factory.fromHeader("123456789abcdef0123456789abcdef0/13;o=3"); assertThat(context.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(context.getSpanId()).isEqualTo(new SpanId(13)); assertThat(context.getTraceOptions().getOptionsMask()).isEqualTo(3); SpanContext contextPre = factory.fromHeader("123456789abcdef0123456789abcdef0/13;x=u;o=3"); assertThat(contextPre.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(contextPre.getSpanId()).isEqualTo(new SpanId(13)); assertThat(contextPre.getTraceOptions().getOptionsMask()).isEqualTo(3); SpanContext contextPost = factory.fromHeader("123456789abcdef0123456789abcdef0/13;o=3;x=u"); assertThat(contextPost.getTraceId()) .isEqualTo(new TraceId(new BigInteger("123456789abcdef0123456789abcdef0", 16))); assertThat(contextPost.getSpanId()).isEqualTo(new SpanId(13)); assertThat(contextPost.getTraceOptions().getOptionsMask()).isEqualTo(3); }
public SpanContext fromHeader(String header) { int index = header.indexOf('/'); if (index == -1) { TraceId traceId = parseTraceId(header); return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } TraceId traceId = parseTraceId(header.substring(0, index)); if (!traceId.isValid()) { return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } String[] afterTraceId = header.substring(index + 1).split(";"); SpanId spanId = parseSpanId(afterTraceId[0]); TraceOptions traceOptions = null; for (int i = 1; i < afterTraceId.length; i++) { if (afterTraceId[i].startsWith("o=")) { traceOptions = parseTraceOptions(afterTraceId[i].substring(2)); } } if (traceOptions == null) { traceOptions = traceOptionsFactory.create(); } return new SpanContext(traceId, spanId, traceOptions); }
SpanContextFactory { public SpanContext fromHeader(String header) { int index = header.indexOf('/'); if (index == -1) { TraceId traceId = parseTraceId(header); return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } TraceId traceId = parseTraceId(header.substring(0, index)); if (!traceId.isValid()) { return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } String[] afterTraceId = header.substring(index + 1).split(";"); SpanId spanId = parseSpanId(afterTraceId[0]); TraceOptions traceOptions = null; for (int i = 1; i < afterTraceId.length; i++) { if (afterTraceId[i].startsWith("o=")) { traceOptions = parseTraceOptions(afterTraceId[i].substring(2)); } } if (traceOptions == null) { traceOptions = traceOptionsFactory.create(); } return new SpanContext(traceId, spanId, traceOptions); } }
SpanContextFactory { public SpanContext fromHeader(String header) { int index = header.indexOf('/'); if (index == -1) { TraceId traceId = parseTraceId(header); return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } TraceId traceId = parseTraceId(header.substring(0, index)); if (!traceId.isValid()) { return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } String[] afterTraceId = header.substring(index + 1).split(";"); SpanId spanId = parseSpanId(afterTraceId[0]); TraceOptions traceOptions = null; for (int i = 1; i < afterTraceId.length; i++) { if (afterTraceId[i].startsWith("o=")) { traceOptions = parseTraceOptions(afterTraceId[i].substring(2)); } } if (traceOptions == null) { traceOptions = traceOptionsFactory.create(); } return new SpanContext(traceId, spanId, traceOptions); } SpanContextFactory(TraceOptionsFactory traceOptionsFactory);  SpanContextFactory(TraceOptionsFactory traceOptionsFactory,
      IdFactory<TraceId> traceIdFactory, IdFactory<SpanId> spanIdFactory); }
SpanContextFactory { public SpanContext fromHeader(String header) { int index = header.indexOf('/'); if (index == -1) { TraceId traceId = parseTraceId(header); return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } TraceId traceId = parseTraceId(header.substring(0, index)); if (!traceId.isValid()) { return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } String[] afterTraceId = header.substring(index + 1).split(";"); SpanId spanId = parseSpanId(afterTraceId[0]); TraceOptions traceOptions = null; for (int i = 1; i < afterTraceId.length; i++) { if (afterTraceId[i].startsWith("o=")) { traceOptions = parseTraceOptions(afterTraceId[i].substring(2)); } } if (traceOptions == null) { traceOptions = traceOptionsFactory.create(); } return new SpanContext(traceId, spanId, traceOptions); } SpanContextFactory(TraceOptionsFactory traceOptionsFactory);  SpanContextFactory(TraceOptionsFactory traceOptionsFactory,
      IdFactory<TraceId> traceIdFactory, IdFactory<SpanId> spanIdFactory); static String headerKey(); SpanContext childContext(SpanContext parentContext); SpanContext initialContext(); SpanContext fromHeader(String header); static String toHeader(SpanContext context); }
SpanContextFactory { public SpanContext fromHeader(String header) { int index = header.indexOf('/'); if (index == -1) { TraceId traceId = parseTraceId(header); return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } TraceId traceId = parseTraceId(header.substring(0, index)); if (!traceId.isValid()) { return new SpanContext(traceId, SpanId.invalid(), traceOptionsFactory.create()); } String[] afterTraceId = header.substring(index + 1).split(";"); SpanId spanId = parseSpanId(afterTraceId[0]); TraceOptions traceOptions = null; for (int i = 1; i < afterTraceId.length; i++) { if (afterTraceId[i].startsWith("o=")) { traceOptions = parseTraceOptions(afterTraceId[i].substring(2)); } } if (traceOptions == null) { traceOptions = traceOptionsFactory.create(); } return new SpanContext(traceId, spanId, traceOptions); } SpanContextFactory(TraceOptionsFactory traceOptionsFactory);  SpanContextFactory(TraceOptionsFactory traceOptionsFactory,
      IdFactory<TraceId> traceIdFactory, IdFactory<SpanId> spanIdFactory); static String headerKey(); SpanContext childContext(SpanContext parentContext); SpanContext initialContext(); SpanContext fromHeader(String header); static String toHeader(SpanContext context);  }