mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-26 00:39:01 +00:00
Backport nullability refinements for Micrometer
See gh-35170
This commit is contained in:
+9
-5
@@ -29,6 +29,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.server.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
|
||||
import org.springframework.http.server.observation.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -89,12 +90,16 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getContextualName(ServerRequestObservationContext context) {
|
||||
String httpMethod = context.getCarrier().getMethod().toLowerCase(Locale.ROOT);
|
||||
if (context.getPathPattern() != null) {
|
||||
return "http " + httpMethod + " " + context.getPathPattern();
|
||||
if (context.getCarrier() != null) {
|
||||
String httpMethod = context.getCarrier().getMethod().toLowerCase(Locale.ROOT);
|
||||
if (context.getPathPattern() != null) {
|
||||
return "http " + httpMethod + " " + context.getPathPattern();
|
||||
}
|
||||
return "http " + httpMethod;
|
||||
}
|
||||
return "http " + httpMethod;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -193,7 +198,6 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
return HTTP_OUTCOME_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
-5
@@ -27,6 +27,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.server.reactive.observation.ServerHttpObservationDocumentation.HighCardinalityKeyNames;
|
||||
import org.springframework.http.server.reactive.observation.ServerHttpObservationDocumentation.LowCardinalityKeyNames;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -87,12 +88,16 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getContextualName(ServerRequestObservationContext context) {
|
||||
String httpMethod = context.getCarrier().getMethod().name().toLowerCase(Locale.ROOT);
|
||||
if (context.getPathPattern() != null) {
|
||||
return "http " + httpMethod + " " + context.getPathPattern();
|
||||
if (context.getCarrier() != null) {
|
||||
String httpMethod = context.getCarrier().getMethod().name().toLowerCase(Locale.ROOT);
|
||||
if (context.getPathPattern() != null) {
|
||||
return "http " + httpMethod + " " + context.getPathPattern();
|
||||
}
|
||||
return "http " + httpMethod;
|
||||
}
|
||||
return "http " + httpMethod;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,7 +196,6 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
|
||||
return HTTP_OUTCOME_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+31
-18
@@ -55,19 +55,23 @@ import org.springframework.lang.Nullable;
|
||||
public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
|
||||
/**
|
||||
* Name of the request attribute holding the {@link ServerRequestObservationContext context} for the current observation.
|
||||
* Name of the request attribute holding the {@link ServerRequestObservationContext} for the current observation.
|
||||
*/
|
||||
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".context";
|
||||
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE =
|
||||
ServerHttpObservationFilter.class.getName() + ".context";
|
||||
|
||||
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultServerRequestObservationConvention();
|
||||
private static final String CURRENT_OBSERVATION_ATTRIBUTE =
|
||||
ServerHttpObservationFilter.class.getName() + ".observation";
|
||||
|
||||
private static final String CURRENT_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".observation";
|
||||
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION =
|
||||
new DefaultServerRequestObservationConvention();
|
||||
|
||||
|
||||
private final ObservationRegistry observationRegistry;
|
||||
|
||||
private final ServerRequestObservationConvention observationConvention;
|
||||
|
||||
|
||||
/**
|
||||
* Create an {@code HttpRequestsObservationFilter} that records observations
|
||||
* against the given {@link ObservationRegistry}. The default
|
||||
@@ -89,14 +93,6 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
this.observationConvention = observationConvention;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
|
||||
* @param request the current request
|
||||
* @return the current observation context
|
||||
*/
|
||||
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) {
|
||||
return Optional.ofNullable((ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilterAsyncDispatch() {
|
||||
@@ -150,8 +146,9 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
Observation observation = (Observation) request.getAttribute(CURRENT_OBSERVATION_ATTRIBUTE);
|
||||
if (observation == null) {
|
||||
ServerRequestObservationContext context = new ServerRequestObservationContext(request, response);
|
||||
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation(this.observationConvention,
|
||||
DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry).start();
|
||||
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation(
|
||||
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry)
|
||||
.start();
|
||||
request.setAttribute(CURRENT_OBSERVATION_ATTRIBUTE, observation);
|
||||
if (!observation.isNoop()) {
|
||||
request.setAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation.getContext());
|
||||
@@ -160,9 +157,15 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
return observation;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static Throwable unwrapServletException(Throwable ex) {
|
||||
return (ex instanceof ServletException) ? ex.getCause() : ex;
|
||||
|
||||
/**
|
||||
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available.
|
||||
* @param request the current request
|
||||
* @return the current observation context
|
||||
*/
|
||||
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) {
|
||||
return Optional.ofNullable(
|
||||
(ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -170,6 +173,17 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
return (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
|
||||
}
|
||||
|
||||
static Throwable unwrapServletException(Throwable ex) {
|
||||
if (ex instanceof ServletException) {
|
||||
Throwable cause = ex.getCause();
|
||||
if (cause != null) {
|
||||
return cause;
|
||||
}
|
||||
}
|
||||
return ex;
|
||||
}
|
||||
|
||||
|
||||
private static class ObservationAsyncListener implements AsyncListener {
|
||||
|
||||
private final Observation currentObservation;
|
||||
@@ -197,7 +211,6 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
public void onError(AsyncEvent event) {
|
||||
this.currentObservation.error(unwrapServletException(event.getThrowable()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user