Divide httpTags and routeTags provider
This commit is contained in:
@@ -29,7 +29,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
|
||||
import org.springframework.cloud.gateway.filter.GatewayMetricsFilter;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.DefaultGatewayTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayHttpTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayRouteTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayTagsProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -45,8 +46,13 @@ import org.springframework.web.reactive.DispatcherHandler;
|
||||
public class GatewayMetricsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public GatewayTagsProvider defaultGatewayTagsProvider() {
|
||||
return new DefaultGatewayTagsProvider();
|
||||
public GatewayTagsProvider gatewayHttpTagsProvider() {
|
||||
return new GatewayHttpTagsProvider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GatewayTagsProvider gatewayRouteTagsProvider() {
|
||||
return new GatewayRouteTagsProvider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.gateway.filter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
@@ -27,10 +27,9 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayHttpTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayRouteTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayTagsProvider;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -39,8 +38,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
* @author Tony Clarke
|
||||
* @author Ingyu Hwang
|
||||
*/
|
||||
public class GatewayMetricsFilter
|
||||
implements GlobalFilter, Ordered, ApplicationContextAware {
|
||||
public class GatewayMetricsFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GatewayMetricsFilter.class);
|
||||
|
||||
@@ -48,19 +46,16 @@ public class GatewayMetricsFilter
|
||||
|
||||
private GatewayTagsProvider compositeTagsProvider;
|
||||
|
||||
private AtomicBoolean initialized = new AtomicBoolean(false);
|
||||
|
||||
public GatewayMetricsFilter(MeterRegistry meterRegistry,
|
||||
List<GatewayTagsProvider> tagsProviders) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
this.compositeTagsProvider = tagsProviders.stream()
|
||||
.reduce(exchange -> Tags.empty(), GatewayTagsProvider::and);
|
||||
initialized.compareAndSet(false, true);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public GatewayMetricsFilter(MeterRegistry meterRegistry) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
this(meterRegistry, Arrays.asList(new GatewayHttpTagsProvider(), new GatewayRouteTagsProvider()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,14 +65,6 @@ public class GatewayMetricsFilter
|
||||
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
if (initialized.compareAndSet(false, true)) {
|
||||
this.compositeTagsProvider = context.getBean("defaultGatewayTagsProvider",
|
||||
GatewayTagsProvider.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
Sample sample = Timer.start(meterRegistry);
|
||||
|
||||
@@ -18,23 +18,17 @@ package org.springframework.cloud.gateway.support.tagsprovider;
|
||||
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
/**
|
||||
* @author Ingyu Hwang
|
||||
*/
|
||||
public final class GatewayTags {
|
||||
public class GatewayHttpTagsProvider implements GatewayTagsProvider {
|
||||
|
||||
private GatewayTags() {
|
||||
throw new AssertionError("Must not instantiate utility class.");
|
||||
}
|
||||
|
||||
public static Tags http(ServerWebExchange exchange) {
|
||||
@Override
|
||||
public Tags apply(ServerWebExchange exchange) {
|
||||
String outcome = "CUSTOM";
|
||||
String status = "CUSTOM";
|
||||
String httpStatusCodeStr = "NA";
|
||||
@@ -71,15 +65,4 @@ public final class GatewayTags {
|
||||
"httpStatusCode", httpStatusCodeStr, "httpMethod", httpMethod);
|
||||
}
|
||||
|
||||
public static Tags route(ServerWebExchange exchange) {
|
||||
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
|
||||
|
||||
if (route != null) {
|
||||
return Tags.of("routeId", route.getId(),
|
||||
"routeUri", route.getUri().toString());
|
||||
}
|
||||
|
||||
return Tags.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,16 +18,26 @@ package org.springframework.cloud.gateway.support.tagsprovider;
|
||||
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
/**
|
||||
* @author Ingyu Hwang
|
||||
*/
|
||||
public class DefaultGatewayTagsProvider implements GatewayTagsProvider {
|
||||
public class GatewayRouteTagsProvider implements GatewayTagsProvider {
|
||||
|
||||
@Override
|
||||
public Tags apply(ServerWebExchange exchange) {
|
||||
return Tags.concat(GatewayTags.http(exchange), GatewayTags.route(exchange));
|
||||
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
|
||||
|
||||
if (route != null) {
|
||||
return Tags.of("routeId", route.getId(), "routeUri",
|
||||
route.getUri().toString());
|
||||
}
|
||||
|
||||
return Tags.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,10 +28,10 @@ import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.filter.GatewayMetricsFilter;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.DefaultGatewayTagsProvider;
|
||||
import org.springframework.cloud.gateway.support.tagsprovider.GatewayTagsProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -54,8 +54,7 @@ public class GatewayMetricsAutoConfigurationTests {
|
||||
@Test
|
||||
public void gatewayMetricsBeansExists() {
|
||||
assertThat(filter).isNotNull();
|
||||
assertThat(tagsProviders).extracting("class")
|
||||
.containsOnlyOnce(DefaultGatewayTagsProvider.class);
|
||||
assertThat(tagsProviders).isNotEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,7 +87,7 @@ public class GatewayMetricsAutoConfigurationTests {
|
||||
public void gatewayMetricsBeansExists() {
|
||||
assertThat(filter).isNotNull();
|
||||
assertThat(tagsProviders).extracting("class")
|
||||
.hasSize(2);
|
||||
.contains(CustomTagsProviderConfig.EmptyTagsProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -105,7 +104,16 @@ public class GatewayMetricsAutoConfigurationTests {
|
||||
|
||||
@Bean
|
||||
public GatewayTagsProvider emptyTagsProvider() {
|
||||
return (exchange) -> Tags.empty();
|
||||
return new EmptyTagsProvider();
|
||||
}
|
||||
|
||||
protected static class EmptyTagsProvider implements GatewayTagsProvider {
|
||||
|
||||
@Override
|
||||
public Tags apply(ServerWebExchange exchange) {
|
||||
return Tags.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,8 @@
|
||||
package org.springframework.cloud.gateway.support.tagsprovider;
|
||||
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
|
||||
@@ -31,43 +28,28 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
/**
|
||||
* @author Ingyu Hwang
|
||||
*/
|
||||
public class DefaultGatewayTagsProviderTests {
|
||||
public class GatewayHttpTagsProviderTests {
|
||||
|
||||
private Route route;
|
||||
|
||||
private DefaultGatewayTagsProvider defaultTagsProvider;
|
||||
|
||||
private Tags defaultTags;
|
||||
private final GatewayHttpTagsProvider tagsProvider = new GatewayHttpTagsProvider();
|
||||
|
||||
private static final String ROUTE_URI = "http://gatewaytagsprovider.org:80";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
String routeId = "test-route";
|
||||
HttpStatus ok = HttpStatus.OK;
|
||||
|
||||
route = Route.async().id(routeId).uri(ROUTE_URI).predicate(swe -> true).build();
|
||||
|
||||
defaultTagsProvider = new DefaultGatewayTagsProvider();
|
||||
defaultTags = Tags.of("outcome", ok.series().name(), "status", ok.name(),
|
||||
"httpStatusCode", String.valueOf(ok.value()), "httpMethod", "GET",
|
||||
"routeId", routeId, "routeUri", ROUTE_URI);
|
||||
}
|
||||
private static final Tags DEFAULT_TAGS = Tags.of("outcome", OK.series().name(),
|
||||
"status", OK.name(), "httpStatusCode", String.valueOf(OK.value()), "httpMethod", "GET");
|
||||
|
||||
@Test
|
||||
public void defaultProvider() {
|
||||
public void httpTags() {
|
||||
ServerWebExchange exchange = MockServerWebExchange
|
||||
.from(MockServerHttpRequest.get(ROUTE_URI).build());
|
||||
exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, route);
|
||||
exchange.getResponse().setStatusCode(HttpStatus.OK);
|
||||
exchange.getResponse().setStatusCode(OK);
|
||||
|
||||
Tags tags = defaultTagsProvider.apply(exchange);
|
||||
assertThat(tags).isEqualTo(defaultTags);
|
||||
Tags tags = tagsProvider.apply(exchange);
|
||||
assertThat(tags).isEqualTo(DEFAULT_TAGS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,7 +57,7 @@ public class DefaultGatewayTagsProviderTests {
|
||||
ServerWebExchange exchange = MockServerWebExchange
|
||||
.from(MockServerHttpRequest.get(ROUTE_URI).build());
|
||||
|
||||
Tags tags = defaultTagsProvider.apply(exchange);
|
||||
Tags tags = tagsProvider.apply(exchange);
|
||||
assertThat(tags).isEqualTo(Tags.of("outcome", "CUSTOM", "status", "CUSTOM",
|
||||
"httpStatusCode", "NA", "httpMethod", "GET"));
|
||||
}
|
||||
@@ -85,15 +67,14 @@ public class DefaultGatewayTagsProviderTests {
|
||||
ServerWebExchange mockExchange = mock(ServerWebExchange.class);
|
||||
ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(
|
||||
new MockServerHttpResponse());
|
||||
responseDecorator.setStatusCode(HttpStatus.OK);
|
||||
responseDecorator.setStatusCode(OK);
|
||||
|
||||
when(mockExchange.getRequest())
|
||||
.thenReturn(MockServerHttpRequest.get(ROUTE_URI).build());
|
||||
when(mockExchange.getResponse()).thenReturn(responseDecorator);
|
||||
when(mockExchange.getAttribute(GATEWAY_ROUTE_ATTR)).thenReturn(route);
|
||||
|
||||
Tags tags = defaultTagsProvider.apply(mockExchange);
|
||||
assertThat(tags).isEqualTo(defaultTags);
|
||||
Tags tags = tagsProvider.apply(mockExchange);
|
||||
assertThat(tags).isEqualTo(DEFAULT_TAGS);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.support.tagsprovider;
|
||||
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
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.Assertions.assertThat;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
/**
|
||||
* @author Ingyu Hwang
|
||||
*/
|
||||
public class GatewayRouteTagsProviderTests {
|
||||
|
||||
private final GatewayRouteTagsProvider tagsProvider = new GatewayRouteTagsProvider();
|
||||
|
||||
private static final String ROUTE_URI = "http://gatewaytagsprovider.org:80";
|
||||
|
||||
private static final String ROUTE_ID = "test-route";
|
||||
|
||||
private static final Route ROUTE = Route.async().id(ROUTE_ID).uri(ROUTE_URI)
|
||||
.predicate(swe -> true).build();
|
||||
|
||||
private static final Tags DEFAULT_TAGS = Tags.of("routeId", ROUTE_ID, "routeUri",
|
||||
ROUTE_URI);
|
||||
|
||||
@Test
|
||||
public void routeTags() {
|
||||
ServerWebExchange exchange = MockServerWebExchange
|
||||
.from(MockServerHttpRequest.get(ROUTE_URI).build());
|
||||
exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, ROUTE);
|
||||
|
||||
Tags tags = tagsProvider.apply(exchange);
|
||||
assertThat(tags).isEqualTo(DEFAULT_TAGS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyRoute() {
|
||||
ServerWebExchange exchange = MockServerWebExchange
|
||||
.from(MockServerHttpRequest.get(ROUTE_URI).build());
|
||||
|
||||
Tags tags = tagsProvider.apply(exchange);
|
||||
assertThat(tags).isEqualTo(Tags.empty());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user