@Override public Message postProcessMessage(Message message) {
    MessageProducerRequest request = new MessageProducerRequest(message);

    TraceContext maybeParent = currentTraceContext.get();
    // Unlike message consumers, we try current span before trying extraction. This is the proper
    // order because the span in scope should take precedence over a potentially stale header entry.
    //
    // NOTE: Brave instrumentation used properly does not result in stale header entries, as we
    // always clear message headers after reading.
    Span span;
    if (maybeParent == null) {
      TraceContextOrSamplingFlags extracted =
        springRabbitTracing.extractAndClearTraceIdHeaders(extractor, request, message);
      span = springRabbitTracing.nextMessagingSpan(sampler, request, extracted);
    } else { // If we have a span in scope assume headers were cleared before
      span = tracer.newChild(maybeParent);
    }

    if (!span.isNoop()) {
      span.kind(PRODUCER).name("publish");
      if (remoteServiceName != null) span.remoteServiceName(remoteServiceName);
      // incur timestamp overhead only once
      long timestamp = tracing.clock(span.context()).currentTimeMicroseconds();
      span.start(timestamp).finish(timestamp);
    }

    injector.inject(span.context(), request);
    return message;
  }
@Test void should_prefer_current_span() {
    // Will be either a bug, or a missing processor stage which can result in an old span in headers
    Message message = MessageBuilder.withBody(new byte[0]).build();
    message.getMessageProperties().setHeader("b3", B3SingleFormat.writeB3SingleFormat(grandparent));

    Message postProcessMessage;
    try (Scope scope = tracing.currentTraceContext().newScope(parent)) {
      postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);
    }

    assertThat(spans.get(0).parentId()).isEqualTo(parent.spanIdString());
    Map<String, Object> headers = postProcessMessage.getMessageProperties().getHeaders();
    assertThat(headers.get("b3").toString()).endsWith("-" + spans.get(0).id() + "-1");
  }