TextMapCodec implements Codec<TextMap> { @Override public JaegerSpanContext extract(TextMap carrier) { JaegerSpanContext context = null; Map<String, String> baggage = null; String debugId = null; for (Map.Entry<String, String> entry : carrier) { String key = entry.getKey().toLowerCase(Locale.ROOT); if (key.equals(contextKey)) { context = contextFromString(decodedValue(entry.getValue())); } else if (key.equals(Constants.DEBUG_ID_HEADER_KEY)) { debugId = decodedValue(entry.getValue()); } else if (key.startsWith(baggagePrefix)) { if (baggage == null) { baggage = new HashMap<String, String>(); } baggage.put(keys.unprefixedKey(key, baggagePrefix), decodedValue(entry.getValue())); } else if (key.equals(Constants.BAGGAGE_HEADER_KEY)) { baggage = parseBaggageHeader(decodedValue(entry.getValue()), baggage); } } if (debugId == null && baggage == null) { return context; } return objectFactory.createSpanContext( context == null ? 0L : context.getTraceIdHigh(), context == null ? 0L : context.getTraceIdLow(), context == null ? 0L : context.getSpanId(), context == null ? 0L : context.getParentId(), context == null ? (byte)0 : context.getFlags(), baggage, debugId); } TextMapCodec(boolean urlEncoding); private  TextMapCodec(Builder builder); static String contextAsString(JaegerSpanContext context); @Override void inject(JaegerSpanContext spanContext, TextMap carrier); @Override JaegerSpanContext extract(TextMap carrier); @Override String toString(); static Builder builder();  }
@Test public void testAdhocBaggageWithoutTraceId() { Map<String, String> headers = new HashMap<String, String>(); headers.put("jaeger-baggage", "k1=v1, k2 = v2, k3=v3=d3"); TextMapCodec codec = new TextMapCodec(false); JaegerSpanContext context = codec.extract(new TextMapAdapter(headers)); assertEquals("v1", context.getBaggageItem("k1")); assertEquals("v2", context.getBaggageItem("k2")); assertEquals(null, context.getBaggageItem("k3")); }
@Test public void testExtractSupportNonEncodedSpanContext() { Map<String, String> headers = new HashMap<String, String>(); headers.put("uber-trace-id", "2a:1:0:1"); TextMapCodec codec = new TextMapCodec(true); JaegerSpanContext context = codec.extract(new TextMapAdapter(headers)); assertEquals(42, context.getTraceIdLow()); assertEquals(0L, context.getTraceIdHigh()); assertEquals(1L, context.getSpanId()); assertTrue(context.isSampled()); }
@Test public void testExtractSupportEncodedSpanContext() { Map<String, String> headers = new HashMap<String, String>(); headers.put("uber-trace-id", "2a%3A1%3A0%3A1"); TextMapCodec codec = new TextMapCodec(true); JaegerSpanContext context = codec.extract(new TextMapAdapter(headers)); assertEquals(42, context.getTraceIdLow()); assertEquals(0L, context.getTraceIdHigh()); assertEquals(1L, context.getSpanId()); assertTrue(context.isSampled()); }