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

@@ -100,7 +100,8 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
if (context.isConnectionAborted()) {
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) {
@@ -112,7 +113,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
}
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());
if (status != null) {
if (status.is3xxRedirection()) {
@@ -141,7 +142,7 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
if (context.isConnectionAborted()) {
return HTTP_OUTCOME_UNKNOWN;
}
if (context.getResponse() != null) {
if (context.getResponse() != null && context.getResponse().getStatusCode() != null) {
return HttpOutcome.forStatus(context.getResponse().getStatusCode());
}
return HTTP_OUTCOME_UNKNOWN;