Avoid NPEs in DefaultServerRequestObservationConvention

In some cases, the default response status of a `ServerWebExchange` can
be `null`, especially when the response is not available or the server
implementation does not set a default response status.
This commit ensures that the status code is available when deriving
`KeyValue` information from it, or uses a fallback value for the key
value.

Fixes gh-29359
This commit is contained in:
Brian Clozel
2022-10-20 14:29:34 +02:00
parent e749cd1ef1
commit fcbd5ec80a
2 changed files with 14 additions and 3 deletions

View File

@@ -141,6 +141,16 @@ class DefaultServerRequestObservationConventionTests {
.contains(KeyValue.of("http.url", "/test/resource"));
}
@Test
void supportsNullStatusCode() {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test/resource"));
ServerRequestObservationContext context = new ServerRequestObservationContext(exchange);
assertThat(this.convention.getLowCardinalityKeyValues(context))
.contains(KeyValue.of("status", "UNKNOWN"),
KeyValue.of("exception", "none"), KeyValue.of("outcome", "UNKNOWN"));
}
private static PathPattern getPathPattern(String pattern) {
PathPatternParser pathPatternParser = new PathPatternParser();
return pathPatternParser.parse(pattern);