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:
parent
e749cd1ef1
commit
fcbd5ec80a
|
|
@ -100,7 +100,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||||
if (context.isConnectionAborted()) {
|
if (context.isConnectionAborted()) {
|
||||||
return STATUS_UNKNOWN;
|
return STATUS_UNKNOWN;
|
||||||
}
|
}
|
||||||
return (context.getResponse() != null) ? KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatusCode().value())) : STATUS_UNKNOWN;
|
return (context.getResponse() != null && context.getResponse().getStatusCode() != null) ?
|
||||||
|
KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.STATUS, Integer.toString(context.getResponse().getStatusCode().value())) : STATUS_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected KeyValue uri(ServerRequestObservationContext context) {
|
protected KeyValue uri(ServerRequestObservationContext context) {
|
||||||
|
|
@ -112,7 +113,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||||
}
|
}
|
||||||
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, pattern.toString());
|
return KeyValue.of(ServerHttpObservationDocumentation.LowCardinalityKeyNames.URI, pattern.toString());
|
||||||
}
|
}
|
||||||
if (context.getResponse() != null) {
|
if (context.getResponse() != null && context.getResponse().getStatusCode() != null) {
|
||||||
HttpStatus status = HttpStatus.resolve(context.getResponse().getStatusCode().value());
|
HttpStatus status = HttpStatus.resolve(context.getResponse().getStatusCode().value());
|
||||||
if (status != null) {
|
if (status != null) {
|
||||||
if (status.is3xxRedirection()) {
|
if (status.is3xxRedirection()) {
|
||||||
|
|
@ -141,7 +142,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||||
if (context.isConnectionAborted()) {
|
if (context.isConnectionAborted()) {
|
||||||
return HTTP_OUTCOME_UNKNOWN;
|
return HTTP_OUTCOME_UNKNOWN;
|
||||||
}
|
}
|
||||||
if (context.getResponse() != null) {
|
if (context.getResponse() != null && context.getResponse().getStatusCode() != null) {
|
||||||
return HttpOutcome.forStatus(context.getResponse().getStatusCode());
|
return HttpOutcome.forStatus(context.getResponse().getStatusCode());
|
||||||
}
|
}
|
||||||
return HTTP_OUTCOME_UNKNOWN;
|
return HTTP_OUTCOME_UNKNOWN;
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,16 @@ class DefaultServerRequestObservationConventionTests {
|
||||||
.contains(KeyValue.of("http.url", "/test/resource"));
|
.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) {
|
private static PathPattern getPathPattern(String pattern) {
|
||||||
PathPatternParser pathPatternParser = new PathPatternParser();
|
PathPatternParser pathPatternParser = new PathPatternParser();
|
||||||
return pathPatternParser.parse(pattern);
|
return pathPatternParser.parse(pattern);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue