Modify WebClient tracing to include the uri template in generated span names (#1940)

Spans names are in the form '<http method> <uri template>'
(e.g. 'GET /path/{id}'). This naming strategy matches the span names
generated by TraceWebFilter.

The improve naming strategy is not applied to RestTemplate as the uri
template is not readily available to interceptors and RestTemplate is
in maintenance mode
This commit is contained in:
Jason Hinch
2021-05-08 06:21:54 +10:00
committed by GitHub
parent 59c170d147
commit ac729f141d
3 changed files with 60 additions and 5 deletions

View File

@@ -68,6 +68,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -252,6 +253,25 @@ public abstract class WebClientTests {
.collect(Collectors.toList())).isNotEmpty().contains("CLIENT");
}
@Test
@SuppressWarnings("unchecked")
public void shouldUseUriTemplateInSpanName() {
Span span = this.tracer.nextSpan().name("foo").start();
try (Tracer.SpanInScope ws = this.tracer.withSpan(span)) {
this.webClientBuilder.baseUrl("http://localhost:" + this.port).build().get()
.uri("/prefix/{variable}/suffix", "value").retrieve().bodyToMono(String.class)
.block(Duration.ofSeconds(5));
}
finally {
span.end();
}
thenThereIsNoCurrentSpan();
then(this.spans.reportedSpans().stream().filter(r -> r.getKind() == Span.Kind.CLIENT).map(r -> r.getName())
.collect(Collectors.toList())).isNotEmpty().contains("GET /prefix/{variable}/suffix");
}
/**
* Cancel before {@link Subscription#request(long)} means a network request was never
* sent
@@ -549,6 +569,11 @@ public abstract class WebClientTests {
return "ok";
}
@RequestMapping(value = "/prefix/{variable}/suffix", method = RequestMethod.GET)
String pathVariable(@PathVariable("variable") String variable) {
return "variable = " + variable;
}
}
@Configuration(proxyBeanMethods = false)