Skip to content

Commit 5473260

Browse files
committed
Backport nullability refinements for Micrometer
See gh-35170
1 parent 12a6098 commit 5473260

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.http.HttpStatusCode;
3030
import org.springframework.http.server.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
3131
import org.springframework.http.server.observation.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
32+
import org.springframework.lang.Nullable;
3233
import org.springframework.util.StringUtils;
3334

3435
/**
@@ -89,12 +90,16 @@ public String getName() {
8990
}
9091

9192
@Override
93+
@Nullable
9294
public String getContextualName(ServerRequestObservationContext context) {
93-
String httpMethod = context.getCarrier().getMethod().toLowerCase(Locale.ROOT);
94-
if (context.getPathPattern() != null) {
95-
return "http " + httpMethod + " " + context.getPathPattern();
95+
if (context.getCarrier() != null) {
96+
String httpMethod = context.getCarrier().getMethod().toLowerCase(Locale.ROOT);
97+
if (context.getPathPattern() != null) {
98+
return "http " + httpMethod + " " + context.getPathPattern();
99+
}
100+
return "http " + httpMethod;
96101
}
97-
return "http " + httpMethod;
102+
return null;
98103
}
99104

100105
@Override
@@ -193,7 +198,6 @@ else if (statusCode instanceof HttpStatus status) {
193198
return HTTP_OUTCOME_UNKNOWN;
194199
}
195200
}
196-
197201
}
198202

199203
}

spring-web/src/main/java/org/springframework/http/server/reactive/observation/DefaultServerRequestObservationConvention.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.http.HttpStatusCode;
2828
import org.springframework.http.server.reactive.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
2929
import org.springframework.http.server.reactive.observation.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
30+
import org.springframework.lang.Nullable;
3031
import org.springframework.util.StringUtils;
3132

3233
/**
@@ -87,12 +88,16 @@ public String getName() {
8788
}
8889

8990
@Override
91+
@Nullable
9092
public String getContextualName(ServerRequestObservationContext context) {
91-
String httpMethod = context.getCarrier().getMethod().name().toLowerCase(Locale.ROOT);
92-
if (context.getPathPattern() != null) {
93-
return "http " + httpMethod + " " + context.getPathPattern();
93+
if (context.getCarrier() != null) {
94+
String httpMethod = context.getCarrier().getMethod().name().toLowerCase(Locale.ROOT);
95+
if (context.getPathPattern() != null) {
96+
return "http " + httpMethod + " " + context.getPathPattern();
97+
}
98+
return "http " + httpMethod;
9499
}
95-
return "http " + httpMethod;
100+
return null;
96101
}
97102

98103
@Override
@@ -191,7 +196,6 @@ else if (statusCode instanceof HttpStatus status) {
191196
return HTTP_OUTCOME_UNKNOWN;
192197
}
193198
}
194-
195199
}
196200

197201
}

spring-web/src/main/java/org/springframework/web/filter/ServerHttpObservationFilter.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,23 @@
5555
public class ServerHttpObservationFilter extends OncePerRequestFilter {
5656

5757
/**
58-
* Name of the request attribute holding the {@link ServerRequestObservationContext context} for the current observation.
58+
* Name of the request attribute holding the {@link ServerRequestObservationContext} for the current observation.
5959
*/
60-
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".context";
60+
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE =
61+
ServerHttpObservationFilter.class.getName() + ".context";
6162

62-
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultServerRequestObservationConvention();
63+
private static final String CURRENT_OBSERVATION_ATTRIBUTE =
64+
ServerHttpObservationFilter.class.getName() + ".observation";
6365

64-
private static final String CURRENT_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".observation";
66+
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION =
67+
new DefaultServerRequestObservationConvention();
6568

6669

6770
private final ObservationRegistry observationRegistry;
6871

6972
private final ServerRequestObservationConvention observationConvention;
7073

74+
7175
/**
7276
* Create an {@code HttpRequestsObservationFilter} that records observations
7377
* against the given {@link ObservationRegistry}. The default
@@ -89,14 +93,6 @@ public ServerHttpObservationFilter(ObservationRegistry observationRegistry, Serv
8993
this.observationConvention = observationConvention;
9094
}
9195

92-
/**
93-
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
94-
* @param request the current request
95-
* @return the current observation context
96-
*/
97-
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) {
98-
return Optional.ofNullable((ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
99-
}
10096

10197
@Override
10298
protected boolean shouldNotFilterAsyncDispatch() {
@@ -150,8 +146,9 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
150146
Observation observation = (Observation) request.getAttribute(CURRENT_OBSERVATION_ATTRIBUTE);
151147
if (observation == null) {
152148
ServerRequestObservationContext context = new ServerRequestObservationContext(request, response);
153-
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation(this.observationConvention,
154-
DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry).start();
149+
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation(
150+
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry)
151+
.start();
155152
request.setAttribute(CURRENT_OBSERVATION_ATTRIBUTE, observation);
156153
if (!observation.isNoop()) {
157154
request.setAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation.getContext());
@@ -160,16 +157,33 @@ private Observation createOrFetchObservation(HttpServletRequest request, HttpSer
160157
return observation;
161158
}
162159

163-
@Nullable
164-
static Throwable unwrapServletException(Throwable ex) {
165-
return (ex instanceof ServletException) ? ex.getCause() : ex;
160+
161+
/**
162+
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
163+
* @param request the current request
164+
* @return the current observation context
165+
*/
166+
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) {
167+
return Optional.ofNullable(
168+
(ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
166169
}
167170

168171
@Nullable
169172
static Throwable fetchException(ServletRequest request) {
170173
return (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
171174
}
172175

176+
static Throwable unwrapServletException(Throwable ex) {
177+
if (ex instanceof ServletException) {
178+
Throwable cause = ex.getCause();
179+
if (cause != null) {
180+
return cause;
181+
}
182+
}
183+
return ex;
184+
}
185+
186+
173187
private static class ObservationAsyncListener implements AsyncListener {
174188

175189
private final Observation currentObservation;
@@ -197,7 +211,6 @@ public void onComplete(AsyncEvent event) {
197211
public void onError(AsyncEvent event) {
198212
this.currentObservation.error(unwrapServletException(event.getThrowable()));
199213
}
200-
201214
}
202215

203216
}

0 commit comments

Comments
 (0)