Record URI pattern tag for WebFlux Fn metrics

This commit records URI tags for Spring WebFlux Fn applications for
`http.server.requests` metrics. This is possible since SPR-17098.

Closes gh-12757
This commit is contained in:
Brian Clozel 2018-10-12 13:57:32 +02:00
parent 19232ad87a
commit 5607fcae85
2 changed files with 19 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import io.micrometer.core.instrument.Tag;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPattern;
@ -86,7 +87,9 @@ public final class WebFluxTags {
/** /**
* Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the * Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern. * {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern from
* WebFlux annotation or {@link RouterFunctions#MATCHING_PATTERN_ATTRIBUTE} from
* WebFlux Fn.
* @param exchange the exchange * @param exchange the exchange
* @return the uri tag derived from the exchange * @return the uri tag derived from the exchange
*/ */
@ -96,6 +99,11 @@ public final class WebFluxTags {
if (pathPattern != null) { if (pathPattern != null) {
return Tag.of("uri", pathPattern.getPatternString()); return Tag.of("uri", pathPattern.getPatternString());
} }
String matchingPattern = exchange
.getAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
if (matchingPattern != null) {
return Tag.of("uri", matchingPattern);
}
HttpStatus status = exchange.getResponse().getStatusCode(); HttpStatus status = exchange.getResponse().getStatusCode();
if (status != null) { if (status != null) {
if (status.is3xxRedirection()) { if (status.is3xxRedirection()) {

View File

@ -25,6 +25,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPatternParser; import org.springframework.web.util.pattern.PathPatternParser;
@ -58,6 +59,15 @@ public class WebFluxTagsTests {
assertThat(tag.getValue()).isEqualTo("/spring"); assertThat(tag.getValue()).isEqualTo("/spring");
} }
@Test
public void uriTagValueIsFnMatchingPatternWhenAvailable() {
this.exchange.getAttributes().put(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE,
"/spring");
this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY);
Tag tag = WebFluxTags.uri(this.exchange);
assertThat(tag.getValue()).isEqualTo("/spring");
}
@Test @Test
public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() { public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() {
this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY); this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY);