Remove ServerWebExchange dependency in ServerRequestObservationContext

Avoiding cycle between http.server and web.server packages.

See gh-30013
This commit is contained in:
Juergen Hoeller
2023-06-14 21:57:27 +02:00
parent 220995b830
commit f00a8cb3a3
7 changed files with 40 additions and 22 deletions

View File

@@ -25,11 +25,12 @@ import io.micrometer.observation.transport.RequestReplyReceiverContext;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.web.server.ServerWebExchange;
/**
* Context that holds information for metadata collection regarding
* {@link ServerHttpObservationDocumentation#HTTP_REACTIVE_SERVER_REQUESTS reactive HTTP requests} observations.
* {@link ServerHttpObservationDocumentation#HTTP_REACTIVE_SERVER_REQUESTS reactive HTTP requests}
* observations.
*
* <p>This context also extends {@link RequestReplyReceiverContext} for propagating
* tracing information during HTTP request processing.
*
@@ -39,10 +40,11 @@ import org.springframework.web.server.ServerWebExchange;
public class ServerRequestObservationContext extends RequestReplyReceiverContext<ServerHttpRequest, ServerHttpResponse> {
/**
* Name of the request attribute holding the {@link ServerRequestObservationContext context} for the current observation.
* Name of the request attribute holding the {@link ServerRequestObservationContext context}
* for the current observation.
* @since 6.1
*/
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerRequestObservationContext.class.getName() + ".context";
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerRequestObservationContext.class.getName();
private final Map<String, Object> attributes;
@@ -53,7 +55,15 @@ public class ServerRequestObservationContext extends RequestReplyReceiverContext
private boolean connectionAborted;
public ServerRequestObservationContext(ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> attributes) {
/**
* Create a new {@code ServerRequestObservationContext} instance.
* @param request the current request
* @param response the current response
* @param attributes the current attributes
*/
public ServerRequestObservationContext(
ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> attributes) {
super((req, key) -> req.getHeaders().getFirst(key));
setCarrier(request);
setResponse(response);
@@ -89,8 +99,8 @@ public class ServerRequestObservationContext extends RequestReplyReceiverContext
}
/**
* Whether the current connection was aborted by the client, resulting
* in a {@link reactor.core.publisher.SignalType#CANCEL cancel signal} on the reactive chain,
* Whether the current connection was aborted by the client, resulting in a
* {@link reactor.core.publisher.SignalType#CANCEL cancel signal} on the reactive chain,
* or an {@code AbortedException} when reading the request.
* @return if the connection has been aborted
*/
@@ -99,8 +109,8 @@ public class ServerRequestObservationContext extends RequestReplyReceiverContext
}
/**
* Set whether the current connection was aborted by the client, resulting
* in a {@link reactor.core.publisher.SignalType#CANCEL cancel signal} on the reactive chain,
* Set whether the current connection was aborted by the client, resulting in a
* {@link reactor.core.publisher.SignalType#CANCEL cancel signal} on the reactive chain,
* or an {@code AbortedException} when reading the request.
* @param connectionAborted if the connection has been aborted
*/
@@ -110,13 +120,15 @@ public class ServerRequestObservationContext extends RequestReplyReceiverContext
/**
* Get the current {@link ServerRequestObservationContext observation context} from the given exchange, if available.
* @param exchange the current exchange
* Get the current {@link ServerRequestObservationContext observation context}
* from the given attributes, if available.
* @param attributes the current exchange attributes
* @return the current observation context
* @since 6.1
*/
public static Optional<ServerRequestObservationContext> findCurrent(ServerWebExchange exchange) {
return Optional.ofNullable(exchange.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
public static Optional<ServerRequestObservationContext> findCurrent(Map<String, Object> attributes) {
return Optional.ofNullable(
(ServerRequestObservationContext) attributes.get(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
}
}

View File

@@ -296,9 +296,10 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
exchange.getLogPrefix() + formatRequest(exchange.getRequest()) +
(traceOn ? ", headers=" + formatHeaders(exchange.getRequest().getHeaders()) : ""));
ServerRequestObservationContext observationContext = new ServerRequestObservationContext(exchange.getRequest(),
exchange.getResponse(), exchange.getAttributes());
exchange.getAttributes().put(ServerRequestObservationContext.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observationContext);
ServerRequestObservationContext observationContext = new ServerRequestObservationContext(
exchange.getRequest(), exchange.getResponse(), exchange.getAttributes());
exchange.getAttributes().put(
ServerRequestObservationContext.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observationContext);
return getDelegate().handle(exchange)
.transformDeferred(call -> transform(exchange, observationContext, call))