Fixed a classcast for gateway observation; fixes gh-2877

This commit is contained in:
Marcin Grzejszczak
2023-03-01 16:41:23 +01:00
parent 07da47aca0
commit 429991d2ad
2 changed files with 51 additions and 7 deletions

View File

@@ -44,13 +44,15 @@ public class ObservedResponseHttpHeadersFilter implements HttpHeadersFilter {
if (childObservation == null) {
return input;
}
GatewayContext context = (GatewayContext) childObservation.getContext();
if (log.isDebugEnabled()) {
log.debug("Will instrument the response");
}
context.setResponse(exchange.getResponse());
if (log.isDebugEnabled()) {
log.debug("The response was handled for observation " + childObservation);
Observation.Context childObservationContext = childObservation.getContext();
if (childObservationContext instanceof GatewayContext context) {
if (log.isDebugEnabled()) {
log.debug("Will instrument the response");
}
context.setResponse(exchange.getResponse());
if (log.isDebugEnabled()) {
log.debug("The response was handled for observation " + childObservation);
}
}
childObservation.stop();
exchange.getAttributes().put(OBSERVATION_STOPPED, "true");

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2013-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.filter.headers.observation;
import io.micrometer.observation.Observation;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.BDDAssertions.thenNoException;
class ObservedResponseHttpHeadersFilterTests {
@Test
void shouldDoNothingWhenObservationIsNoOp() {
MockServerHttpRequest request = MockServerHttpRequest.get("/get").build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_OBSERVATION_ATTR, Observation.NOOP);
ObservedResponseHttpHeadersFilter filter = new ObservedResponseHttpHeadersFilter();
thenNoException().isThrownBy(() -> filter.filter(new HttpHeaders(), exchange));
}
}