Skip to content

Fix regression introduced in #1126 (brave headers propagation) #1206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,17 @@ public static ExchangeFilterFunction create(BeanFactory beanFactory) {

@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
return new MonoWebClientTrace(next, request, this);
ClientRequest.Builder builder = ClientRequest.from(request);
if (log.isDebugEnabled()) {
log.debug("Instrumenting WebClient call");
}
Span span = handler().handleSend(injector(), builder, request,
tracer().nextSpan());
if (log.isDebugEnabled()) {
log.debug("Handled send of " + span);
}

return new MonoWebClientTrace(next, builder.build(), this, span);
}

private static final class MonoWebClientTrace extends Mono<ClientResponse> {
Expand All @@ -165,37 +175,27 @@ private static final class MonoWebClientTrace extends Mono<ClientResponse> {

final Function<? super Publisher<DataBuffer>, ? extends Publisher<DataBuffer>> scopePassingTransformer;

private final Span span;

MonoWebClientTrace(ExchangeFunction next, ClientRequest request,
TraceExchangeFilterFunction parent) {
TraceExchangeFilterFunction parent, Span span) {
this.next = next;
this.request = request;
this.tracer = parent.tracer();
this.handler = parent.handler();
this.injector = parent.injector();
this.tracing = parent.httpTracing().tracing();
this.scopePassingTransformer = parent.scopePassingTransformer;
this.span = span;
}

@Override
public void subscribe(CoreSubscriber<? super ClientResponse> subscriber) {
final ClientRequest.Builder builder = ClientRequest.from(this.request);

Context context = subscriber.currentContext();

this.next.exchange(builder.build()).subscribe(new WebClientTracerSubscriber(
subscriber, context, findOrCreateSpan(builder), this));
}

private Span findOrCreateSpan(ClientRequest.Builder builder) {
if (log.isDebugEnabled()) {
log.debug("Instrumenting WebClient call");
}
Span clientSpan = this.handler.handleSend(this.injector, builder,
this.request, this.tracer.nextSpan());
if (log.isDebugEnabled()) {
log.debug("Handled send of " + clientSpan);
}
return clientSpan;
this.next.exchange(request).subscribe(
new WebClientTracerSubscriber(subscriber, context, span, this));
}

static final class WebClientTracerSubscriber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -491,6 +492,28 @@ public void should_wrap_rest_template_builders() {
then(this.reporter.getSpans()).extracting("kind.name").contains("CLIENT");
}

@Test
public void should_add_headers_eagerly() {
Span span = this.tracer.nextSpan().name("foo").start();

AtomicReference<String> traceId = new AtomicReference<>();
try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) {
this.webClientBuilder
.filter((request, exchange) -> {
traceId.set(request.headers().getFirst("X-B3-SpanId"));

return exchange.exchange(request);
})
.build()
.get().uri("http://localhost:" + this.port + "/traceid")
.retrieve().bodyToMono(String.class).block();
}
finally {
span.finish();
}
then(traceId).doesNotHaveValue(null);
}

private String getHeader(ResponseEntity<String> response, String name) {
List<String> headers = response.getHeaders().get(name);
return headers == null || headers.isEmpty() ? null : headers.get(0);
Expand Down