@Test void testContextInjection() { MockTracer mt = new MockTracer(); MockSpan parent = mt.buildSpan("parentOperation").start(); MockSpan span = mt.buildSpan("anOperation") .asChildOf(parent) .start(); String parentSpanId = "51b3b1a413dce011"; String spanId = "521c61ede905945f"; String traceId = "0000816c055dc421"; ZipkinTracerProvider provider = new ZipkinTracerProvider(); Map<String, List<String>> inboundHeaders = Map.of( ZipkinTracerProvider.X_OT_SPAN_CONTEXT, List.of("0000816c055dc421;0000816c055dc421;0000000000000000;sr"), ZipkinTracerProvider.X_B3_PARENT_SPAN_ID, List.of(parentSpanId), ZipkinTracerProvider.X_B3_SPAN_ID, List.of(spanId), ZipkinTracerProvider.X_B3_TRACE_ID, List.of(traceId) ); Map<String, List<String>> outboundHeaders = Map.of(); outboundHeaders = provider.updateOutboundHeaders(span, mt, parent.context(), outboundHeaders, inboundHeaders); List<String> values = outboundHeaders.get(ZipkinTracerProvider.X_OT_SPAN_CONTEXT); assertThat(values, is(List.of("0000816c055dc421;521c61ede905945f;51b3b1a413dce011;sr"))); values = outboundHeaders.get(ZipkinTracerProvider.X_B3_PARENT_SPAN_ID); assertThat(values, is(List.of(parentSpanId))); values = outboundHeaders.get(ZipkinTracerProvider.X_B3_SPAN_ID); assertThat(values, is(List.of(spanId))); values = outboundHeaders.get(ZipkinTracerProvider.X_B3_TRACE_ID); assertThat(values, is(List.of(traceId))); }
@Override public Map<String, List<String>> updateOutboundHeaders(Span currentSpan, Tracer tracer, SpanContext parentSpan, Map<String, List<String>> outboundHeaders, Map<String, List<String>> inboundHeaders) { Map<String, List<String>> result = new HashMap<>(outboundHeaders); if (inboundHeaders.isEmpty()) { return result; } TRACING_CONTEXT_PROPAGATION_HEADERS.forEach(header -> result.computeIfAbsent(header, inboundHeaders::get)); fixXOtSpanContext(result); return result; }
ZipkinTracerProvider implements TracerProvider { @Override public Map<String, List<String>> updateOutboundHeaders(Span currentSpan, Tracer tracer, SpanContext parentSpan, Map<String, List<String>> outboundHeaders, Map<String, List<String>> inboundHeaders) { Map<String, List<String>> result = new HashMap<>(outboundHeaders); if (inboundHeaders.isEmpty()) { return result; } TRACING_CONTEXT_PROPAGATION_HEADERS.forEach(header -> result.computeIfAbsent(header, inboundHeaders::get)); fixXOtSpanContext(result); return result; } }
ZipkinTracerProvider implements TracerProvider { @Override public Map<String, List<String>> updateOutboundHeaders(Span currentSpan, Tracer tracer, SpanContext parentSpan, Map<String, List<String>> outboundHeaders, Map<String, List<String>> inboundHeaders) { Map<String, List<String>> result = new HashMap<>(outboundHeaders); if (inboundHeaders.isEmpty()) { return result; } TRACING_CONTEXT_PROPAGATION_HEADERS.forEach(header -> result.computeIfAbsent(header, inboundHeaders::get)); fixXOtSpanContext(result); return result; }  }
ZipkinTracerProvider implements TracerProvider { @Override public Map<String, List<String>> updateOutboundHeaders(Span currentSpan, Tracer tracer, SpanContext parentSpan, Map<String, List<String>> outboundHeaders, Map<String, List<String>> inboundHeaders) { Map<String, List<String>> result = new HashMap<>(outboundHeaders); if (inboundHeaders.isEmpty()) { return result; } TRACING_CONTEXT_PROPAGATION_HEADERS.forEach(header -> result.computeIfAbsent(header, inboundHeaders::get)); fixXOtSpanContext(result); return result; }  @Override TracerBuilder<?> createBuilder(); @Override Map<String, List<String>> updateOutboundHeaders(Span currentSpan,
                                                           Tracer tracer,
                                                           SpanContext parentSpan,
                                                           Map<String, List<String>> outboundHeaders,
                                                           Map<String, List<String>> inboundHeaders); }
ZipkinTracerProvider implements TracerProvider { @Override public Map<String, List<String>> updateOutboundHeaders(Span currentSpan, Tracer tracer, SpanContext parentSpan, Map<String, List<String>> outboundHeaders, Map<String, List<String>> inboundHeaders) { Map<String, List<String>> result = new HashMap<>(outboundHeaders); if (inboundHeaders.isEmpty()) { return result; } TRACING_CONTEXT_PROPAGATION_HEADERS.forEach(header -> result.computeIfAbsent(header, inboundHeaders::get)); fixXOtSpanContext(result); return result; }  @Override TracerBuilder<?> createBuilder(); @Override Map<String, List<String>> updateOutboundHeaders(Span currentSpan,
                                                           Tracer tracer,
                                                           SpanContext parentSpan,
                                                           Map<String, List<String>> outboundHeaders,
                                                           Map<String, List<String>> inboundHeaders);  }