B3TextMapCodec implements Codec<TextMap> { @Override public void inject(JaegerSpanContext spanContext, TextMap carrier) { carrier.put(TRACE_ID_NAME, HexCodec.toLowerHex(spanContext.getTraceIdHigh(), spanContext.getTraceIdLow())); if (spanContext.getParentId() != 0L) { carrier.put(PARENT_SPAN_ID_NAME, HexCodec.toLowerHex(spanContext.getParentId())); } carrier.put(SPAN_ID_NAME, HexCodec.toLowerHex(spanContext.getSpanId())); carrier.put(SAMPLED_NAME, spanContext.isSampled() ? "1" : "0"); if (spanContext.isDebug()) { carrier.put(FLAGS_NAME, "1"); } for (Map.Entry<String, String> entry : spanContext.baggageItems()) { carrier.put(keys.prefixedKey(entry.getKey(), baggagePrefix), entry.getValue()); } } @Deprecated  B3TextMapCodec(); private  B3TextMapCodec(Builder builder); @Override void inject(JaegerSpanContext spanContext, TextMap carrier); @Override JaegerSpanContext extract(TextMap carrier);  }
@Test public void testDefault() { B3TextMapCodec b3Codec = new B3TextMapCodec.Builder() .build(); DelegatingTextMap entries = new DelegatingTextMap(); long traceIdLow = 1; long spanId = 2; long parentId = 3; JaegerSpanContext spanContext = new JaegerSpanContext(0L, traceIdLow, spanId, parentId, (byte)0) .withBaggageItem("foo", "bar"); b3Codec.inject(spanContext, entries); assertEquals(5, entries.delegate.size()); assertNotNull(entries.delegate.get(B3TextMapCodec.TRACE_ID_NAME)); assertNotNull(entries.delegate.get(B3TextMapCodec.SPAN_ID_NAME)); assertNotNull(entries.delegate.get(B3TextMapCodec.PARENT_SPAN_ID_NAME)); assertNotNull(entries.delegate.get(B3TextMapCodec.SAMPLED_NAME)); assertEquals("bar", entries.delegate.get("baggage-foo")); }
@Test public void testChangeBaggagePrefix() { B3TextMapCodec b3Codec = new B3TextMapCodec.Builder() .withBaggagePrefix("foo") .build(); DelegatingTextMap entries = new DelegatingTextMap(); long traceIdLow = 1; long spanId = 2; long parentId = 3; JaegerSpanContext spanContext = new JaegerSpanContext(0L, traceIdLow, spanId, parentId, (byte)0) .withBaggageItem("foo", "bar"); b3Codec.inject(spanContext, entries); assertEquals(5, entries.delegate.size()); assertEquals("bar", entries.delegate.get("foofoo")); }
@Test public void testInject() { DelegatingTextMap textMap = new DelegatingTextMap(); long traceIdLow = 1; long spanId = 2; long parentId = 3; b3Codec.inject(new JaegerSpanContext(0L, traceIdLow, spanId, parentId, SAMPLED), textMap); assertTrue(textMap.containsKey(B3TextMapCodec.TRACE_ID_NAME)); assertTrue(textMap.containsKey(B3TextMapCodec.SPAN_ID_NAME)); }
@Test public void testInject128BitTraceId() { DelegatingTextMap textMap = new DelegatingTextMap(); long traceIdLow = 1; long traceIdHigh = 2; long spanId = 3; long parentId = 4; b3Codec.inject(new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, SAMPLED), textMap); final String traceId = textMap.get(B3TextMapCodec.TRACE_ID_NAME); assertEquals(traceIdLow, HexCodec.lowerHexToUnsignedLong(traceId).longValue()); assertEquals(traceIdHigh, HexCodec.higherHexToUnsignedLong(traceId).longValue()); }