Avoid Servlet observations failures for invalid HTTP status
Prior to this commit, the `DefaultServerRequestObservationConvention` for Servlet failed when the HTTP response status was invalid (for example, set to "0"). This commit catches `IllegalArgumentException` thrown for such invalid HTTP status and instead returns an unknown outcome for the observation. Fixes gh-33725
This commit is contained in:
parent
73fd9133e9
commit
67d78eb61c
|
@ -160,9 +160,14 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
|||
}
|
||||
|
||||
protected KeyValue outcome(ServerRequestObservationContext context) {
|
||||
if (context.getResponse() != null) {
|
||||
HttpStatusCode statusCode = HttpStatusCode.valueOf(context.getResponse().getStatus());
|
||||
return HttpOutcome.forStatus(statusCode);
|
||||
try {
|
||||
if (context.getResponse() != null) {
|
||||
HttpStatusCode statusCode = HttpStatusCode.valueOf(context.getResponse().getStatus());
|
||||
return HttpOutcome.forStatus(statusCode);
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
return HTTP_OUTCOME_UNKNOWN;
|
||||
}
|
||||
return HTTP_OUTCOME_UNKNOWN;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -137,4 +137,16 @@ class DefaultServerRequestObservationConventionTests {
|
|||
.contains(KeyValue.of("http.url", "/test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addsKeyValuesForInvalidStatusExchange() {
|
||||
this.request.setRequestURI("/test/invalidStatus");
|
||||
this.response.setStatus(0);
|
||||
|
||||
assertThat(this.convention.getLowCardinalityKeyValues(this.context)).hasSize(5)
|
||||
.contains(KeyValue.of("method", "GET"), KeyValue.of("uri", "UNKNOWN"), KeyValue.of("status", "0"),
|
||||
KeyValue.of("exception", "none"), KeyValue.of("outcome", "UNKNOWN"));
|
||||
assertThat(this.convention.getHighCardinalityKeyValues(this.context)).hasSize(1)
|
||||
.contains(KeyValue.of("http.url", "/test/invalidStatus"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue