TraceContextCodec implements Codec<TextMap> { @Override public JaegerSpanContext extract(TextMap carrier) { String traceParent = null; String traceState = null; String debugId = null; for (Map.Entry<String, String> entry: carrier) { if (TRACE_PARENT.equalsIgnoreCase(entry.getKey())) { traceParent = entry.getValue(); } if (TRACE_STATE.equalsIgnoreCase(entry.getKey())) { traceState = entry.getValue(); } if (Constants.DEBUG_ID_HEADER_KEY.equalsIgnoreCase(entry.getKey())) { debugId = entry.getValue(); } } if (traceParent == null) { if (debugId != null) { return objectFactory.createSpanContext(0L, 0L, 0L, 0L, (byte) 0, null, debugId); } return null; } return extractContextFromTraceParent(traceParent, traceState, debugId); } private  TraceContextCodec(Builder builder); @Override JaegerSpanContext extract(TextMap carrier); @Override void inject(JaegerSpanContext spanContext, TextMap carrier);  }
@Test public void support128BitTraceIdExtraction() { String hex128Bits = "463ac35c9f6413ad48485a3953bb6124"; String parentSpan = "d1595c6ec91668af"; String tracecontext = String.format("00-%s-%s-01", hex128Bits, parentSpan); TextMapAdapter textMap = new TextMapAdapter(new HashMap<>()); textMap.put(TRACE_PARENT, tracecontext); JaegerSpanContext context = traceContextCodec.extract(textMap); assertNotNull(HexCodec.lowerHexToUnsignedLong(parentSpan)); assertEquals(HexCodec.lowerHexToUnsignedLong(hex128Bits).longValue(), context.getTraceIdLow()); assertEquals(HexCodec.higherHexToUnsignedLong(hex128Bits).longValue(), context.getTraceIdHigh()); assertEquals(HexCodec.lowerHexToUnsignedLong(parentSpan).longValue(), context.getSpanId()); assertTrue(context.isSampled()); }
@Test public void testExtractWithCapitalizedTraceHeaders() { Map<String, String> extractCarrier = new HashMap<>(); TextMapAdapter textMap = new TextMapAdapter(extractCarrier); textMap.put("Traceparent", EXAMPLE_TRACE_PARENT); textMap.put("Tracestate", "whatever"); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); assertEquals("0000000000000001:0000000000000002:0:0", spanContext.toString()); assertEquals("whatever", spanContext.getTraceState()); }
@Test public void testInvalidTraceId() { TextMapAdapter textMap = new TextMapAdapter(new HashMap<>()); textMap.put(TRACE_PARENT, "00-00000000000000000000000000000000-0000000000000002-00"); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); assertNull(spanContext); verifyWarningPresent(); }
@Test public void testNoTraceHeader() { TextMapAdapter textMap = new TextMapAdapter(new HashMap<>()); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); assertNull(spanContext); verifyWarningNotPresent(); }
@Test public void testInvalidParentId() { TextMapAdapter textMap = new TextMapAdapter(new HashMap<>()); textMap.put(TRACE_PARENT, "00-00000000000000000000000000000001-0000000000000000-00"); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); assertNull(spanContext); verifyWarningPresent(); }
@Test public void testDebugIdWithoutTraceHeader() { Map<String, String> extractCarrier = new HashMap<>(); TextMapAdapter textMap = new TextMapAdapter(extractCarrier); textMap.put(Constants.DEBUG_ID_HEADER_KEY, EXAMPLE_DEBUG_ID); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); JaegerTracer tracer = new JaegerTracer.Builder("service").withReporter(new InMemoryReporter()).build(); JaegerSpan child = tracer.buildSpan("span").asChildOf(spanContext).start(); assertTrue(child.context().isDebug()); child.finish(); tracer.close(); }
@Test public void testDebugIdWithTraceHeader() { Map<String, String> extractCarrier = new HashMap<>(); TextMapAdapter textMap = new TextMapAdapter(extractCarrier); textMap.put(TRACE_PARENT, EXAMPLE_TRACE_PARENT); textMap.put(Constants.DEBUG_ID_HEADER_KEY, EXAMPLE_DEBUG_ID); JaegerSpanContext spanContext = traceContextCodec.extract(textMap); JaegerTracer tracer = new JaegerTracer.Builder("service").withReporter(new InMemoryReporter()).build(); assertEquals("0000000000000001", spanContext.getTraceId()); JaegerSpan child = tracer.buildSpan("span").asChildOf(spanContext).start(); assertFalse(child.context().isDebug()); child.finish(); tracer.close(); }