Add support for api gateway metrics
This commit is contained in:
@@ -21,6 +21,8 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.netflix.hystrix.HystrixObservableCommand;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -48,6 +50,7 @@ import org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter;
|
||||
import org.springframework.cloud.gateway.filter.ForwardPathFilter;
|
||||
import org.springframework.cloud.gateway.filter.ForwardRoutingFilter;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayMetricsFilter;
|
||||
import org.springframework.cloud.gateway.filter.NettyRoutingFilter;
|
||||
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
||||
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
|
||||
@@ -320,6 +323,12 @@ public class GatewayAutoConfiguration {
|
||||
|
||||
|
||||
// GlobalFilter beans
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.cloud.gateway.metrics.enabled", matchIfMissing = true)
|
||||
public GatewayMetricsFilter gatewayMetricFilter(MeterRegistry meterRegistry) {
|
||||
return new GatewayMetricsFilter(meterRegistry);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AdaptCachedBodyGlobalFilter adaptCachedBodyGlobalFilter() {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.cloud.gateway.filter;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public class GatewayMetricsFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private MeterRegistry meterRegistry;
|
||||
|
||||
public GatewayMetricsFilter(MeterRegistry meterRegistry) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
long start = System.nanoTime();
|
||||
return chain.filter(exchange).then(Mono.defer(() -> {
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
boolean success = statusCode.is2xxSuccessful();
|
||||
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
|
||||
Iterable<Tag> iterableTags = Arrays.asList(
|
||||
Tag.of("success", Boolean.toString(success)),
|
||||
Tag.of("httpStatus", statusCode.name()),
|
||||
Tag.of("routeId", route.getId()),
|
||||
Tag.of("routeUri", route.getUri().toString()));
|
||||
meterRegistry.timer("gateway.requests", iterableTags)
|
||||
.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
|
||||
return Mono.empty();
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import reactor.ipc.netty.http.client.HttpClientOptions;
|
||||
import reactor.ipc.netty.options.ClientProxyOptions;
|
||||
import reactor.ipc.netty.resources.PoolResources;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
|
||||
@@ -36,6 +38,8 @@ public class GatewayAutoConfigurationTests {
|
||||
public void nettyHttpClientDefaults() {
|
||||
new ReactiveWebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class,
|
||||
MetricsAutoConfiguration.class,
|
||||
SimpleMetricsExportAutoConfiguration.class,
|
||||
GatewayAutoConfiguration.class))
|
||||
.withPropertyValues("debug=true")
|
||||
.run(context -> {
|
||||
@@ -59,6 +63,8 @@ public class GatewayAutoConfigurationTests {
|
||||
public void nettyHttpClientConfigured() {
|
||||
new ReactiveWebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class,
|
||||
MetricsAutoConfiguration.class,
|
||||
SimpleMetricsExportAutoConfiguration.class,
|
||||
GatewayAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.gateway.httpclient.ssl.use-insecure-trust-manager=true",
|
||||
"spring.cloud.gateway.httpclient.connect-timeout=10",
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
public class GatewayMetricFilterTests extends BaseWebClientTests {
|
||||
|
||||
private static final String REQUEST_METRICS_NAME = "gateway.requests";
|
||||
|
||||
@Autowired
|
||||
private GatewayProperties properties;
|
||||
|
||||
@Autowired
|
||||
private MeterRegistry meterRegistry;
|
||||
|
||||
@Test
|
||||
public void gatewayRequestsMeterFilterHasTags() {
|
||||
assertThat(this.properties.getDefaultFilters()).isNotEmpty();
|
||||
|
||||
testClient.get().uri("/headers").exchange().expectStatus().isOk();
|
||||
assertMetricsContainsTag("success", Boolean.TRUE.toString());
|
||||
assertMetricsContainsTag("httpStatus", HttpStatus.OK.name());
|
||||
assertMetricsContainsTag("routeId", "default_path_to_httpbin");
|
||||
assertMetricsContainsTag("routeUri", "lb://testservice");
|
||||
}
|
||||
|
||||
private void assertMetricsContainsTag(String tagKey, String tagValue) {
|
||||
assertThat(this.meterRegistry.get(REQUEST_METRICS_NAME).tag(tagKey, tagValue).timer()
|
||||
.count()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootConfiguration
|
||||
@Import(DefaultTestConfig.class)
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user