TraceContextCodec implements Codec<TextMap> { @Override public void inject(JaegerSpanContext spanContext, TextMap carrier) { char[] chars = new char[TRACEPARENT_HEADER_SIZE]; chars[0] = VERSION.charAt(0); chars[1] = VERSION.charAt(1); chars[2] = TRACEPARENT_DELIMITER; HexCodec.writeHexLong(chars, TRACE_ID_OFFSET, spanContext.getTraceIdHigh()); HexCodec.writeHexLong(chars, TRACE_ID_OFFSET + 16, spanContext.getTraceIdLow()); chars[SPAN_ID_OFFSET - 1] = TRACEPARENT_DELIMITER; HexCodec.writeHexLong(chars, SPAN_ID_OFFSET, spanContext.getSpanId()); chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER; chars[TRACE_OPTION_OFFSET] = '0'; chars[TRACE_OPTION_OFFSET + 1] = spanContext.isSampled() ? '1' : '0'; carrier.put(TRACE_PARENT, new String(chars)); if (spanContext.getTraceState() != null && !spanContext.getTraceState().isEmpty()) { carrier.put(TRACE_STATE, spanContext.getTraceState()); } } private  TraceContextCodec(Builder builder); @Override JaegerSpanContext extract(TextMap carrier); @Override void inject(JaegerSpanContext spanContext, TextMap carrier);  }
@Test public void testInject() { Map<String, String> carrier = new HashMap<>(); TextMapAdapter textMap = new TextMapAdapter(carrier); long traceIdLow = 1; long spanId = 2; long parentId = 3; long traceIdHigh = HexCodec.hexToUnsignedLong("c281c27976c85681", 0, 16); JaegerSpanContext spanContext = new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, (byte) 0); traceContextCodec.inject(spanContext, textMap); String expectedTraceContextHeader = "00-c281c27976c856810000000000000001-0000000000000002-00"; assertEquals(1, carrier.size()); assertNotNull(carrier.get(TRACE_PARENT)); assertEquals(expectedTraceContextHeader, carrier.get(TRACE_PARENT)); }