Skip to content

Commit 59957d7

Browse files
Pin DefaultGatewayObservationConvention behavior at unit level
Adds focused unit assertions for the gateway client observation convention beyond the metric name renamed in the previous commit: - guard that getName() does not equal Spring Web's http.client.requests - getContextualName span naming - low-cardinality key set (http.method, http.status_code, and both spring.cloud.gateway.route.* keys) Previously only covered indirectly by the integration-style ObservedHttpHeadersFilterTests. See gh-3153 Signed-off-by: Jooyoung Jung <143606756+LivingLikeKrillin@users.noreply.github.com>
1 parent 4ab710c commit 59957d7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

spring-cloud-gateway-server-webflux/src/test/java/org/springframework/cloud/gateway/filter/headers/observation/DefaultGatewayObservationConventionTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@
1616

1717
package org.springframework.cloud.gateway.filter.headers.observation;
1818

19+
import io.micrometer.common.KeyValue;
20+
import io.micrometer.common.KeyValues;
1921
import org.junit.jupiter.api.Test;
2022

23+
import org.springframework.cloud.gateway.route.Route;
24+
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
27+
import org.springframework.mock.web.server.MockServerWebExchange;
28+
2129
import static org.assertj.core.api.Assertions.assertThat;
2230

2331
class DefaultGatewayObservationConventionTests {
@@ -28,4 +36,37 @@ void getName_returnsSpringCloudGatewayPrefixedName() {
2836
.isEqualTo("spring.cloud.gateway.http.client.requests");
2937
}
3038

39+
@Test
40+
void getName_doesNotCollideWithSpringWebClientMetric() {
41+
assertThat(DefaultGatewayObservationConvention.INSTANCE.getName()).isNotEqualTo("http.client.requests");
42+
}
43+
44+
@Test
45+
void getContextualName_returnsHttpMethodPrefixedName() {
46+
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/foo").build();
47+
MockServerWebExchange exchange = MockServerWebExchange.from(request);
48+
GatewayContext context = new GatewayContext(new HttpHeaders(), exchange.getRequest(), exchange);
49+
50+
assertThat(DefaultGatewayObservationConvention.INSTANCE.getContextualName(context)).isEqualTo("HTTP GET");
51+
}
52+
53+
@Test
54+
void getLowCardinalityKeyValues_pinsGatewayRouteKeys() {
55+
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/foo").build();
56+
Route route = Route.async()
57+
.id("test-route")
58+
.uri("http://localhost:8080/")
59+
.order(1)
60+
.predicate(exchange -> true)
61+
.build();
62+
MockServerWebExchange exchange = MockServerWebExchange.from(request);
63+
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, route);
64+
GatewayContext context = new GatewayContext(new HttpHeaders(), exchange.getRequest(), exchange);
65+
66+
KeyValues keyValues = DefaultGatewayObservationConvention.INSTANCE.getLowCardinalityKeyValues(context);
67+
68+
assertThat(keyValues.stream().map(KeyValue::getKey)).containsExactlyInAnyOrder("http.method",
69+
"http.status_code", "spring.cloud.gateway.route.id", "spring.cloud.gateway.route.uri");
70+
}
71+
3172
}

0 commit comments

Comments
 (0)