Updates metrics filter to use int status first.
This commit is contained in:
@@ -20,6 +20,8 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import io.micrometer.core.instrument.Timer.Sample;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
@@ -36,6 +38,8 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G
|
||||
*/
|
||||
public class GatewayMetricsFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GatewayMetricsFilter.class);
|
||||
|
||||
private MeterRegistry meterRegistry;
|
||||
|
||||
public GatewayMetricsFilter(MeterRegistry meterRegistry) {
|
||||
@@ -78,30 +82,42 @@ public class GatewayMetricsFilter implements GlobalFilter, Ordered {
|
||||
String httpStatusCodeStr = "NA";
|
||||
|
||||
String httpMethod = exchange.getRequest().getMethodValue();
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
if (statusCode != null) {
|
||||
httpStatusCodeStr = String.valueOf(statusCode.value());
|
||||
outcome = statusCode.series().name();
|
||||
status = statusCode.name();
|
||||
}
|
||||
else { // a non standard HTTPS status could be used. Let's be defensive here
|
||||
if (exchange.getResponse() instanceof AbstractServerHttpResponse) {
|
||||
Integer statusInt = ((AbstractServerHttpResponse) exchange.getResponse())
|
||||
.getStatusCodeValue();
|
||||
if (statusInt != null) {
|
||||
status = String.valueOf(statusInt);
|
||||
httpStatusCodeStr = status;
|
||||
}
|
||||
else {
|
||||
status = "NA";
|
||||
|
||||
// a non standard HTTPS status could be used. Let's be defensive here
|
||||
// it needs to be checked for first, otherwise the delegate response
|
||||
// who's status DIDN"T change, will be used
|
||||
if (exchange.getResponse() instanceof AbstractServerHttpResponse) {
|
||||
Integer statusInt = ((AbstractServerHttpResponse) exchange.getResponse())
|
||||
.getStatusCodeValue();
|
||||
if (statusInt != null) {
|
||||
status = String.valueOf(statusInt);
|
||||
httpStatusCodeStr = status;
|
||||
HttpStatus resolved = HttpStatus.resolve(statusInt);
|
||||
if (resolved != null) {
|
||||
// this is not a CUSTOM status, so use series here.
|
||||
outcome = resolved.series().name();
|
||||
status = resolved.name();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
if (statusCode != null) {
|
||||
httpStatusCodeStr = String.valueOf(statusCode.value());
|
||||
outcome = statusCode.series().name();
|
||||
status = statusCode.name();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO refactor to allow Tags provider like in MetricsWebFilter
|
||||
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
|
||||
Tags tags = Tags.of("outcome", outcome, "status", status, "httpStatusCode",
|
||||
httpStatusCodeStr, "routeId", route.getId(), "routeUri",
|
||||
route.getUri().toString(), "httpMethod", httpMethod);
|
||||
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("gateway.requests tags: " + tags);
|
||||
}
|
||||
sample.stop(meterRegistry.timer("gateway.requests", tags));
|
||||
}
|
||||
|
||||
|
||||
@@ -163,6 +163,9 @@ public final class ServerWebExchangeUtils {
|
||||
if (exchange.getResponse().isCommitted()) {
|
||||
return false;
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Setting response status to " + statusHolder);
|
||||
}
|
||||
if (statusHolder.getHttpStatus() != null) {
|
||||
return setResponseStatus(exchange, statusHolder.getHttpStatus());
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class GatewayMetricsFilterTests extends BaseWebClientTests {
|
||||
@Test
|
||||
public void hasMetricsForSetStatusFilter() throws InterruptedException {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.HOST, "www.setcustomstatus.org");
|
||||
headers.set(HttpHeaders.HOST, "www.setcustomstatusmetrics.org");
|
||||
// cannot use netty client since we cannot read custom http status
|
||||
ResponseEntity<String> response = new TestRestTemplate().exchange(
|
||||
baseUri + "/headers", HttpMethod.POST, new HttpEntity<>(headers),
|
||||
@@ -93,7 +93,7 @@ public class GatewayMetricsFilterTests extends BaseWebClientTests {
|
||||
assertThat(response.getStatusCodeValue()).isEqualTo(432);
|
||||
assertMetricsContainsTag("outcome", "CUSTOM");
|
||||
assertMetricsContainsTag("status", "432");
|
||||
assertMetricsContainsTag("routeId", "test_custom_http_status");
|
||||
assertMetricsContainsTag("routeId", "test_custom_http_status_metrics");
|
||||
assertMetricsContainsTag("routeUri", testUri);
|
||||
assertMetricsContainsTag("httpStatusCode", "432");
|
||||
assertMetricsContainsTag("httpMethod", HttpMethod.POST.toString());
|
||||
@@ -116,8 +116,9 @@ public class GatewayMetricsFilterTests extends BaseWebClientTests {
|
||||
@Bean
|
||||
public RouteLocator myRouteLocator(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("test_custom_http_status", r -> r.host("*.setcustomstatus.org")
|
||||
.filters(f -> f.setStatus(432)).uri(testUri))
|
||||
.route("test_custom_http_status_metrics",
|
||||
r -> r.host("*.setcustomstatusmetrics.org")
|
||||
.filters(f -> f.setStatus(432)).uri(testUri))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user