Skip to content

Commit 67d78eb

Browse files
committed
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
1 parent 73fd913 commit 67d78eb

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/server/observation/DefaultServerRequestObservationConvention.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,14 @@ protected KeyValue exception(ServerRequestObservationContext context) {
160160
}
161161

162162
protected KeyValue outcome(ServerRequestObservationContext context) {
163-
if (context.getResponse() != null) {
164-
HttpStatusCode statusCode = HttpStatusCode.valueOf(context.getResponse().getStatus());
165-
return HttpOutcome.forStatus(statusCode);
163+
try {
164+
if (context.getResponse() != null) {
165+
HttpStatusCode statusCode = HttpStatusCode.valueOf(context.getResponse().getStatus());
166+
return HttpOutcome.forStatus(statusCode);
167+
}
168+
}
169+
catch (IllegalArgumentException ex) {
170+
return HTTP_OUTCOME_UNKNOWN;
166171
}
167172
return HTTP_OUTCOME_UNKNOWN;
168173
}

Diff for: spring-web/src/test/java/org/springframework/http/server/observation/DefaultServerRequestObservationConventionTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,4 +137,16 @@ void addsKeyValuesForUnknownHttpMethodExchange() {
137137
.contains(KeyValue.of("http.url", "/test"));
138138
}
139139

140+
@Test
141+
void addsKeyValuesForInvalidStatusExchange() {
142+
this.request.setRequestURI("/test/invalidStatus");
143+
this.response.setStatus(0);
144+
145+
assertThat(this.convention.getLowCardinalityKeyValues(this.context)).hasSize(5)
146+
.contains(KeyValue.of("method", "GET"), KeyValue.of("uri", "UNKNOWN"), KeyValue.of("status", "0"),
147+
KeyValue.of("exception", "none"), KeyValue.of("outcome", "UNKNOWN"));
148+
assertThat(this.convention.getHighCardinalityKeyValues(this.context)).hasSize(1)
149+
.contains(KeyValue.of("http.url", "/test/invalidStatus"));
150+
}
151+
140152
}

0 commit comments

Comments
 (0)